|
| 1 | +import * as ts from 'typescript'; |
| 2 | +import * as Lint from 'tslint/lib/lint'; |
| 3 | + |
| 4 | +const OPTION_ALWAYS = 'always'; |
| 5 | + |
| 6 | +export class Rule extends Lint.Rules.AbstractRule { |
| 7 | + public static FAILURE_STRING = { |
| 8 | + noBeginningSpace: 'There should be no space after "["', |
| 9 | + noEndingSpace: 'There should be no space before "]"', |
| 10 | + requiredBeginningSpace: 'A space is required after "["', |
| 11 | + requiredEndingSpace: 'A space is required before "]"' |
| 12 | + }; |
| 13 | + |
| 14 | + public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { |
| 15 | + const walker = new ArrayBracketSpacingWalker(sourceFile, this.getOptions()); |
| 16 | + return this.applyWithWalker(walker); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class ArrayBracketSpacingWalker extends Lint.RuleWalker { |
| 21 | + |
| 22 | + private always: boolean; |
| 23 | + private never: boolean; |
| 24 | + constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) { |
| 25 | + super(sourceFile, options); |
| 26 | + this.always = this.hasOption(OPTION_ALWAYS) || (this.getOptions() && this.getOptions().length === 0); |
| 27 | + |
| 28 | + // this redundant variable is for readability below |
| 29 | + this.never = !this.always; |
| 30 | + } |
| 31 | + |
| 32 | + protected visitNode(node: ts.Node): void { |
| 33 | + if (node.kind === ts.SyntaxKind.ArrayBindingPattern) { |
| 34 | + this.validateSquareBrackets(node); |
| 35 | + } |
| 36 | + super.visitNode(node); |
| 37 | + } |
| 38 | + |
| 39 | + protected visitArrayLiteralExpression(node: ts.ArrayLiteralExpression): void { |
| 40 | + this.validateSquareBrackets(node); |
| 41 | + super.visitArrayLiteralExpression(node); |
| 42 | + } |
| 43 | + |
| 44 | + // note: this function assumes that the node will always have at |
| 45 | + // least two children: an opening bracket and a closing bracket |
| 46 | + private validateSquareBrackets(node: ts.Node): void { |
| 47 | + const children = node.getChildren(); |
| 48 | + const isEmptyArrayLiteral = !this.isSpaceBetween(node.getFirstToken(), node.getLastToken()); |
| 49 | + const isSpaceAfterOpeningBracket = this.isSpaceBetween(children[0], children[1]); |
| 50 | + const isBreakAfterOpeningBracket = this.isLineBreakBetween(children[0], children[1]); |
| 51 | + const isSpaceBeforeClosingBracket = this.isSpaceBetween(children[children.length - 2], children[children.length - 1]); |
| 52 | + const isBreakBeforeClosingBracket = this.isLineBreakBetween(children[children.length - 2], children[children.length - 1]); |
| 53 | + |
| 54 | + node.getChildren().forEach((child, index, parent) => { |
| 55 | + if (child.kind === ts.SyntaxKind.OpenBracketToken) { |
| 56 | + if (this.never && isSpaceAfterOpeningBracket && !isBreakAfterOpeningBracket) { |
| 57 | + this.addFailure(this.createFailure(child.getStart(), child.getWidth(), Rule.FAILURE_STRING.noBeginningSpace)); |
| 58 | + } |
| 59 | + |
| 60 | + if (this.always && !isSpaceAfterOpeningBracket && !isEmptyArrayLiteral) { |
| 61 | + this.addFailure(this.createFailure(child.getStart(), child.getWidth(), Rule.FAILURE_STRING.requiredBeginningSpace)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (child.kind === ts.SyntaxKind.CloseBracketToken) { |
| 66 | + if (this.never && isSpaceBeforeClosingBracket && !isBreakBeforeClosingBracket) { |
| 67 | + this.addFailure(this.createFailure(child.getStart(), child.getWidth(), Rule.FAILURE_STRING.noEndingSpace)); |
| 68 | + } |
| 69 | + |
| 70 | + if (this.always && !isSpaceBeforeClosingBracket && !isEmptyArrayLiteral) { |
| 71 | + this.addFailure(this.createFailure(child.getStart(), child.getWidth(), Rule.FAILURE_STRING.requiredEndingSpace)); |
| 72 | + } |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + // space/line break detection helpers |
| 78 | + private isSpaceBetween(node: ts.Node, nextNode: ts.Node): boolean { |
| 79 | + return nextNode.getStart() - node.getEnd() > 0; |
| 80 | + } |
| 81 | + |
| 82 | + private isLineBreakBetween(node: ts.Node, nextNode: ts.Node): boolean { |
| 83 | + return this.getEndPosition(node).line !== this.getStartPosition(nextNode).line; |
| 84 | + } |
| 85 | + |
| 86 | + private getStartPosition(node: ts.Node) { |
| 87 | + return node.getSourceFile().getLineAndCharacterOfPosition(node.getStart()); |
| 88 | + } |
| 89 | + |
| 90 | + private getEndPosition(node: ts.Node) { |
| 91 | + return node.getSourceFile().getLineAndCharacterOfPosition(node.getEnd()); |
| 92 | + } |
| 93 | +} |
0 commit comments