-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
prettierRule.ts
150 lines (133 loc) · 3.8 KB
/
prettierRule.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import * as utils from 'eslint-plugin-prettier';
import LineAndColumn from 'lines-and-columns';
import * as path from 'path';
import * as prettier from 'prettier';
import * as tslint from 'tslint';
import * as ts from 'typescript';
export class Rule extends tslint.Rules.AbstractRule {
public apply(sourceFile: ts.SourceFile): tslint.RuleFailure[] {
return this.applyWithWalker(
new Walker(sourceFile, this.ruleName, this.ruleArguments),
);
}
}
class Walker extends tslint.AbstractWalker<any[]> {
public walk(sourceFile: ts.SourceFile) {
const [ruleArgument1, ruleArgument2 = {}] = this.options;
const { editorconfig = true } = ruleArgument2;
let options: prettier.Options = {};
switch (typeof ruleArgument1) {
case 'string': {
const configFilePath = path.resolve(
process.cwd(),
ruleArgument1 as string,
);
const resolvedConfig = prettier.resolveConfig.sync(
sourceFile.fileName,
{ config: configFilePath, editorconfig },
);
// istanbul ignore next
if (resolvedConfig === null) {
throw new Error(`Config file not found: ${configFilePath}`);
}
options = resolvedConfig;
break;
}
case 'object':
if (ruleArgument1) {
options = ruleArgument1 as prettier.Options;
break;
}
// falls through for null
default: {
const resolvedConfig = prettier.resolveConfig.sync(
sourceFile.fileName,
{ editorconfig },
);
if (resolvedConfig !== null) {
options = resolvedConfig;
}
break;
}
}
const source = sourceFile.getFullText();
try {
const formatted = prettier.format(source, {
parser: 'typescript',
...options,
});
if (source === formatted) {
return;
}
reportDifferences(this, source, formatted);
} catch (e) {
// istanbul ignore else
if (e.loc) {
reportSyntaxError(this, source, e);
} else {
throw e;
}
}
}
}
function reportSyntaxError(
walkContext: tslint.WalkContext<any>,
source: string,
error: { message: string; loc: { start: { line: number; column: number } } },
) {
const locator = new LineAndColumn(source);
const offset = locator.indexForLocation({
column: error.loc.start.column - 1,
line: error.loc.start.line - 1,
})!;
const message = error.message
.split('\n')[0]
.replace(/\s*\(\d+:\d+\)\s*$/, '');
walkContext.addFailureAt(offset, 1, `SyntaxError: ${message}`);
}
function reportDifferences(
walkContext: tslint.WalkContext<any>,
source: string,
formatted: string,
) {
utils.generateDifferences(source, formatted).forEach(difference => {
const {
operation,
offset: start,
deleteText = '',
insertText = '',
} = difference;
const end = start + deleteText.length;
const deleteCode = utils.showInvisibles(deleteText);
const insertCode = utils.showInvisibles(insertText);
switch (operation) {
case 'insert':
walkContext.addFailureAt(
start,
1,
`Insert \`${insertCode}\``,
tslint.Replacement.appendText(start, insertText),
);
break;
case 'delete':
walkContext.addFailure(
start,
end,
`Delete \`${deleteCode}\``,
tslint.Replacement.deleteFromTo(start, end),
);
break;
case 'replace':
walkContext.addFailure(
start,
end,
`Replace \`${deleteCode}\` with \`${insertCode}\``,
tslint.Replacement.replaceFromTo(start, end, insertText),
);
break;
// istanbul ignore next
default:
throw new Error(`Unexpected operation '${operation}'`);
}
});
}