forked from vuejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.ts
217 lines (212 loc) · 5.44 KB
/
context.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import type * as CompilerDOM from '@vue/compiler-dom';
import type { Code, VueCodeInformation } from '../../types';
import { codeFeatures } from '../codeFeatures';
import { InlayHintInfo } from '../inlayHints';
import { endOfLine, newLine, wrapWith } from '../utils';
import type { TemplateCodegenOptions } from './index';
export type TemplateCodegenContext = ReturnType<typeof createTemplateCodegenContext>;
export function createTemplateCodegenContext(options: Pick<TemplateCodegenOptions, 'scriptSetupBindingNames' | 'edited'>) {
let ignoredError = false;
let expectErrorToken: {
errors: number;
node: CompilerDOM.CommentNode;
} | undefined;
let lastGenericComment: {
content: string;
offset: number;
} | undefined;
let variableId = 0;
function resolveCodeFeatures(features: VueCodeInformation) {
if (features.verification) {
if (ignoredError) {
return {
...features,
verification: false,
};
}
if (expectErrorToken) {
const token = expectErrorToken;
return {
...features,
verification: {
shouldReport: () => {
token.errors++;
return false;
},
},
};
}
}
return features;
}
const hoistVars = new Map<string, string>();
const localVars = new Map<string, number>();
const dollarVars = new Set<string>();
const accessExternalVariables = new Map<string, Set<number>>();
const slots: {
name: string;
offset?: number;
tagRange: [number, number];
nodeLoc: any;
propsVar: string;
}[] = [];
const dynamicSlots: {
expVar: string;
propsVar: string;
}[] = [];
const blockConditions: string[] = [];
const scopedClasses: {
source: string;
className: string;
offset: number;
}[] = [];
const emptyClassOffsets: number[] = [];
const inlayHints: InlayHintInfo[] = [];
const bindingAttrLocs: CompilerDOM.SourceLocation[] = [];
const inheritedAttrVars = new Set<string>();
const templateRefs = new Map<string, {
typeExp: string;
offset: number;
}>();
return {
codeFeatures: new Proxy(codeFeatures, {
get(target, key: keyof typeof codeFeatures) {
const data = target[key];
return resolveCodeFeatures(data);
},
}),
resolveCodeFeatures,
slots,
dynamicSlots,
dollarVars,
accessExternalVariables,
lastGenericComment,
blockConditions,
scopedClasses,
emptyClassOffsets,
inlayHints,
bindingAttrLocs,
inheritedAttrVars,
templateRefs,
currentComponent: undefined as {
ctxVar: string;
used: boolean;
} | undefined,
singleRootElTypes: [] as string[],
singleRootNodes: new Set<CompilerDOM.ElementNode | null>(),
accessExternalVariable(name: string, offset?: number) {
let arr = accessExternalVariables.get(name);
if (!arr) {
accessExternalVariables.set(name, arr = new Set());
}
if (offset !== undefined) {
arr.add(offset);
}
},
hasLocalVariable: (name: string) => {
return !!localVars.get(name);
},
addLocalVariable: (name: string) => {
localVars.set(name, (localVars.get(name) ?? 0) + 1);
},
removeLocalVariable: (name: string) => {
localVars.set(name, localVars.get(name)! - 1);
},
getInternalVariable: () => {
return `__VLS_${variableId++}`;
},
getHoistVariable: (originalVar: string) => {
let name = hoistVars.get(originalVar);
if (name === undefined) {
hoistVars.set(originalVar, name = `__VLS_${variableId++}`);
}
return name;
},
generateHoistVariables: function* () {
// trick to avoid TS 4081 (#5186)
if (hoistVars.size) {
yield `// @ts-ignore${newLine}`;
yield `var `;
for (const [originalVar, hoistVar] of hoistVars) {
yield `${hoistVar} = ${originalVar}, `;
}
yield endOfLine;
}
},
ignoreError: function* (): Generator<Code> {
if (!ignoredError) {
ignoredError = true;
yield `// @vue-ignore start${newLine}`;
}
},
expectError: function* (prevNode: CompilerDOM.CommentNode): Generator<Code> {
if (!expectErrorToken) {
expectErrorToken = {
errors: 0,
node: prevNode,
};
yield `// @vue-expect-error start${newLine}`;
}
},
resetDirectiveComments: function* (endStr: string): Generator<Code> {
if (expectErrorToken) {
const token = expectErrorToken;
yield* wrapWith(
expectErrorToken.node.loc.start.offset,
expectErrorToken.node.loc.end.offset,
{
verification: {
shouldReport: () => token.errors === 0,
},
},
`// @ts-expect-error __VLS_TS_EXPECT_ERROR`
);
yield `${newLine}${endOfLine}`;
expectErrorToken = undefined;
yield `// @vue-expect-error ${endStr}${newLine}`;
}
if (ignoredError) {
ignoredError = false;
yield `// @vue-ignore ${endStr}${newLine}`;
}
},
generateAutoImportCompletion: function* (): Generator<Code> {
if (!options.edited) {
return;
}
const all = [...accessExternalVariables.entries()];
if (!all.some(([_, offsets]) => offsets.size)) {
return;
}
yield `// @ts-ignore${newLine}`; // #2304
yield `[`;
for (const [varName, offsets] of all) {
for (const offset of offsets) {
if (options.scriptSetupBindingNames.has(varName)) {
// #3409
yield [
varName,
'template',
offset,
{
...codeFeatures.additionalCompletion,
...codeFeatures.withoutHighlightAndCompletionAndNavigation,
},
];
}
else {
yield [
varName,
'template',
offset,
codeFeatures.additionalCompletion,
];
}
yield `,`;
}
offsets.clear();
}
yield `]${endOfLine}`;
}
};
}