/
scriptRanges.ts
63 lines (56 loc) · 2.08 KB
/
scriptRanges.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
import { getStartEnd, parseBindingRanges } from './scriptSetupRanges';
import type { TextRange } from '../types';
import { parseScriptImportRanges } from './scriptImportRanges';
export interface ScriptRanges extends ReturnType<typeof parseScriptRanges> { }
export function parseScriptRanges(ts: typeof import('typescript/lib/tsserverlibrary'), ast: ts.SourceFile, hasScriptSetup: boolean, withComponentOption: boolean, withNode: boolean) {
let exportDefault: (TextRange & {
expression: TextRange,
args: TextRange,
argsNode: ts.ObjectLiteralExpression | undefined,
componentsOption: TextRange | undefined,
componentsOptionNode: ts.ObjectLiteralExpression | undefined,
}) | undefined;
const bindings = hasScriptSetup ? parseBindingRanges(ts, ast, false) : [];
ast.forEachChild(node => {
if (ts.isExportAssignment(node)) {
let obj: ts.ObjectLiteralExpression | undefined;
if (ts.isObjectLiteralExpression(node.expression)) {
obj = node.expression;
}
else if (ts.isCallExpression(node.expression) && node.expression.arguments.length) {
const arg0 = node.expression.arguments[0];
if (ts.isObjectLiteralExpression(arg0)) {
obj = arg0;
}
}
if (obj) {
let componentsOptionNode: ts.ObjectLiteralExpression | undefined;
if (withComponentOption) {
obj.forEachChild(node => {
if (ts.isPropertyAssignment(node) && ts.isIdentifier(node.name)) {
if (node.name.escapedText === 'components' && ts.isObjectLiteralExpression(node.initializer)) {
componentsOptionNode = node.initializer;
}
}
});
}
exportDefault = {
..._getStartEnd(node),
expression: _getStartEnd(node.expression),
args: _getStartEnd(obj),
argsNode: withNode ? obj : undefined,
componentsOption: componentsOptionNode ? _getStartEnd(componentsOptionNode) : undefined,
componentsOptionNode: withNode ? componentsOptionNode : undefined,
};
}
}
});
return {
imports: parseScriptImportRanges(ts, ast, ast.getEnd()),
exportDefault,
bindings,
};
function _getStartEnd(node: ts.Node) {
return getStartEnd(node, ast);
}
}