diff options
Diffstat (limited to 'node_modules/tslint/lib/rules/memberAccessRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/memberAccessRule.js | 76 |
1 files changed, 42 insertions, 34 deletions
diff --git a/node_modules/tslint/lib/rules/memberAccessRule.js b/node_modules/tslint/lib/rules/memberAccessRule.js index c25b3826a..2143f15d4 100644 --- a/node_modules/tslint/lib/rules/memberAccessRule.js +++ b/node_modules/tslint/lib/rules/memberAccessRule.js @@ -19,6 +19,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var tsutils_1 = require("tsutils"); var ts = require("typescript"); +var error_1 = require("../error"); var Lint = require("../index"); var OPTION_NO_PUBLIC = "no-public"; var OPTION_CHECK_ACCESSOR = "check-accessor"; @@ -39,37 +40,44 @@ var Rule = (function (_super) { var checkConstructor = options.indexOf(OPTION_CHECK_CONSTRUCTOR) !== -1; if (noPublic) { if (checkAccessor || checkConstructor) { - throw new Error("If 'no-public' is present, it should be the only option."); + error_1.showWarningOnce("Warning: " + this.ruleName + " - If 'no-public' is present, it should be the only option."); + return []; } checkAccessor = checkConstructor = true; } - return this.applyWithFunction(sourceFile, function (ctx) { return walk(ctx, noPublic, checkAccessor, checkConstructor); }); + return this.applyWithFunction(sourceFile, walk, { + checkAccessor: checkAccessor, + checkConstructor: checkConstructor, + noPublic: noPublic, + }); }; + /* tslint:disable:object-literal-sort-keys */ + Rule.metadata = { + ruleName: "member-access", + description: "Requires explicit visibility declarations for class members.", + rationale: "Explicit visibility declarations can make code more readable and accessible for those new to TS.", + optionsDescription: (_a = ["\n These arguments may be optionally provided:\n\n * `\"no-public\"` forbids public accessibility to be specified, because this is the default.\n * `\"check-accessor\"` enforces explicit visibility on get/set accessors\n * `\"check-constructor\"` enforces explicit visibility on constructors"], _a.raw = ["\n These arguments may be optionally provided:\n\n * \\`\"no-public\"\\` forbids public accessibility to be specified, because this is the default.\n * \\`\"check-accessor\"\\` enforces explicit visibility on get/set accessors\n * \\`\"check-constructor\"\\` enforces explicit visibility on constructors"], Lint.Utils.dedent(_a)), + options: { + type: "array", + items: { + type: "string", + enum: [OPTION_NO_PUBLIC, OPTION_CHECK_ACCESSOR, OPTION_CHECK_CONSTRUCTOR], + }, + minLength: 0, + maxLength: 3, + }, + optionExamples: [true, [true, OPTION_NO_PUBLIC], [true, OPTION_CHECK_ACCESSOR]], + type: "typescript", + typescriptOnly: true, + hasFix: true, + }; + /* tslint:enable:object-literal-sort-keys */ + Rule.FAILURE_STRING_NO_PUBLIC = "'public' is implicit."; return Rule; }(Lint.Rules.AbstractRule)); -/* tslint:disable:object-literal-sort-keys */ -Rule.metadata = { - ruleName: "member-access", - description: "Requires explicit visibility declarations for class members.", - rationale: "Explicit visibility declarations can make code more readable and accessible for those new to TS.", - optionsDescription: (_a = ["\n These arguments may be optionally provided:\n\n * `\"no-public\"` forbids public accessibility to be specified, because this is the default.\n * `\"check-accessor\"` enforces explicit visibility on get/set accessors\n * `\"check-constructor\"` enforces explicit visibility on constructors"], _a.raw = ["\n These arguments may be optionally provided:\n\n * \\`\"no-public\"\\` forbids public accessibility to be specified, because this is the default.\n * \\`\"check-accessor\"\\` enforces explicit visibility on get/set accessors\n * \\`\"check-constructor\"\\` enforces explicit visibility on constructors"], Lint.Utils.dedent(_a)), - options: { - type: "array", - items: { - type: "string", - enum: [OPTION_NO_PUBLIC, OPTION_CHECK_ACCESSOR, OPTION_CHECK_CONSTRUCTOR], - }, - minLength: 0, - maxLength: 3, - }, - optionExamples: [true, [true, OPTION_NO_PUBLIC], [true, OPTION_CHECK_ACCESSOR]], - type: "typescript", - typescriptOnly: true, -}; -/* tslint:enable:object-literal-sort-keys */ -Rule.FAILURE_STRING_NO_PUBLIC = "'public' is implicit."; exports.Rule = Rule; -function walk(ctx, noPublic, checkAccessor, checkConstructor) { +function walk(ctx) { + var _a = ctx.options, noPublic = _a.noPublic, checkAccessor = _a.checkAccessor, checkConstructor = _a.checkConstructor; return ts.forEachChild(ctx.sourceFile, function recur(node) { if (tsutils_1.isClassLikeDeclaration(node)) { for (var _i = 0, _a = node.members; _i < _a.length; _i++) { @@ -99,21 +107,21 @@ function walk(ctx, noPublic, checkAccessor, checkConstructor) { if (Lint.hasModifier(node.modifiers, ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.PrivateKeyword)) { return; } - var isPublic = Lint.hasModifier(node.modifiers, ts.SyntaxKind.PublicKeyword); - if (noPublic && isPublic) { - var publicKeyword = node.modifiers.find(function (m) { return m.kind === ts.SyntaxKind.PublicKeyword; }); - ctx.addFailureAtNode(publicKeyword, Rule.FAILURE_STRING_NO_PUBLIC); + var publicKeyword = tsutils_1.getModifier(node, ts.SyntaxKind.PublicKeyword); + if (noPublic && publicKeyword !== undefined) { + var start = publicKeyword.end - "public".length; + ctx.addFailure(start, publicKeyword.end, Rule.FAILURE_STRING_NO_PUBLIC, Lint.Replacement.deleteFromTo(start, tsutils_1.getNextToken(publicKeyword, ctx.sourceFile).getStart(ctx.sourceFile))); } - if (!noPublic && !isPublic) { - var nameNode = tsutils_1.isConstructorDeclaration(node) - ? tsutils_1.getChildOfKind(node, ts.SyntaxKind.ConstructorKeyword) + if (!noPublic && publicKeyword === undefined) { + var nameNode = node.kind === ts.SyntaxKind.Constructor + ? tsutils_1.getChildOfKind(node, ts.SyntaxKind.ConstructorKeyword, ctx.sourceFile) : node.name !== undefined ? node.name : node; - var memberName = node.name !== undefined && tsutils_1.isIdentifier(node.name) ? node.name.text : undefined; - ctx.addFailureAtNode(nameNode, Rule.FAILURE_STRING_FACTORY(memberType(node), memberName)); + var memberName = node.name !== undefined && node.name.kind === ts.SyntaxKind.Identifier ? node.name.text : undefined; + ctx.addFailureAtNode(nameNode, Rule.FAILURE_STRING_FACTORY(typeToString(node), memberName), Lint.Replacement.appendText(node.getStart(ctx.sourceFile), "public ")); } } } -function memberType(node) { +function typeToString(node) { switch (node.kind) { case ts.SyntaxKind.MethodDeclaration: return "class method"; |