From 5862b41657f51d54456055397b0c54fd442ef22a Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Tue, 2 Oct 2018 17:08:07 -0700 Subject: [PATCH] Fixing strict null rules in tslint --- build/lib/tslint/layeringRule.ts | 6 ++--- .../lib/tslint/noUnexternalizedStringsRule.js | 7 +++--- .../lib/tslint/noUnexternalizedStringsRule.ts | 25 ++++++++++--------- build/lib/tslint/translationRemindRule.ts | 4 +-- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/build/lib/tslint/layeringRule.ts b/build/lib/tslint/layeringRule.ts index 089fd764afe..6b28270dabc 100644 --- a/build/lib/tslint/layeringRule.ts +++ b/build/lib/tslint/layeringRule.ts @@ -18,7 +18,7 @@ export class Rule extends Lint.Rules.AbstractRule { const parts = dirname(sourceFile.fileName).split(/\\|\//); let ruleArgs = this.getOptions().ruleArguments[0]; - let config: Config; + let config: Config | undefined; for (let i = parts.length - 1; i >= 0; i--) { if (ruleArgs[parts[i]]) { config = { @@ -26,8 +26,8 @@ export class Rule extends Lint.Rules.AbstractRule { disallowed: new Set() }; Object.keys(ruleArgs).forEach(key => { - if (!config.allowed.has(key)) { - config.disallowed.add(key); + if (!config!.allowed.has(key)) { + config!.disallowed.add(key); } }); break; diff --git a/build/lib/tslint/noUnexternalizedStringsRule.js b/build/lib/tslint/noUnexternalizedStringsRule.js index f4ca615803a..2b2dff0efe8 100644 --- a/build/lib/tslint/noUnexternalizedStringsRule.js +++ b/build/lib/tslint/noUnexternalizedStringsRule.js @@ -116,12 +116,12 @@ var NoUnexternalizedStringsRuleWalker = /** @class */ (function (_super) { return; } // We have a string that is a direct argument into the localize call. - var keyArg = callInfo.argIndex === this.keyIndex + var keyArg = callInfo && callInfo.argIndex === this.keyIndex ? callInfo.callExpression.arguments[this.keyIndex] : null; if (keyArg) { if (isStringLiteral(keyArg)) { - this.recordKey(keyArg, this.messageIndex ? callInfo.callExpression.arguments[this.messageIndex] : undefined); + this.recordKey(keyArg, this.messageIndex && callInfo ? callInfo.callExpression.arguments[this.messageIndex] : undefined); } else if (isObjectLiteral(keyArg)) { for (var i = 0; i < keyArg.properties.length; i++) { @@ -131,7 +131,7 @@ var NoUnexternalizedStringsRuleWalker = /** @class */ (function (_super) { if (name_1 === 'key') { var initializer = property.initializer; if (isStringLiteral(initializer)) { - this.recordKey(initializer, this.messageIndex ? callInfo.callExpression.arguments[this.messageIndex] : undefined); + this.recordKey(initializer, this.messageIndex && callInfo ? callInfo.callExpression.arguments[this.messageIndex] : undefined); } break; } @@ -188,6 +188,7 @@ var NoUnexternalizedStringsRuleWalker = /** @class */ (function (_super) { } node = parent; } + return null; }; NoUnexternalizedStringsRuleWalker.ImportFailureMessage = 'Do not use double quotes for imports.'; NoUnexternalizedStringsRuleWalker.DOUBLE_QUOTE = '"'; diff --git a/build/lib/tslint/noUnexternalizedStringsRule.ts b/build/lib/tslint/noUnexternalizedStringsRule.ts index abcd5496670..ce7913279eb 100644 --- a/build/lib/tslint/noUnexternalizedStringsRule.ts +++ b/build/lib/tslint/noUnexternalizedStringsRule.ts @@ -40,7 +40,7 @@ function isPropertyAssignment(node: ts.Node): node is ts.PropertyAssignment { interface KeyMessagePair { key: ts.StringLiteral; - message: ts.Node; + message: ts.Node | undefined; } class NoUnexternalizedStringsRuleWalker extends Lint.RuleWalker { @@ -50,8 +50,8 @@ class NoUnexternalizedStringsRuleWalker extends Lint.RuleWalker { private static DOUBLE_QUOTE: string = '"'; private signatures: Map; - private messageIndex: number; - private keyIndex: number; + private messageIndex: number | undefined; + private keyIndex: number | undefined; private ignores: Map; private usedKeys: Map; @@ -121,7 +121,7 @@ class NoUnexternalizedStringsRuleWalker extends Lint.RuleWalker { return; } - if (doubleQuoted && (!callInfo || callInfo.argIndex === -1 || !this.signatures[functionName])) { + if (doubleQuoted && (!callInfo || callInfo.argIndex === -1 || !this.signatures[functionName!])) { const s = node.getText(); const fix = [ Lint.Replacement.replaceFromTo(node.getStart(), node.getWidth(), `nls.localize('KEY-${s.substring(1, s.length - 1)}', ${s})`), @@ -130,16 +130,16 @@ class NoUnexternalizedStringsRuleWalker extends Lint.RuleWalker { return; } // We have a single quoted string outside a localize function name. - if (!doubleQuoted && !this.signatures[functionName]) { + if (!doubleQuoted && !this.signatures[functionName!]) { return; } // We have a string that is a direct argument into the localize call. - let keyArg: ts.Expression = callInfo.argIndex === this.keyIndex + let keyArg: ts.Expression | null = callInfo && callInfo.argIndex === this.keyIndex ? callInfo.callExpression.arguments[this.keyIndex] : null; if (keyArg) { if (isStringLiteral(keyArg)) { - this.recordKey(keyArg, this.messageIndex ? callInfo.callExpression.arguments[this.messageIndex] : undefined); + this.recordKey(keyArg, this.messageIndex && callInfo ? callInfo.callExpression.arguments[this.messageIndex] : undefined); } else if (isObjectLiteral(keyArg)) { for (let i = 0; i < keyArg.properties.length; i++) { let property = keyArg.properties[i]; @@ -148,7 +148,7 @@ class NoUnexternalizedStringsRuleWalker extends Lint.RuleWalker { if (name === 'key') { let initializer = property.initializer; if (isStringLiteral(initializer)) { - this.recordKey(initializer, this.messageIndex ? callInfo.callExpression.arguments[this.messageIndex] : undefined); + this.recordKey(initializer, this.messageIndex && callInfo ? callInfo.callExpression.arguments[this.messageIndex] : undefined); } break; } @@ -157,17 +157,17 @@ class NoUnexternalizedStringsRuleWalker extends Lint.RuleWalker { } } - const messageArg = callInfo.callExpression.arguments[this.messageIndex]; + const messageArg = callInfo!.callExpression.arguments[this.messageIndex!]; if (messageArg && messageArg.kind !== ts.SyntaxKind.StringLiteral) { this.addFailure(this.createFailure( messageArg.getStart(), messageArg.getWidth(), - `Message argument to '${callInfo.callExpression.expression.getText()}' must be a string literal.`)); + `Message argument to '${callInfo!.callExpression.expression.getText()}' must be a string literal.`)); return; } } - private recordKey(keyNode: ts.StringLiteral, messageNode: ts.Node) { + private recordKey(keyNode: ts.StringLiteral, messageNode: ts.Node | undefined) { let text = keyNode.getText(); // We have an empty key if (text.match(/(['"]) *\1/)) { @@ -191,7 +191,7 @@ class NoUnexternalizedStringsRuleWalker extends Lint.RuleWalker { occurrences.push({ key: keyNode, message: messageNode }); } - private findDescribingParent(node: ts.Node): { callInfo?: { callExpression: ts.CallExpression, argIndex: number }, isImport?: boolean; } { + private findDescribingParent(node: ts.Node): { callInfo?: { callExpression: ts.CallExpression, argIndex: number }, isImport?: boolean; } | null { let parent: ts.Node; while ((parent = node.parent)) { let kind = parent.kind; @@ -208,5 +208,6 @@ class NoUnexternalizedStringsRuleWalker extends Lint.RuleWalker { } node = parent; } + return null; } } diff --git a/build/lib/tslint/translationRemindRule.ts b/build/lib/tslint/translationRemindRule.ts index 2c5adcc4c49..e1488cde66b 100644 --- a/build/lib/tslint/translationRemindRule.ts +++ b/build/lib/tslint/translationRemindRule.ts @@ -30,7 +30,7 @@ class TranslationRemindRuleWalker extends Lint.RuleWalker { this.visitImportLikeDeclaration(node); } - protected visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration): void { + protected visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration): void { const reference = node.moduleReference.getText(); if (reference !== `require('${TranslationRemindRuleWalker.NLS_MODULE}')`) { return; @@ -47,7 +47,7 @@ class TranslationRemindRuleWalker extends Lint.RuleWalker { return; } - const resource = matchService ? matchService[0] : matchPart[0]; + const resource = matchService ? matchService[0] : matchPart![0]; let resourceDefined = false; let json;