From 96923331e9dcb7090661c0da043af2a18886e13a Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Tue, 7 Aug 2018 22:26:27 +0200 Subject: [PATCH] build: add tslint rule enforce import spacing (#12536) --- tools/tslint-rules/noImportSpacingRule.ts | 29 +++++++++++++++++++++++ tslint.json | 1 + 2 files changed, 30 insertions(+) create mode 100644 tools/tslint-rules/noImportSpacingRule.ts diff --git a/tools/tslint-rules/noImportSpacingRule.ts b/tools/tslint-rules/noImportSpacingRule.ts new file mode 100644 index 00000000000..be591d2e0c7 --- /dev/null +++ b/tools/tslint-rules/noImportSpacingRule.ts @@ -0,0 +1,29 @@ +import * as ts from 'typescript'; +import * as Lint from 'tslint'; + +/** + * Rule that ensures that there are no spaces before/after the braces in import clauses. + */ +export class Rule extends Lint.Rules.AbstractRule { + apply(sourceFile: ts.SourceFile) { + return this.applyWithWalker(new Walker(sourceFile, this.getOptions())); + } +} + +class Walker extends Lint.RuleWalker { + visitImportDeclaration(node: ts.ImportDeclaration) { + if (!node.importClause) { + return super.visitImportDeclaration(node); + } + + const importClause = node.importClause.getText(); + + if (importClause.startsWith('{') && importClause.endsWith('}') && ( + importClause.includes('{ ') || importClause.includes(' }'))) { + this.addFailureAtNode(node.importClause, 'Import clauses should not have spaces after the ' + + 'opening brace or before the closing one.'); + } + + super.visitImportDeclaration(node); + } +} diff --git a/tslint.json b/tslint.json index e4192481325..6d8f8647fe6 100644 --- a/tslint.json +++ b/tslint.json @@ -97,6 +97,7 @@ // Custom Rules "ts-loader": true, "no-exposed-todo": true, + "no-import-spacing": true, "setters-after-getters": true, "rxjs-imports": true, "no-host-decorator-in-concrete": [