typescript-svelte-plugin 0.3.14 → 0.3.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/index.js +1 -1
- package/dist/src/language-service/call-hierarchy.d.ts +3 -0
- package/dist/src/language-service/call-hierarchy.js +232 -0
- package/dist/src/language-service/completions.d.ts +1 -1
- package/dist/src/language-service/completions.js +18 -0
- package/dist/src/language-service/index.d.ts +1 -1
- package/dist/src/language-service/index.js +5 -3
- package/dist/src/svelte-snapshots.d.ts +3 -0
- package/dist/src/svelte-snapshots.js +32 -0
- package/dist/src/utils.d.ts +14 -0
- package/dist/src/utils.js +52 -1
- package/package.json +1 -1
package/dist/src/index.js
CHANGED
|
@@ -50,7 +50,7 @@ function init(modules) {
|
|
|
50
50
|
info.project.updateGraph();
|
|
51
51
|
}
|
|
52
52
|
});
|
|
53
|
-
return decorateLanguageServiceDispose((0, language_service_1.decorateLanguageService)(info.languageService, snapshotManager, logger, configManager), projectSvelteFilesManager !== null && projectSvelteFilesManager !== void 0 ? projectSvelteFilesManager : {
|
|
53
|
+
return decorateLanguageServiceDispose((0, language_service_1.decorateLanguageService)(info.languageService, snapshotManager, logger, configManager, modules.typescript), projectSvelteFilesManager !== null && projectSvelteFilesManager !== void 0 ? projectSvelteFilesManager : {
|
|
54
54
|
dispose() { }
|
|
55
55
|
});
|
|
56
56
|
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decorateCallHierarchy = void 0;
|
|
4
|
+
const utils_1 = require("../utils");
|
|
5
|
+
const ENSURE_COMPONENT_HELPER = '__sveltets_2_ensureComponent';
|
|
6
|
+
function decorateCallHierarchy(ls, snapshotManager, typescript) {
|
|
7
|
+
// don't need to patch prepare. It's always a ts/js file
|
|
8
|
+
// const prepareCallHierarchy = ls.prepareCallHierarchy;
|
|
9
|
+
const provideCallHierarchyIncomingCalls = ls.provideCallHierarchyIncomingCalls;
|
|
10
|
+
const provideCallHierarchyOutgoingCalls = ls.provideCallHierarchyOutgoingCalls;
|
|
11
|
+
ls.provideCallHierarchyIncomingCalls = (fileName, position) => {
|
|
12
|
+
var _a;
|
|
13
|
+
const program = ls.getProgram();
|
|
14
|
+
// probably won't happen
|
|
15
|
+
if (!program) {
|
|
16
|
+
return provideCallHierarchyIncomingCalls(fileName, position);
|
|
17
|
+
}
|
|
18
|
+
const snapshot = snapshotManager.get(fileName);
|
|
19
|
+
const componentExportOffset = isComponentModulePosition(fileName, position) && snapshot
|
|
20
|
+
? (0, utils_1.offsetOfGeneratedComponentExport)(snapshot)
|
|
21
|
+
: -1;
|
|
22
|
+
const redirectedPosition = componentExportOffset >= 0 ? componentExportOffset : position;
|
|
23
|
+
const tsResult = provideCallHierarchyIncomingCalls(fileName, redirectedPosition);
|
|
24
|
+
return tsResult
|
|
25
|
+
.map((item) => {
|
|
26
|
+
if (!(0, utils_1.isSvelteFilePath)(item.from.file)) {
|
|
27
|
+
return item;
|
|
28
|
+
}
|
|
29
|
+
const snapshot = snapshotManager.get(item.from.file);
|
|
30
|
+
const from = convertSvelteCallHierarchyItem(item.from, program);
|
|
31
|
+
if (!from || !snapshot) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
const fromSpans = item.fromSpans
|
|
35
|
+
.map((span) => snapshot.getOriginalTextSpan(span))
|
|
36
|
+
.filter(utils_1.isNotNullOrUndefined);
|
|
37
|
+
return {
|
|
38
|
+
from,
|
|
39
|
+
fromSpans: fromSpans
|
|
40
|
+
};
|
|
41
|
+
})
|
|
42
|
+
.concat((_a = getInComingCallsForComponent(ls, program, fileName, redirectedPosition)) !== null && _a !== void 0 ? _a : [])
|
|
43
|
+
.filter(utils_1.isNotNullOrUndefined);
|
|
44
|
+
};
|
|
45
|
+
ls.provideCallHierarchyOutgoingCalls = (fileName, position) => {
|
|
46
|
+
var _a, _b, _c;
|
|
47
|
+
const program = ls.getProgram();
|
|
48
|
+
// probably won't happen
|
|
49
|
+
if (!program) {
|
|
50
|
+
return provideCallHierarchyOutgoingCalls(fileName, position);
|
|
51
|
+
}
|
|
52
|
+
const sourceFile = program === null || program === void 0 ? void 0 : program.getSourceFile(fileName);
|
|
53
|
+
const renderFunctionOffset = isComponentModulePosition(fileName, position) && sourceFile
|
|
54
|
+
? (_b = (_a = sourceFile.statements
|
|
55
|
+
.find((statement) => {
|
|
56
|
+
var _a;
|
|
57
|
+
return typescript.isFunctionDeclaration(statement) &&
|
|
58
|
+
((_a = statement.name) === null || _a === void 0 ? void 0 : _a.getText()) === 'render';
|
|
59
|
+
})) === null || _a === void 0 ? void 0 : _a.name) === null || _b === void 0 ? void 0 : _b.getStart()
|
|
60
|
+
: -1;
|
|
61
|
+
const offset = renderFunctionOffset != null && renderFunctionOffset >= 0
|
|
62
|
+
? renderFunctionOffset
|
|
63
|
+
: position;
|
|
64
|
+
const snapshot = snapshotManager.get(fileName);
|
|
65
|
+
return provideCallHierarchyOutgoingCalls(fileName, offset)
|
|
66
|
+
.concat(program && sourceFile && isComponentModulePosition(fileName, position)
|
|
67
|
+
? (_c = getOutgoingCallsForComponent(program, sourceFile)) !== null && _c !== void 0 ? _c : []
|
|
68
|
+
: [])
|
|
69
|
+
.map((item) => {
|
|
70
|
+
const to = convertSvelteCallHierarchyItem(item.to, program);
|
|
71
|
+
if (!to ||
|
|
72
|
+
item.to.name.startsWith('__sveltets') ||
|
|
73
|
+
item.to.containerName === 'svelteHTML') {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const fromSpans = snapshot
|
|
77
|
+
? item.fromSpans
|
|
78
|
+
.map((span) => snapshot.getOriginalTextSpan(span))
|
|
79
|
+
.filter(utils_1.isNotNullOrUndefined)
|
|
80
|
+
: item.fromSpans;
|
|
81
|
+
if (!fromSpans.length) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
to,
|
|
86
|
+
fromSpans
|
|
87
|
+
};
|
|
88
|
+
})
|
|
89
|
+
.filter(utils_1.isNotNullOrUndefined);
|
|
90
|
+
};
|
|
91
|
+
function isComponentModulePosition(fileName, position) {
|
|
92
|
+
return (0, utils_1.isSvelteFilePath)(fileName) && position === 0;
|
|
93
|
+
}
|
|
94
|
+
function convertSvelteCallHierarchyItem(item, program) {
|
|
95
|
+
if (!(0, utils_1.isSvelteFilePath)(item.file)) {
|
|
96
|
+
return item;
|
|
97
|
+
}
|
|
98
|
+
const snapshot = snapshotManager.get(item.file);
|
|
99
|
+
if (!snapshot) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const redirectedCallHierarchyItem = redirectSvelteCallHierarchyItem(snapshot, program, item);
|
|
103
|
+
if (redirectedCallHierarchyItem) {
|
|
104
|
+
return redirectedCallHierarchyItem;
|
|
105
|
+
}
|
|
106
|
+
const selectionSpan = snapshot.getOriginalTextSpan(item.selectionSpan);
|
|
107
|
+
if (!selectionSpan) {
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
const span = snapshot.getOriginalTextSpan(item.span);
|
|
111
|
+
if (!span) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
...item,
|
|
116
|
+
span,
|
|
117
|
+
selectionSpan
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function redirectSvelteCallHierarchyItem(snapshot, program, item) {
|
|
121
|
+
const sourceFile = program.getSourceFile(item.file);
|
|
122
|
+
if (!sourceFile) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
if ((0, utils_1.isGeneratedSvelteComponentName)(item.name)) {
|
|
126
|
+
return toComponentCallHierarchyItem(snapshot, item.file);
|
|
127
|
+
}
|
|
128
|
+
if (item.name === 'render') {
|
|
129
|
+
const end = item.selectionSpan.start + item.selectionSpan.length;
|
|
130
|
+
const renderFunction = sourceFile.statements.find((statement) => statement.getStart() <= item.selectionSpan.start && statement.getEnd() >= end);
|
|
131
|
+
if (!renderFunction || !sourceFile.statements.includes(renderFunction)) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
return toComponentCallHierarchyItem(snapshot, item.file);
|
|
135
|
+
}
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
function toComponentCallHierarchyItem(snapshot, file) {
|
|
139
|
+
const fileSpan = { start: 0, length: snapshot.getOriginalText().length };
|
|
140
|
+
return {
|
|
141
|
+
kind: typescript.ScriptElementKind.moduleElement,
|
|
142
|
+
file: file,
|
|
143
|
+
name: '',
|
|
144
|
+
selectionSpan: { start: 0, length: 0 },
|
|
145
|
+
span: fileSpan
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function getInComingCallsForComponent(ls, program, fileName, position) {
|
|
149
|
+
var _a, _b;
|
|
150
|
+
if (!(0, utils_1.isSvelteFilePath)(fileName)) {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
return ((_b = (_a = ls
|
|
154
|
+
.findReferences(fileName, position)) === null || _a === void 0 ? void 0 : _a.map((ref) => componentRefToIncomingCall(ref, program)).filter(utils_1.isNotNullOrUndefined)) !== null && _b !== void 0 ? _b : null);
|
|
155
|
+
}
|
|
156
|
+
function componentRefToIncomingCall(ref, program) {
|
|
157
|
+
const snapshot = (0, utils_1.isSvelteFilePath)(ref.definition.fileName) &&
|
|
158
|
+
snapshotManager.get(ref.definition.fileName);
|
|
159
|
+
const sourceFile = program.getSourceFile(ref.definition.fileName);
|
|
160
|
+
if (!snapshot || !sourceFile) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
const startTags = ref.references
|
|
164
|
+
.map((ref) => {
|
|
165
|
+
const generatedTextSpan = snapshot.getGeneratedTextSpan(ref.textSpan);
|
|
166
|
+
const node = generatedTextSpan &&
|
|
167
|
+
(0, utils_1.findNodeAtSpan)(sourceFile, generatedTextSpan, isComponentStartTag);
|
|
168
|
+
if (node) {
|
|
169
|
+
return ref;
|
|
170
|
+
}
|
|
171
|
+
return null;
|
|
172
|
+
})
|
|
173
|
+
.filter(utils_1.isNotNullOrUndefined);
|
|
174
|
+
if (!startTags.length) {
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
from: toComponentCallHierarchyItem(snapshot, ref.definition.fileName),
|
|
179
|
+
fromSpans: startTags.map((tag) => tag.textSpan)
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function isComponentStartTag(node) {
|
|
183
|
+
return (!!node &&
|
|
184
|
+
node.parent &&
|
|
185
|
+
typescript.isCallExpression(node.parent) &&
|
|
186
|
+
typescript.isIdentifier(node.parent.expression) &&
|
|
187
|
+
node.parent.expression.text === ENSURE_COMPONENT_HELPER &&
|
|
188
|
+
typescript.isIdentifier(node) &&
|
|
189
|
+
node === node.parent.arguments[0]);
|
|
190
|
+
}
|
|
191
|
+
function getOutgoingCallsForComponent(program, sourceFile) {
|
|
192
|
+
var _a, _b, _c, _d;
|
|
193
|
+
const groups = new Map();
|
|
194
|
+
const startTags = (0, utils_1.gatherDescendants)(sourceFile, isComponentStartTag);
|
|
195
|
+
const typeChecker = program.getTypeChecker();
|
|
196
|
+
for (const startTag of startTags) {
|
|
197
|
+
const type = typeChecker.getTypeAtLocation(startTag);
|
|
198
|
+
const symbol = (_a = type.aliasSymbol) !== null && _a !== void 0 ? _a : type.symbol;
|
|
199
|
+
const declaration = (_b = symbol === null || symbol === void 0 ? void 0 : symbol.valueDeclaration) !== null && _b !== void 0 ? _b : (_c = symbol === null || symbol === void 0 ? void 0 : symbol.declarations) === null || _c === void 0 ? void 0 : _c[0];
|
|
200
|
+
if (!declaration || !typescript.isClassDeclaration(declaration)) {
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
let group = groups.get(declaration);
|
|
204
|
+
if (!group) {
|
|
205
|
+
group = [];
|
|
206
|
+
groups.set(declaration, group);
|
|
207
|
+
}
|
|
208
|
+
group.push({ start: startTag.getStart(), length: startTag.getWidth() });
|
|
209
|
+
}
|
|
210
|
+
return ((_d = Array.from(groups).map(([declaration, group]) => {
|
|
211
|
+
var _a, _b;
|
|
212
|
+
const file = declaration.getSourceFile().fileName;
|
|
213
|
+
const name = (_b = (_a = declaration.name) === null || _a === void 0 ? void 0 : _a.getText()) !== null && _b !== void 0 ? _b : file.slice(file.lastIndexOf('.'));
|
|
214
|
+
const span = { start: declaration.getStart(), length: declaration.getWidth() };
|
|
215
|
+
const selectionSpan = declaration.name
|
|
216
|
+
? { start: declaration.name.getStart(), length: declaration.name.getWidth() }
|
|
217
|
+
: span;
|
|
218
|
+
return {
|
|
219
|
+
to: {
|
|
220
|
+
file,
|
|
221
|
+
kind: typescript.ScriptElementKind.classElement,
|
|
222
|
+
name,
|
|
223
|
+
selectionSpan,
|
|
224
|
+
span
|
|
225
|
+
},
|
|
226
|
+
fromSpans: group
|
|
227
|
+
};
|
|
228
|
+
})) !== null && _d !== void 0 ? _d : null);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
exports.decorateCallHierarchy = decorateCallHierarchy;
|
|
232
|
+
//# sourceMappingURL=call-hierarchy.js.map
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.decorateCompletions = void 0;
|
|
4
7
|
const path_1 = require("path");
|
|
8
|
+
const tsserverlibrary_1 = __importDefault(require("typescript/lib/tsserverlibrary"));
|
|
5
9
|
const utils_1 = require("../utils");
|
|
6
10
|
const componentPostfix = '__SvelteComponent_';
|
|
7
11
|
function decorateCompletions(ls, logger) {
|
|
@@ -47,6 +51,20 @@ function decorateCompletions(ls, logger) {
|
|
|
47
51
|
}
|
|
48
52
|
}
|
|
49
53
|
}
|
|
54
|
+
if ((0, path_1.basename)(fileName).startsWith('+page') || (0, path_1.basename)(fileName).startsWith('+layout')) {
|
|
55
|
+
if ((0, path_1.extname)(fileName) === '.js') {
|
|
56
|
+
completions.entries.push({
|
|
57
|
+
// TODO doesn't show up for "export" and "load" only at the bottom for "function"
|
|
58
|
+
kind: tsserverlibrary_1.default.ScriptElementKind.functionElement,
|
|
59
|
+
name: 'export function load() {}',
|
|
60
|
+
sortText: '-99',
|
|
61
|
+
kindModifiers: tsserverlibrary_1.default.ScriptElementKindModifier.exportedModifier,
|
|
62
|
+
insertText: `/** @type {import('./$types').Foo} */\nexport function load() {}`
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
}
|
|
67
|
+
}
|
|
50
68
|
return {
|
|
51
69
|
...completions,
|
|
52
70
|
entries: completions.entries.map((entry) => {
|
|
@@ -3,4 +3,4 @@ import { ConfigManager } from '../config-manager';
|
|
|
3
3
|
import { Logger } from '../logger';
|
|
4
4
|
import { SvelteSnapshotManager } from '../svelte-snapshots';
|
|
5
5
|
export declare function isPatched(ls: ts.LanguageService): boolean;
|
|
6
|
-
export declare function decorateLanguageService(ls: ts.LanguageService, snapshotManager: SvelteSnapshotManager, logger: Logger, configManager: ConfigManager): ts.LanguageService;
|
|
6
|
+
export declare function decorateLanguageService(ls: ts.LanguageService, snapshotManager: SvelteSnapshotManager, logger: Logger, configManager: ConfigManager, typescript: typeof ts): ts.LanguageService;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.decorateLanguageService = exports.isPatched = void 0;
|
|
4
4
|
const utils_1 = require("../utils");
|
|
5
|
+
const call_hierarchy_1 = require("./call-hierarchy");
|
|
5
6
|
const completions_1 = require("./completions");
|
|
6
7
|
const definition_1 = require("./definition");
|
|
7
8
|
const diagnostics_1 = require("./diagnostics");
|
|
@@ -14,15 +15,15 @@ function isPatched(ls) {
|
|
|
14
15
|
return ls[sveltePluginPatchSymbol] === true;
|
|
15
16
|
}
|
|
16
17
|
exports.isPatched = isPatched;
|
|
17
|
-
function decorateLanguageService(ls, snapshotManager, logger, configManager) {
|
|
18
|
+
function decorateLanguageService(ls, snapshotManager, logger, configManager, typescript) {
|
|
18
19
|
// Decorate using a proxy so we can dynamically enable/disable method
|
|
19
20
|
// patches depending on the enabled state of our config
|
|
20
21
|
const proxy = new Proxy(ls, createProxyHandler(configManager));
|
|
21
|
-
decorateLanguageServiceInner(proxy, snapshotManager, logger);
|
|
22
|
+
decorateLanguageServiceInner(proxy, snapshotManager, logger, typescript);
|
|
22
23
|
return proxy;
|
|
23
24
|
}
|
|
24
25
|
exports.decorateLanguageService = decorateLanguageService;
|
|
25
|
-
function decorateLanguageServiceInner(ls, snapshotManager, logger) {
|
|
26
|
+
function decorateLanguageServiceInner(ls, snapshotManager, logger, typescript) {
|
|
26
27
|
patchLineColumnOffset(ls, snapshotManager);
|
|
27
28
|
(0, rename_1.decorateRename)(ls, snapshotManager, logger);
|
|
28
29
|
(0, diagnostics_1.decorateDiagnostics)(ls, logger);
|
|
@@ -31,6 +32,7 @@ function decorateLanguageServiceInner(ls, snapshotManager, logger) {
|
|
|
31
32
|
(0, definition_1.decorateGetDefinition)(ls, snapshotManager, logger);
|
|
32
33
|
(0, implementation_1.decorateGetImplementation)(ls, snapshotManager, logger);
|
|
33
34
|
(0, update_imports_1.decorateUpdateImports)(ls, snapshotManager, logger);
|
|
35
|
+
(0, call_hierarchy_1.decorateCallHierarchy)(ls, snapshotManager, typescript);
|
|
34
36
|
return ls;
|
|
35
37
|
}
|
|
36
38
|
function createProxyHandler(configManager) {
|
|
@@ -16,6 +16,8 @@ export declare class SvelteSnapshot {
|
|
|
16
16
|
update(svelteCode: string, mapper: SourceMapper): void;
|
|
17
17
|
getOriginalTextSpan(textSpan: ts.TextSpan): ts.TextSpan | null;
|
|
18
18
|
getOriginalOffset(generatedOffset: number): number;
|
|
19
|
+
getGeneratedTextSpan(textSpan: ts.TextSpan): ts.TextSpan | null;
|
|
20
|
+
getGeneratedOffset(originalOffset: number): number;
|
|
19
21
|
setAndPatchScriptInfo(scriptInfo: ts.server.ScriptInfo): void;
|
|
20
22
|
/**
|
|
21
23
|
* Get the line and character based on the offset
|
|
@@ -33,6 +35,7 @@ export declare class SvelteSnapshot {
|
|
|
33
35
|
private debug;
|
|
34
36
|
private toggleMappingMode;
|
|
35
37
|
getText(): string;
|
|
38
|
+
getOriginalText(): string;
|
|
36
39
|
}
|
|
37
40
|
export declare class SvelteSnapshotManager {
|
|
38
41
|
private typescript;
|
|
@@ -53,6 +53,35 @@ class SvelteSnapshot {
|
|
|
53
53
|
this.debug('converted offset to', original, '/', originalOffset);
|
|
54
54
|
return originalOffset;
|
|
55
55
|
}
|
|
56
|
+
getGeneratedTextSpan(textSpan) {
|
|
57
|
+
const start = this.getGeneratedOffset(textSpan.start);
|
|
58
|
+
if (start === -1) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
// Assumption: We don't change identifiers itself, so we don't change ranges.
|
|
62
|
+
return {
|
|
63
|
+
start,
|
|
64
|
+
length: textSpan.length
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
getGeneratedOffset(originalOffset) {
|
|
68
|
+
if (!this.scriptInfo) {
|
|
69
|
+
return originalOffset;
|
|
70
|
+
}
|
|
71
|
+
const lineOffset = this.scriptInfo.positionToLineOffset(originalOffset);
|
|
72
|
+
const original = this.mapper.getGeneratedPosition({
|
|
73
|
+
line: lineOffset.line - 1,
|
|
74
|
+
character: lineOffset.offset - 1
|
|
75
|
+
});
|
|
76
|
+
if (original.line === -1) {
|
|
77
|
+
return -1;
|
|
78
|
+
}
|
|
79
|
+
this.toggleMappingMode(true);
|
|
80
|
+
const generatedOffset = this.scriptInfo.lineOffsetToPosition(original.line + 1, original.character + 1);
|
|
81
|
+
this.toggleMappingMode(false);
|
|
82
|
+
this.debug('converted offset to', original, '/', generatedOffset);
|
|
83
|
+
return generatedOffset;
|
|
84
|
+
}
|
|
56
85
|
setAndPatchScriptInfo(scriptInfo) {
|
|
57
86
|
// @ts-expect-error
|
|
58
87
|
scriptInfo.scriptKind = this.typescript.ScriptKind.TSX;
|
|
@@ -186,6 +215,9 @@ class SvelteSnapshot {
|
|
|
186
215
|
}
|
|
187
216
|
return snapshot.getText(0, snapshot.getLength());
|
|
188
217
|
}
|
|
218
|
+
getOriginalText() {
|
|
219
|
+
return this.svelteCode;
|
|
220
|
+
}
|
|
189
221
|
}
|
|
190
222
|
exports.SvelteSnapshot = SvelteSnapshot;
|
|
191
223
|
class SvelteSnapshotManager {
|
package/dist/src/utils.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type ts from 'typescript/lib/tsserverlibrary';
|
|
2
|
+
import { SvelteSnapshot } from './svelte-snapshots';
|
|
2
3
|
export declare function isSvelteFilePath(filePath: string): boolean;
|
|
3
4
|
export declare function isVirtualSvelteFilePath(filePath: string): boolean;
|
|
4
5
|
export declare function toRealSvelteFilePath(filePath: string): string;
|
|
@@ -21,3 +22,16 @@ export declare function replaceDeep<T extends Record<string, any>>(obj: T, searc
|
|
|
21
22
|
export declare function getConfigPathForProject(project: ts.server.Project): ts.server.NormalizedPath;
|
|
22
23
|
export declare function isStoreVariableIn$storeDeclaration(text: string, varStart: number): boolean;
|
|
23
24
|
export declare function get$storeOffsetOf$storeDeclaration(text: string, storePosition: number): number;
|
|
25
|
+
type NodePredicate = (node: ts.Node) => boolean;
|
|
26
|
+
type NodeTypePredicate<T extends ts.Node> = (node: ts.Node) => node is T;
|
|
27
|
+
/**
|
|
28
|
+
* Finds node exactly matching span {start, length}.
|
|
29
|
+
*/
|
|
30
|
+
export declare function findNodeAtSpan<T extends ts.Node>(node: ts.Node, span: {
|
|
31
|
+
start: number;
|
|
32
|
+
length: number;
|
|
33
|
+
}, predicate?: NodeTypePredicate<T>): T | void;
|
|
34
|
+
export declare function isGeneratedSvelteComponentName(className: string): boolean;
|
|
35
|
+
export declare function offsetOfGeneratedComponentExport(snapshot: SvelteSnapshot): number;
|
|
36
|
+
export declare function gatherDescendants<T extends ts.Node>(node: ts.Node, predicate: NodePredicate | NodeTypePredicate<T>, dest?: T[]): T[];
|
|
37
|
+
export {};
|
package/dist/src/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.get$storeOffsetOf$storeDeclaration = exports.isStoreVariableIn$storeDeclaration = exports.getConfigPathForProject = exports.replaceDeep = exports.isNoTextSpanInGeneratedCode = exports.isInGeneratedCode = exports.isNotNullOrUndefined = exports.ensureRealSvelteFilePath = exports.toRealSvelteFilePath = exports.isVirtualSvelteFilePath = exports.isSvelteFilePath = void 0;
|
|
3
|
+
exports.gatherDescendants = exports.offsetOfGeneratedComponentExport = exports.isGeneratedSvelteComponentName = exports.findNodeAtSpan = exports.get$storeOffsetOf$storeDeclaration = exports.isStoreVariableIn$storeDeclaration = exports.getConfigPathForProject = exports.replaceDeep = exports.isNoTextSpanInGeneratedCode = exports.isInGeneratedCode = exports.isNotNullOrUndefined = exports.ensureRealSvelteFilePath = exports.toRealSvelteFilePath = exports.isVirtualSvelteFilePath = exports.isSvelteFilePath = void 0;
|
|
4
4
|
function isSvelteFilePath(filePath) {
|
|
5
5
|
return filePath.endsWith('.svelte');
|
|
6
6
|
}
|
|
@@ -77,4 +77,55 @@ function get$storeOffsetOf$storeDeclaration(text, storePosition) {
|
|
|
77
77
|
return text.lastIndexOf(' =', storePosition) - 1;
|
|
78
78
|
}
|
|
79
79
|
exports.get$storeOffsetOf$storeDeclaration = get$storeOffsetOf$storeDeclaration;
|
|
80
|
+
/**
|
|
81
|
+
* Finds node exactly matching span {start, length}.
|
|
82
|
+
*/
|
|
83
|
+
function findNodeAtSpan(node, span, predicate) {
|
|
84
|
+
const { start, length } = span;
|
|
85
|
+
const end = start + length;
|
|
86
|
+
for (const child of node.getChildren()) {
|
|
87
|
+
const childStart = child.getStart();
|
|
88
|
+
if (end <= childStart) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const childEnd = child.getEnd();
|
|
92
|
+
if (start >= childEnd) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (start === childStart && end === childEnd) {
|
|
96
|
+
if (!predicate) {
|
|
97
|
+
return child;
|
|
98
|
+
}
|
|
99
|
+
if (predicate(child)) {
|
|
100
|
+
return child;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const foundInChildren = findNodeAtSpan(child, span, predicate);
|
|
104
|
+
if (foundInChildren) {
|
|
105
|
+
return foundInChildren;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.findNodeAtSpan = findNodeAtSpan;
|
|
110
|
+
const COMPONENT_SUFFIX = '__SvelteComponent_';
|
|
111
|
+
function isGeneratedSvelteComponentName(className) {
|
|
112
|
+
return className.endsWith(COMPONENT_SUFFIX);
|
|
113
|
+
}
|
|
114
|
+
exports.isGeneratedSvelteComponentName = isGeneratedSvelteComponentName;
|
|
115
|
+
function offsetOfGeneratedComponentExport(snapshot) {
|
|
116
|
+
return snapshot.getText().lastIndexOf(COMPONENT_SUFFIX);
|
|
117
|
+
}
|
|
118
|
+
exports.offsetOfGeneratedComponentExport = offsetOfGeneratedComponentExport;
|
|
119
|
+
function gatherDescendants(node, predicate, dest = []) {
|
|
120
|
+
if (predicate(node)) {
|
|
121
|
+
dest.push(node);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
for (const child of node.getChildren()) {
|
|
125
|
+
gatherDescendants(child, predicate, dest);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return dest;
|
|
129
|
+
}
|
|
130
|
+
exports.gatherDescendants = gatherDescendants;
|
|
80
131
|
//# sourceMappingURL=utils.js.map
|