-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
WebLinter.ts
158 lines (133 loc) · 4.7 KB
/
WebLinter.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
151
152
153
154
155
156
157
158
import { createVirtualCompilerHost } from '@site/src/components/linter/CompilerHost';
import { parseSettings } from '@site/src/components/linter/config';
import type { analyze } from '@typescript-eslint/scope-manager';
import type { ParserOptions } from '@typescript-eslint/types';
import type { astConverter } from '@typescript-eslint/typescript-estree/use-at-your-own-risk/ast-converter';
import type { getScriptKind } from '@typescript-eslint/typescript-estree/use-at-your-own-risk/getScriptKind';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import type esquery from 'esquery';
import type {
CompilerHost,
CompilerOptions,
SourceFile,
System,
} from 'typescript';
const PARSER_NAME = '@typescript-eslint/parser';
export interface LintUtils {
createLinter: () => TSESLint.Linter;
analyze: typeof analyze;
visitorKeys: TSESLint.SourceCode.VisitorKeys;
astConverter: typeof astConverter;
getScriptKind: typeof getScriptKind;
esquery: typeof esquery;
}
export class WebLinter {
private readonly host: CompilerHost;
public storedAST?: TSESTree.Program;
public storedTsAST?: SourceFile;
public storedScope?: Record<string, unknown>;
private compilerOptions: CompilerOptions;
private readonly parserOptions: ParserOptions = {
ecmaFeatures: {
jsx: false,
globalReturn: false,
},
ecmaVersion: 'latest',
project: ['./tsconfig.json'],
sourceType: 'module',
};
private linter: TSESLint.Linter;
private lintUtils: LintUtils;
private rules: TSESLint.Linter.RulesRecord = {};
public readonly ruleNames: { name: string; description?: string }[] = [];
public readonly rulesUrl = new Map<string, string | undefined>();
constructor(
system: System,
compilerOptions: CompilerOptions,
lintUtils: LintUtils,
) {
this.compilerOptions = compilerOptions;
this.lintUtils = lintUtils;
this.linter = lintUtils.createLinter();
this.host = createVirtualCompilerHost(system, lintUtils);
this.linter.defineParser(PARSER_NAME, {
parseForESLint: (text, options?: ParserOptions) => {
return this.eslintParse(text, options);
},
});
this.linter.getRules().forEach((item, name) => {
this.ruleNames.push({
name: name,
description: item.meta?.docs?.description,
});
this.rulesUrl.set(name, item.meta?.docs?.url);
});
}
get eslintConfig(): TSESLint.Linter.Config {
return {
parser: PARSER_NAME,
parserOptions: this.parserOptions,
rules: this.rules,
};
}
lint(code: string): TSESLint.Linter.LintMessage[] {
return this.linter.verify(code, this.eslintConfig);
}
fix(code: string): TSESLint.Linter.FixReport {
return this.linter.verifyAndFix(code, this.eslintConfig, { fix: true });
}
updateRules(rules: TSESLint.Linter.RulesRecord): void {
this.rules = rules;
}
updateParserOptions(jsx?: boolean, sourceType?: TSESLint.SourceType): void {
this.parserOptions.ecmaFeatures!.jsx = jsx ?? false;
this.parserOptions.sourceType = sourceType ?? 'module';
}
updateCompilerOptions(options: CompilerOptions = {}): void {
this.compilerOptions = options;
}
eslintParse(
code: string,
eslintOptions: ParserOptions = {},
): TSESLint.Linter.ESLintParseResult {
const isJsx = eslintOptions?.ecmaFeatures?.jsx ?? false;
const fileName = isJsx ? '/demo.tsx' : '/demo.ts';
this.storedAST = undefined;
this.storedTsAST = undefined;
this.storedScope = undefined;
this.host.writeFile(fileName, code, false);
const program = window.ts.createProgram({
rootNames: [fileName],
options: this.compilerOptions,
host: this.host,
});
const tsAst = program.getSourceFile(fileName)!;
const checker = program.getTypeChecker();
const { estree: ast, astMaps } = this.lintUtils.astConverter(
tsAst,
{ ...parseSettings, code, codeFullText: code, jsx: isJsx },
true,
);
const scopeManager = this.lintUtils.analyze(ast, {
globalReturn: eslintOptions.ecmaFeatures?.globalReturn ?? false,
sourceType: eslintOptions.sourceType ?? 'script',
});
this.storedAST = ast;
this.storedTsAST = tsAst;
this.storedScope = scopeManager as unknown as Record<string, unknown>;
return {
ast,
services: {
program,
esTreeNodeToTSNodeMap: astMaps.esTreeNodeToTSNodeMap,
tsNodeToESTreeNodeMap: astMaps.tsNodeToESTreeNodeMap,
getSymbolAtLocation: node =>
checker.getSymbolAtLocation(astMaps.esTreeNodeToTSNodeMap.get(node)),
getTypeAtLocation: node =>
checker.getTypeAtLocation(astMaps.esTreeNodeToTSNodeMap.get(node)),
},
scopeManager,
visitorKeys: this.lintUtils.visitorKeys,
};
}
}