This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathobjectCurlySpacingRule.ts
115 lines (102 loc) · 4.25 KB
/
objectCurlySpacingRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import * as ts from 'typescript';
import * as Lint from 'tslint';
const OPTION_ALWAYS = 'always';
export class Rule extends Lint.Rules.AbstractRule {
public static FAILURE_STRING = {
always: {
start: `A space is required after '{'`,
end: `A space is required before '}'`
},
never: {
start: `There should be no space after '{'`,
end: `There should be no space before '}'`
}
};
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const walker = new ObjectCurlySpacingWalker(sourceFile, this.getOptions());
return this.applyWithWalker(walker);
}
}
class ObjectCurlySpacingWalker extends Lint.RuleWalker {
private always: boolean;
private exceptions: {
arraysInObjects: boolean;
objectsInObjects: boolean;
};
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
this.always = this.hasOption(OPTION_ALWAYS) || (this.getOptions() && this.getOptions().length === 0);
const opt = this.getOptions();
this.exceptions = opt[1] || {};
if (this.exceptions.arraysInObjects === undefined) {
this.exceptions.arraysInObjects = this.always;
}
if (this.exceptions.objectsInObjects === undefined) {
this.exceptions.objectsInObjects = this.always;
}
}
protected visitNode(node: ts.Node): void {
const bracedKind = [
ts.SyntaxKind.ObjectLiteralExpression,
ts.SyntaxKind.ObjectBindingPattern,
ts.SyntaxKind.NamedImports,
ts.SyntaxKind.NamedExports
];
if (bracedKind.indexOf(node.kind) > -1) {
this.checkSpacingInsideBraces(node);
}
super.visitNode(node);
}
private checkSpacingInsideBraces(node: ts.Node): void {
const text = node.getText();
if (text.indexOf('\n') !== -1 || /^\{\s*\}$/.test(text)) {
// Rule does not apply when the braces span multiple lines
return;
}
// Lookup whether the last value in the object is an object or array literal
let endsWithObjectLiteral = false;
let endsWithArrayLiteral = false;
if (node.getChildren().length === 3) {
const contents = node.getChildren()[1].getChildren();
if (contents.length > 0) {
const lastElement = contents[contents.length - 1];
if (lastElement.kind === ts.SyntaxKind.PropertyAssignment || lastElement.kind === ts.SyntaxKind.BindingElement) {
const value = lastElement.getChildren();
if (value.length === 3) {
endsWithObjectLiteral = value[2].kind === ts.SyntaxKind.ObjectLiteralExpression || value[2].kind === ts.SyntaxKind.ObjectBindingPattern;
endsWithArrayLiteral = value[2].kind === ts.SyntaxKind.ArrayLiteralExpression;
}
}
}
}
// We have matching braces, lets find out number of leading spaces
const leadingSpace = text.match(/^\{(\s{0,2})/)![1].length;
if (this.always) {
if (leadingSpace === 0) {
const fix = Lint.Replacement.appendText(node.getStart() + 1, ' ');
this.addFailure(this.createFailure(node.getStart(), 1, Rule.FAILURE_STRING.always.start, fix));
}
} else {
if (leadingSpace > 0) {
const fix = Lint.Replacement.deleteText(node.getStart() + 1, leadingSpace);
this.addFailure(this.createFailure(node.getStart(), 1, Rule.FAILURE_STRING.never.start, fix));
}
}
// Finding trailing spaces requires checking if exceptions apply, and adjusting accordingly
const trailingSpace = text.match(/(\s{0,2})}$/)![1].length;
const arrayExceptionApplies = this.always !== this.exceptions.arraysInObjects && endsWithArrayLiteral;
const objectExceptionApplies = this.always !== this.exceptions.objectsInObjects && endsWithObjectLiteral;
const spaceRequired = arrayExceptionApplies || objectExceptionApplies ? !this.always : this.always;
if (spaceRequired) {
if (trailingSpace === 0) {
const fix = Lint.Replacement.appendText(node.getEnd() - 1, ' ');
this.addFailure(this.createFailure(node.getEnd() - 1, 1, Rule.FAILURE_STRING.always.end, fix));
}
} else {
if (trailingSpace > 0) {
const fix = Lint.Replacement.deleteText(node.getEnd() - trailingSpace - 1, trailingSpace);
this.addFailure(this.createFailure(node.getEnd() - 1, 1, Rule.FAILURE_STRING.never.end, fix));
}
}
}
}