/
WebLinter.ts
138 lines (116 loc) · 3.87 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
import { createVirtualCompilerHost } from '@site/src/components/linter/CompilerHost';
import { parseSettings } from '@site/src/components/linter/config';
import type { ParserOptions } from '@typescript-eslint/types';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import type { LintUtils } from '@typescript-eslint/website-eslint';
import type {
CompilerHost,
CompilerOptions,
SourceFile,
System,
} from 'typescript';
const PARSER_NAME = '@typescript-eslint/parser';
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);
});
}
lint(code: string): TSESLint.Linter.LintMessage[] {
return this.linter.verify(code, {
parser: PARSER_NAME,
parserOptions: this.parserOptions,
rules: this.rules,
});
}
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 { estree: ast, astMaps } = this.lintUtils.astConverter(
tsAst,
{ ...parseSettings, code, jsx: isJsx },
true,
);
const scopeManager = this.lintUtils.analyze(ast, {
ecmaVersion:
eslintOptions.ecmaVersion === 'latest'
? 1e8
: eslintOptions.ecmaVersion,
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: {
hasFullTypeInformation: true,
program,
esTreeNodeToTSNodeMap: astMaps.esTreeNodeToTSNodeMap,
tsNodeToESTreeNodeMap: astMaps.tsNodeToESTreeNodeMap,
},
scopeManager,
visitorKeys: this.lintUtils.visitorKeys,
};
}
}