diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-08-14 05:01:11 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-08-14 05:02:09 +0200 |
commit | 363723fc84f7b8477592e0105aeb331ec9a017af (patch) | |
tree | 29f92724f34131bac64d6a318dd7e30612e631c7 /node_modules/tslint/lib/rules/noUnusedVariableRule.js | |
parent | 5634e77ad96bfe1818f6b6ee70b7379652e5487f (diff) |
node_modules
Diffstat (limited to 'node_modules/tslint/lib/rules/noUnusedVariableRule.js')
-rw-r--r-- | node_modules/tslint/lib/rules/noUnusedVariableRule.js | 121 |
1 files changed, 73 insertions, 48 deletions
diff --git a/node_modules/tslint/lib/rules/noUnusedVariableRule.js b/node_modules/tslint/lib/rules/noUnusedVariableRule.js index 79fcb599d..e4fab8a3e 100644 --- a/node_modules/tslint/lib/rules/noUnusedVariableRule.js +++ b/node_modules/tslint/lib/rules/noUnusedVariableRule.js @@ -31,45 +31,45 @@ var Rule = (function (_super) { Rule.prototype.applyWithProgram = function (sourceFile, program) { var _this = this; var x = program.getCompilerOptions(); - if (x.noUnusedLocals === true && x.noUnusedParameters === true) { + if (x.noUnusedLocals && x.noUnusedParameters) { console.warn("WARNING: 'no-unused-variable' lint rule does not need to be set if " + "the 'no-unused-locals' and 'no-unused-parameters' compiler options are enabled."); } return this.applyWithFunction(sourceFile, function (ctx) { return walk(ctx, program, parseOptions(_this.ruleArguments)); }); }; - return Rule; -}(Lint.Rules.TypedRule)); -/* tslint:disable:object-literal-sort-keys */ -Rule.metadata = { - ruleName: "no-unused-variable", - description: (_a = ["Disallows unused imports, variables, functions and\n private class members. Similar to tsc's --noUnusedParameters and --noUnusedLocals\n options, but does not interrupt code compilation."], _a.raw = ["Disallows unused imports, variables, functions and\n private class members. Similar to tsc's --noUnusedParameters and --noUnusedLocals\n options, but does not interrupt code compilation."], Lint.Utils.dedent(_a)), - hasFix: true, - optionsDescription: (_b = ["\n Three optional arguments may be optionally provided:\n\n * `\"check-parameters\"` disallows unused function and constructor parameters.\n * NOTE: this option is experimental and does not work with classes\n that use abstract method declarations, among other things.\n * `{\"ignore-pattern\": \"pattern\"}` where pattern is a case-sensitive regexp.\n Variable names that match the pattern will be ignored."], _b.raw = ["\n Three optional arguments may be optionally provided:\n\n * \\`\"check-parameters\"\\` disallows unused function and constructor parameters.\n * NOTE: this option is experimental and does not work with classes\n that use abstract method declarations, among other things.\n * \\`{\"ignore-pattern\": \"pattern\"}\\` where pattern is a case-sensitive regexp.\n Variable names that match the pattern will be ignored."], Lint.Utils.dedent(_b)), - options: { - type: "array", - items: { - oneOf: [ - { - type: "string", - enum: ["check-parameters"], - }, - { - type: "object", - properties: { - "ignore-pattern": { type: "string" }, + /* tslint:disable:object-literal-sort-keys */ + Rule.metadata = { + ruleName: "no-unused-variable", + description: (_a = ["Disallows unused imports, variables, functions and\n private class members. Similar to tsc's --noUnusedParameters and --noUnusedLocals\n options, but does not interrupt code compilation."], _a.raw = ["Disallows unused imports, variables, functions and\n private class members. Similar to tsc's --noUnusedParameters and --noUnusedLocals\n options, but does not interrupt code compilation."], Lint.Utils.dedent(_a)), + hasFix: true, + optionsDescription: (_b = ["\n Three optional arguments may be optionally provided:\n\n * `\"check-parameters\"` disallows unused function and constructor parameters.\n * NOTE: this option is experimental and does not work with classes\n that use abstract method declarations, among other things.\n * `{\"ignore-pattern\": \"pattern\"}` where pattern is a case-sensitive regexp.\n Variable names that match the pattern will be ignored."], _b.raw = ["\n Three optional arguments may be optionally provided:\n\n * \\`\"check-parameters\"\\` disallows unused function and constructor parameters.\n * NOTE: this option is experimental and does not work with classes\n that use abstract method declarations, among other things.\n * \\`{\"ignore-pattern\": \"pattern\"}\\` where pattern is a case-sensitive regexp.\n Variable names that match the pattern will be ignored."], Lint.Utils.dedent(_b)), + options: { + type: "array", + items: { + oneOf: [ + { + type: "string", + enum: ["check-parameters"], + }, + { + type: "object", + properties: { + "ignore-pattern": { type: "string" }, + }, + additionalProperties: false, }, - additionalProperties: false, - }, - ], + ], + }, + minLength: 0, + maxLength: 3, }, - minLength: 0, - maxLength: 3, - }, - optionExamples: [true, [true, { "ignore-pattern": "^_" }]], - type: "functionality", - typescriptOnly: true, - requiresTypeInfo: true, -}; + optionExamples: [true, [true, { "ignore-pattern": "^_" }]], + type: "functionality", + typescriptOnly: true, + requiresTypeInfo: true, + }; + return Rule; +}(Lint.Rules.TypedRule)); exports.Rule = Rule; function parseOptions(options) { var checkParameters = options.indexOf(OPTION_CHECK_PARAMETERS) !== -1; @@ -93,10 +93,14 @@ function walk(ctx, program, _a) { var unusedCheckedProgram = getUnusedCheckedProgram(program, checkParameters); var diagnostics = ts.getPreEmitDiagnostics(unusedCheckedProgram, sourceFile); var checker = unusedCheckedProgram.getTypeChecker(); // Doesn't matter which program is used for this. + var declaration = program.getCompilerOptions().declaration; // If all specifiers in an import are unused, we elide the entire import. var importSpecifierFailures = new Map(); for (var _i = 0, diagnostics_1 = diagnostics; _i < diagnostics_1.length; _i++) { var diag = diagnostics_1[_i]; + if (diag.start === undefined) { + continue; + } var kind = getUnusedDiagnostic(diag); if (kind === undefined) { continue; @@ -105,7 +109,7 @@ function walk(ctx, program, _a) { if (kind === 0 /* VARIABLE_OR_PARAMETER */) { var importName = findImport(diag.start, sourceFile); if (importName !== undefined) { - if (isImportUsed(importName, sourceFile, checker)) { + if (declaration && isImportUsed(importName, sourceFile, checker)) { continue; } if (importSpecifierFailures.has(importName)) { @@ -133,8 +137,6 @@ function walk(ctx, program, _a) { * - Unused imports are fixable. */ function addImportSpecifierFailures(ctx, failures, sourceFile) { - // tslint:disable return-undefined - // (fixed in tslint 5.3) forEachImport(sourceFile, function (importNode) { if (importNode.kind === ts.SyntaxKind.ImportEqualsDeclaration) { tryRemoveAll(importNode.name); @@ -203,9 +205,28 @@ function addImportSpecifierFailures(ctx, failures, sourceFile) { } } function removeAll(errorNode, failure) { - var fix = Lint.Replacement.deleteFromTo(importNode.getStart(), importNode.getEnd()); + var start = importNode.getStart(); + var end = importNode.getEnd(); + if (isEntireLine(start, end)) { + end = getNextLineStart(end); + } + var fix = Lint.Replacement.deleteFromTo(start, end); ctx.addFailureAtNode(errorNode, failure, fix); } + function isEntireLine(start, end) { + return ctx.sourceFile.getLineAndCharacterOfPosition(start).character === 0 && + ctx.sourceFile.getLineEndOfPosition(end) === end; + } + function getNextLineStart(position) { + var nextLine = ctx.sourceFile.getLineAndCharacterOfPosition(position).line + 1; + var lineStarts = ctx.sourceFile.getLineStarts(); + if (nextLine < lineStarts.length) { + return lineStarts[nextLine]; + } + else { + return position; + } + } }); if (failures.size !== 0) { throw new Error("Should have revisited all import specifier failures."); @@ -242,10 +263,12 @@ function isImportUsed(importSpecifier, sourceFile, checker) { return true; } return ts.forEachChild(child, cb); - }); + }) === true; } function getImplicitType(node, checker) { - if ((utils.isPropertyDeclaration(node) || utils.isVariableDeclaration(node)) && node.type === undefined) { + if ((utils.isPropertyDeclaration(node) || utils.isVariableDeclaration(node)) && + node.type === undefined && node.name.kind === ts.SyntaxKind.Identifier || + utils.isBindingElement(node) && node.name.kind === ts.SyntaxKind.Identifier) { return checker.getTypeAtLocation(node); } else if (utils.isSignatureDeclaration(node) && node.type === undefined) { @@ -327,24 +350,26 @@ function getUnusedCheckedProgram(program, checkParameters) { return checkedProgram; } function makeUnusedCheckedProgram(program, checkParameters) { - var options = tslib_1.__assign({}, program.getCompilerOptions(), { noUnusedLocals: true }, (checkParameters ? { noUnusedParameters: true } : null)); - var sourceFilesByName = new Map(program.getSourceFiles().map(function (s) { return [s.fileName, s]; })); + var originalOptions = program.getCompilerOptions(); + var options = tslib_1.__assign({}, originalOptions, { noEmit: true, noUnusedLocals: true, noUnusedParameters: originalOptions.noUnusedParameters || checkParameters }); + var sourceFilesByName = new Map(program.getSourceFiles().map(function (s) { return [getCanonicalFileName(s.fileName), s]; })); // tslint:disable object-literal-sort-keys return ts.createProgram(Array.from(sourceFilesByName.keys()), options, { - fileExists: function (f) { return sourceFilesByName.has(f); }, - readFile: function (f) { - var s = sourceFilesByName.get(f); - return s.text; - }, - getSourceFile: function (f) { return sourceFilesByName.get(f); }, + fileExists: function (f) { return sourceFilesByName.has(getCanonicalFileName(f)); }, + readFile: function (f) { return sourceFilesByName.get(getCanonicalFileName(f)).text; }, + getSourceFile: function (f) { return sourceFilesByName.get(getCanonicalFileName(f)); }, getDefaultLibFileName: function () { return ts.getDefaultLibFileName(options); }, writeFile: function () { }, getCurrentDirectory: function () { return ""; }, getDirectories: function () { return []; }, - getCanonicalFileName: function (f) { return f; }, - useCaseSensitiveFileNames: function () { return true; }, + getCanonicalFileName: getCanonicalFileName, + useCaseSensitiveFileNames: function () { return ts.sys.useCaseSensitiveFileNames; }, getNewLine: function () { return "\n"; }, }); // tslint:enable object-literal-sort-keys + // We need to be careful with file system case sensitivity + function getCanonicalFileName(fileName) { + return ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase(); + } } var _a, _b; |