Skip to content

Commit 9692333

Browse files
crisbetojosephperrott
authored andcommitted
build: add tslint rule enforce import spacing (#12536)
1 parent 1e1751f commit 9692333

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as ts from 'typescript';
2+
import * as Lint from 'tslint';
3+
4+
/**
5+
* Rule that ensures that there are no spaces before/after the braces in import clauses.
6+
*/
7+
export class Rule extends Lint.Rules.AbstractRule {
8+
apply(sourceFile: ts.SourceFile) {
9+
return this.applyWithWalker(new Walker(sourceFile, this.getOptions()));
10+
}
11+
}
12+
13+
class Walker extends Lint.RuleWalker {
14+
visitImportDeclaration(node: ts.ImportDeclaration) {
15+
if (!node.importClause) {
16+
return super.visitImportDeclaration(node);
17+
}
18+
19+
const importClause = node.importClause.getText();
20+
21+
if (importClause.startsWith('{') && importClause.endsWith('}') && (
22+
importClause.includes('{ ') || importClause.includes(' }'))) {
23+
this.addFailureAtNode(node.importClause, 'Import clauses should not have spaces after the ' +
24+
'opening brace or before the closing one.');
25+
}
26+
27+
super.visitImportDeclaration(node);
28+
}
29+
}

tslint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
// Custom Rules
9898
"ts-loader": true,
9999
"no-exposed-todo": true,
100+
"no-import-spacing": true,
100101
"setters-after-getters": true,
101102
"rxjs-imports": true,
102103
"no-host-decorator-in-concrete": [

0 commit comments

Comments
 (0)