typescript-svelte-plugin 0.3.13 → 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/module-loader.d.ts +7 -0
- package/dist/src/module-loader.js +51 -14
- package/dist/src/source-mapper.js +1 -1
- package/dist/src/svelte-snapshots.d.ts +3 -0
- package/dist/src/svelte-snapshots.js +39 -0
- package/dist/src/utils.d.ts +14 -0
- package/dist/src/utils.js +54 -3
- package/package.json +5 -5
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) {
|
|
@@ -2,6 +2,13 @@ import type ts from 'typescript/lib/tsserverlibrary';
|
|
|
2
2
|
import { ConfigManager } from './config-manager';
|
|
3
3
|
import { Logger } from './logger';
|
|
4
4
|
import { SvelteSnapshotManager } from './svelte-snapshots';
|
|
5
|
+
declare module 'typescript/lib/tsserverlibrary' {
|
|
6
|
+
interface LanguageServiceHost {
|
|
7
|
+
/** @deprecated supply resolveModuleNameLiterals instead for resolution that can handle newer resolution modes like nodenext */
|
|
8
|
+
resolveModuleNames?(moduleNames: string[], containingFile: string, reusedNames: string[] | undefined, redirectedReference: ts.ResolvedProjectReference | undefined, options: ts.CompilerOptions, containingSourceFile?: ts.SourceFile): (ts.ResolvedModule | undefined)[];
|
|
9
|
+
resolveModuleNameLiterals?(moduleLiterals: readonly ts.StringLiteralLike[], containingFile: string, redirectedReference: ts.ResolvedProjectReference | undefined, options: ts.CompilerOptions, containingSourceFile: ts.SourceFile, reusedNames: readonly ts.StringLiteralLike[] | undefined): readonly ts.ResolvedModuleWithFailedLookupLocations[];
|
|
10
|
+
}
|
|
11
|
+
}
|
|
5
12
|
/**
|
|
6
13
|
* Creates a module loader than can also resolve `.svelte` files.
|
|
7
14
|
*
|
|
@@ -57,11 +57,17 @@ class ModuleResolutionCache {
|
|
|
57
57
|
* In order to fix this, we need to wrap typescript's module resolution and reroute all `.svelte.ts` file lookups to .svelte.
|
|
58
58
|
*/
|
|
59
59
|
function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project, configManager) {
|
|
60
|
-
var _a;
|
|
60
|
+
var _a, _b;
|
|
61
61
|
const svelteSys = (0, svelte_sys_1.createSvelteSys)(logger);
|
|
62
62
|
const moduleCache = new ModuleResolutionCache(project.projectService);
|
|
63
63
|
const origResolveModuleNames = (_a = lsHost.resolveModuleNames) === null || _a === void 0 ? void 0 : _a.bind(lsHost);
|
|
64
|
-
lsHost.
|
|
64
|
+
const origResolveModuleNamLiterals = (_b = lsHost.resolveModuleNameLiterals) === null || _b === void 0 ? void 0 : _b.bind(lsHost);
|
|
65
|
+
if (lsHost.resolveModuleNameLiterals) {
|
|
66
|
+
lsHost.resolveModuleNameLiterals = resolveModuleNameLiterals;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
lsHost.resolveModuleNames = resolveModuleNames;
|
|
70
|
+
}
|
|
65
71
|
const origRemoveFile = project.removeFile.bind(project);
|
|
66
72
|
project.removeFile = (info, fileExists, detachFromProject) => {
|
|
67
73
|
logger.log('File is being removed. Delete from cache: ', info.fileName);
|
|
@@ -81,18 +87,13 @@ function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project,
|
|
|
81
87
|
if (!configManager.getConfig().enable) {
|
|
82
88
|
return resolved;
|
|
83
89
|
}
|
|
84
|
-
return resolved.map((
|
|
85
|
-
const
|
|
86
|
-
if (
|
|
87
|
-
return
|
|
88
|
-
}
|
|
89
|
-
const cachedModule = moduleCache.get(fileName, containingFile);
|
|
90
|
-
if (cachedModule) {
|
|
91
|
-
return cachedModule;
|
|
90
|
+
return resolved.map((tsResolvedModule, idx) => {
|
|
91
|
+
const moduleName = moduleNames[idx];
|
|
92
|
+
if (tsResolvedModule || !(0, utils_1.ensureRealSvelteFilePath)(moduleName).endsWith('.svelte')) {
|
|
93
|
+
return tsResolvedModule;
|
|
92
94
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
return resolvedModule;
|
|
95
|
+
return resolveSvelteModuleNameFromCache(moduleName, containingFile, compilerOptions)
|
|
96
|
+
.resolvedModule;
|
|
96
97
|
});
|
|
97
98
|
}
|
|
98
99
|
function resolveSvelteModuleName(name, containingFile, compilerOptions) {
|
|
@@ -111,10 +112,46 @@ function patchModuleLoader(logger, snapshotManager, typescript, lsHost, project,
|
|
|
111
112
|
}
|
|
112
113
|
const resolvedSvelteModule = {
|
|
113
114
|
extension: snapshot.isTsFile ? typescript.Extension.Ts : typescript.Extension.Js,
|
|
114
|
-
resolvedFileName
|
|
115
|
+
resolvedFileName,
|
|
116
|
+
isExternalLibraryImport: svelteResolvedModule.isExternalLibraryImport
|
|
115
117
|
};
|
|
116
118
|
return resolvedSvelteModule;
|
|
117
119
|
}
|
|
120
|
+
function resolveModuleNameLiterals(moduleLiterals, containingFile, redirectedReference, options, containingSourceFile, reusedNames) {
|
|
121
|
+
var _a;
|
|
122
|
+
logger.log('Resolving modules names for ' + containingFile);
|
|
123
|
+
// Try resolving all module names with the original method first.
|
|
124
|
+
// The ones that are undefined will be re-checked if they are a
|
|
125
|
+
// Svelte file and if so, are resolved, too. This way we can defer
|
|
126
|
+
// all module resolving logic except for Svelte files to TypeScript.
|
|
127
|
+
const resolved = (_a = origResolveModuleNamLiterals === null || origResolveModuleNamLiterals === void 0 ? void 0 : origResolveModuleNamLiterals(moduleLiterals, containingFile, redirectedReference, options, containingSourceFile, reusedNames)) !== null && _a !== void 0 ? _a : moduleLiterals.map(() => ({
|
|
128
|
+
resolvedModule: undefined
|
|
129
|
+
}));
|
|
130
|
+
if (!configManager.getConfig().enable) {
|
|
131
|
+
return resolved;
|
|
132
|
+
}
|
|
133
|
+
return resolved.map((tsResolvedModule, idx) => {
|
|
134
|
+
const moduleName = moduleLiterals[idx].text;
|
|
135
|
+
if (tsResolvedModule.resolvedModule ||
|
|
136
|
+
!(0, utils_1.ensureRealSvelteFilePath)(moduleName).endsWith('.svelte')) {
|
|
137
|
+
return tsResolvedModule;
|
|
138
|
+
}
|
|
139
|
+
return resolveSvelteModuleNameFromCache(moduleName, containingFile, options);
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
function resolveSvelteModuleNameFromCache(moduleName, containingFile, options) {
|
|
143
|
+
const cachedModule = moduleCache.get(moduleName, containingFile);
|
|
144
|
+
if (cachedModule) {
|
|
145
|
+
return {
|
|
146
|
+
resolvedModule: cachedModule
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
const resolvedModule = resolveSvelteModuleName(moduleName, containingFile, options);
|
|
150
|
+
moduleCache.set(moduleName, containingFile, resolvedModule);
|
|
151
|
+
return {
|
|
152
|
+
resolvedModule: resolvedModule
|
|
153
|
+
};
|
|
154
|
+
}
|
|
118
155
|
}
|
|
119
156
|
exports.patchModuleLoader = patchModuleLoader;
|
|
120
157
|
//# sourceMappingURL=module-loader.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SourceMapper = void 0;
|
|
4
|
-
const sourcemap_codec_1 = require("sourcemap-codec");
|
|
4
|
+
const sourcemap_codec_1 = require("@jridgewell/sourcemap-codec");
|
|
5
5
|
function binaryInsert(array, value, key) {
|
|
6
6
|
if (0 === key) {
|
|
7
7
|
key = '0';
|
|
@@ -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 {
|
|
@@ -248,6 +280,13 @@ class SvelteSnapshotManager {
|
|
|
248
280
|
if (normalizedPath.endsWith('node_modules/svelte/types/runtime/ambient.d.ts')) {
|
|
249
281
|
return '';
|
|
250
282
|
}
|
|
283
|
+
else if (normalizedPath.endsWith('svelte2tsx/svelte-jsx.d.ts')) {
|
|
284
|
+
// Remove the dom lib reference to not load these ambient types in case
|
|
285
|
+
// the user has a tsconfig.json with different lib settings like in
|
|
286
|
+
// https://github.com/sveltejs/language-tools/issues/1733
|
|
287
|
+
const originalText = readFile(path) || '';
|
|
288
|
+
return originalText.replace('/// <reference lib="dom" />', '');
|
|
289
|
+
}
|
|
251
290
|
else if (normalizedPath.endsWith('svelte2tsx/svelte-shims.d.ts')) {
|
|
252
291
|
let originalText = readFile(path) || '';
|
|
253
292
|
if (!originalText.includes('// -- start svelte-ls-remove --')) {
|
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
|
}
|
|
@@ -69,12 +69,63 @@ function getConfigPathForProject(project) {
|
|
|
69
69
|
}
|
|
70
70
|
exports.getConfigPathForProject = getConfigPathForProject;
|
|
71
71
|
function isStoreVariableIn$storeDeclaration(text, varStart) {
|
|
72
|
-
return (text.lastIndexOf('
|
|
73
|
-
varStart - '
|
|
72
|
+
return (text.lastIndexOf('__sveltets_2_store_get(', varStart) ===
|
|
73
|
+
varStart - '__sveltets_2_store_get('.length);
|
|
74
74
|
}
|
|
75
75
|
exports.isStoreVariableIn$storeDeclaration = isStoreVariableIn$storeDeclaration;
|
|
76
76
|
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
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typescript-svelte-plugin",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.15",
|
|
4
4
|
"description": "A TypeScript Plugin providing Svelte intellisense",
|
|
5
5
|
"main": "dist/src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,12 +18,12 @@
|
|
|
18
18
|
"author": "The Svelte Community",
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@tsconfig/
|
|
22
|
-
"@types/node": "^
|
|
21
|
+
"@tsconfig/node16": "^1.0.0",
|
|
22
|
+
"@types/node": "^16.0.0",
|
|
23
23
|
"typescript": "*"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"
|
|
27
|
-
"
|
|
26
|
+
"@jridgewell/sourcemap-codec": "^1.4.14",
|
|
27
|
+
"svelte2tsx": "~0.6.0"
|
|
28
28
|
}
|
|
29
29
|
}
|