aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/rules/memberAccessRule.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/tslint/lib/rules/memberAccessRule.js')
-rw-r--r--node_modules/tslint/lib/rules/memberAccessRule.js34
1 files changed, 26 insertions, 8 deletions
diff --git a/node_modules/tslint/lib/rules/memberAccessRule.js b/node_modules/tslint/lib/rules/memberAccessRule.js
index 599a0952e..abb843b04 100644
--- a/node_modules/tslint/lib/rules/memberAccessRule.js
+++ b/node_modules/tslint/lib/rules/memberAccessRule.js
@@ -24,6 +24,7 @@ var Lint = require("../index");
var OPTION_NO_PUBLIC = "no-public";
var OPTION_CHECK_ACCESSOR = "check-accessor";
var OPTION_CHECK_CONSTRUCTOR = "check-constructor";
+var OPTION_CHECK_PARAMETER_PROPERTY = "check-parameter-property";
var Rule = /** @class */ (function (_super) {
tslib_1.__extends(Rule, _super);
function Rule() {
@@ -38,16 +39,18 @@ var Rule = /** @class */ (function (_super) {
var noPublic = options.indexOf(OPTION_NO_PUBLIC) !== -1;
var checkAccessor = options.indexOf(OPTION_CHECK_ACCESSOR) !== -1;
var checkConstructor = options.indexOf(OPTION_CHECK_CONSTRUCTOR) !== -1;
+ var checkParameterProperty = options.indexOf(OPTION_CHECK_PARAMETER_PROPERTY) !== -1;
if (noPublic) {
- if (checkAccessor || checkConstructor) {
+ if (checkAccessor || checkConstructor || checkParameterProperty) {
error_1.showWarningOnce("Warning: " + this.ruleName + " - If 'no-public' is present, it should be the only option.");
return [];
}
- checkAccessor = checkConstructor = true;
+ checkAccessor = checkConstructor = checkParameterProperty = true;
}
return this.applyWithFunction(sourceFile, walk, {
checkAccessor: checkAccessor,
checkConstructor: checkConstructor,
+ checkParameterProperty: checkParameterProperty,
noPublic: noPublic,
});
};
@@ -56,15 +59,15 @@ var Rule = /** @class */ (function (_super) {
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)),
+ 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\n * `\"check-parameter-property\"` enforces explicit visibility on parameter properties"], _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\n * \\`\"check-parameter-property\"\\` enforces explicit visibility on parameter properties"], Lint.Utils.dedent(_a)),
options: {
type: "array",
items: {
type: "string",
- enum: [OPTION_NO_PUBLIC, OPTION_CHECK_ACCESSOR, OPTION_CHECK_CONSTRUCTOR],
+ enum: [OPTION_NO_PUBLIC, OPTION_CHECK_ACCESSOR, OPTION_CHECK_CONSTRUCTOR, OPTION_CHECK_PARAMETER_PROPERTY],
},
minLength: 0,
- maxLength: 3,
+ maxLength: 4,
},
optionExamples: [true, [true, OPTION_NO_PUBLIC], [true, OPTION_CHECK_ACCESSOR]],
type: "typescript",
@@ -77,7 +80,7 @@ var Rule = /** @class */ (function (_super) {
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
function walk(ctx) {
- var _a = ctx.options, noPublic = _a.noPublic, checkAccessor = _a.checkAccessor, checkConstructor = _a.checkConstructor;
+ var _a = ctx.options, noPublic = _a.noPublic, checkAccessor = _a.checkAccessor, checkConstructor = _a.checkConstructor, checkParameterProperty = _a.checkParameterProperty;
return ts.forEachChild(ctx.sourceFile, function recur(node) {
if (tsutils_1.isClassLikeDeclaration(node)) {
for (var _i = 0, _a = node.members; _i < _a.length; _i++) {
@@ -85,6 +88,14 @@ function walk(ctx) {
if (shouldCheck(child)) {
check(child);
}
+ if (checkParameterProperty && tsutils_1.isConstructorDeclaration(child) && child.body !== undefined) {
+ for (var _b = 0, _c = child.parameters; _b < _c.length; _b++) {
+ var param = _c[_b];
+ if (tsutils_1.isParameterProperty(param)) {
+ check(param);
+ }
+ }
+ }
}
}
return ts.forEachChild(node, recur);
@@ -109,8 +120,13 @@ function walk(ctx) {
}
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)));
+ var readonlyKeyword = tsutils_1.getModifier(node, ts.SyntaxKind.ReadonlyKeyword);
+ // public is not optional for parameter property without the readonly modifier
+ var isPublicOptional = node.kind !== ts.SyntaxKind.Parameter || readonlyKeyword !== undefined;
+ if (isPublicOptional) {
+ 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 && publicKeyword === undefined) {
var nameNode = node.kind === ts.SyntaxKind.Constructor
@@ -137,6 +153,8 @@ function typeToString(node) {
return "get property accessor";
case ts.SyntaxKind.SetAccessor:
return "set property accessor";
+ case ts.SyntaxKind.Parameter:
+ return "parameter property";
default:
throw new Error("unhandled node type " + ts.SyntaxKind[node.kind]);
}