From e9332c66d272c572f484b68262506fcec8f1ce3f Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Mon, 28 Mar 2016 14:25:22 -0700 Subject: [PATCH] chore(lint): re-enable linter and fix violations fixes #7798 --- gulpfile.js | 12 +----- tools/tslint/duplicateModuleImportRule.ts | 49 +++++++++++++++++++++++ tslint.json | 10 +++++ 3 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 tools/tslint/duplicateModuleImportRule.ts create mode 100644 tslint.json diff --git a/gulpfile.js b/gulpfile.js index 31266f44f3..4deb8ea86b 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -329,17 +329,7 @@ gulp.task('lint', ['build.tools'], function() { var tslint = require('gulp-tslint'); // Built-in rules are at // https://github.com/palantir/tslint#supported-rules - var tslintConfig = { - "rules": { - "requireInternalWithUnderscore": true, - "requireParameterType": true, - "requireReturnType": true, - "semicolon": true, - - // TODO: find a way to just screen for reserved names - "variable-name": false - } - }; + var tslintConfig = require('./tslint.json'); return gulp.src(['modules/angular2/src/**/*.ts', '!modules/angular2/src/testing/**']) .pipe(tslint({ tslint: require('tslint').default, diff --git a/tools/tslint/duplicateModuleImportRule.ts b/tools/tslint/duplicateModuleImportRule.ts new file mode 100644 index 0000000000..b313a85cd2 --- /dev/null +++ b/tools/tslint/duplicateModuleImportRule.ts @@ -0,0 +1,49 @@ +import {RuleFailure} from 'tslint/lib/lint'; +import {AbstractRule} from 'tslint/lib/rules'; +import {RuleWalker} from 'tslint/lib/language/walker'; +import * as ts from 'typescript'; + +export class Rule extends AbstractRule { + public static FAILURE_STRING = "duplicate module import"; + + public apply(sourceFile: ts.SourceFile): RuleFailure[] { + const typedefWalker = new ModuleImportWalker(sourceFile, this.getOptions()); + return this.applyWithWalker(typedefWalker); + } +} + +class ModuleImportWalker extends RuleWalker { + importModulesSeen: string[] = []; + + protected visitImportDeclaration(node: ts.ImportDeclaration): void { + this.visitModuleSpecifier(node.moduleSpecifier); + super.visitImportDeclaration(node); + } + + protected visitImportEqualsDeclaration(node: ts.ImportEqualsDeclaration): void { + this.visitModuleSpecifier(node.moduleReference); + super.visitImportEqualsDeclaration(node); + } + + private checkTypeAnnotation(location: number, typeAnnotation: ts.TypeNode, name?: ts.Node) { + if (typeAnnotation == null) { + let ns = ""; + if (name != null && name.kind === ts.SyntaxKind.Identifier) { + ns = (name).text; + } + if (ns.charAt(0) === '_') return; + let failure = this.createFailure(location, 1, "expected parameter " + ns + " to have a type"); + this.addFailure(failure); + } + } + + private visitModuleSpecifier(moduleSpecifier: ts.Node) { + var text = moduleSpecifier.getText(); + if (this.importModulesSeen.indexOf(text) >= 0) { + let failure = + this.createFailure(moduleSpecifier.getEnd(), 1, "Duplicate imports from module " + text); + this.addFailure(failure); + } + this.importModulesSeen.push(text); + } +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000000..b1c5549a56 --- /dev/null +++ b/tslint.json @@ -0,0 +1,10 @@ +{ + "rules": { + "requireInternalWithUnderscore": true, + "requireParameterType": true, + "requireReturnType": true, + "duplicateModuleImport": false, + "semicolon": true, + "variable-name": false + } +}