@marko/language-server 1.0.1 → 1.0.3

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/index.js CHANGED
@@ -27,17 +27,31 @@ var import_util2 = require("util");
27
27
  var import_node = require("vscode-languageserver/node");
28
28
 
29
29
  // src/utils/project.ts
30
+ var import_path = __toESM(require("path"));
30
31
  var defaultCompiler = __toESM(require("@marko/compiler"));
31
32
  var defaultTranslator = __toESM(require("@marko/translator-default"));
32
- var import_lasso_package_root = __toESM(require("lasso-package-root"));
33
33
  var import_resolve_from = __toESM(require("resolve-from"));
34
- var cwd = process.cwd();
35
- var kTaglib = Symbol("taglib");
34
+ var ignoreErrors = (_err) => {
35
+ };
36
36
  var projectsByDir = /* @__PURE__ */ new Map();
37
+ var projectsByCompiler = /* @__PURE__ */ new Map();
37
38
  var defaultProject = {
38
- rootDir: cwd,
39
39
  cache: /* @__PURE__ */ new Map(),
40
- lookup: defaultCompiler.taglib.buildLookup(cwd, defaultTranslator),
40
+ getLookup(dir) {
41
+ const key = `taglib:${dir}`;
42
+ let lookup = defaultProject.cache.get(key);
43
+ if (!lookup) {
44
+ defaultProject.cache.set(
45
+ key,
46
+ lookup = defaultCompiler.taglib.buildLookup(
47
+ dir,
48
+ defaultTranslator,
49
+ ignoreErrors
50
+ )
51
+ );
52
+ }
53
+ return lookup;
54
+ },
41
55
  compiler: defaultCompiler,
42
56
  translator: defaultTranslator
43
57
  };
@@ -53,55 +67,119 @@ function getMarkoProject(dir) {
53
67
  return project;
54
68
  }
55
69
  function getMarkoProjects() {
56
- return new Set(projectsByDir.values());
70
+ return projectsByCompiler.values();
71
+ }
72
+ function clearMarkoProjectCaches() {
73
+ for (const project of getMarkoProjects()) {
74
+ project.cache.clear();
75
+ project.compiler.taglib.clearCaches();
76
+ }
57
77
  }
58
78
  function loadProject(dir) {
59
- const rootDir = import_lasso_package_root.default.getRootDir(dir) || cwd;
60
- const pkgPath = import_resolve_from.default.silent(rootDir, "@marko/compiler/package.json");
61
- const pkg = pkgPath && require(pkgPath);
62
- const cache = /* @__PURE__ */ new Map();
63
- let translator = defaultTranslator;
64
- let compiler = defaultCompiler;
65
- if (pkg && /^5\./.test(pkg.version)) {
66
- try {
67
- let checkTranslator = [].concat(
68
- Object.keys(pkg.dependencies),
69
- Object.keys(pkg.peerDependencies),
70
- Object.keys(pkg.devDependencies)
71
- ).find((name) => /^marko$|^(@\/marko\/|marko-)translator-/.test(name));
72
- if (checkTranslator === "marko" || !checkTranslator) {
73
- checkTranslator = require((0, import_resolve_from.default)(dir, "@marko/compiler/config")).translator;
79
+ try {
80
+ const compilerConfigPath = (0, import_resolve_from.default)(dir, "@marko/compiler/config");
81
+ const cachedProject = projectsByCompiler.get(compilerConfigPath);
82
+ if (cachedProject)
83
+ return cachedProject;
84
+ const [compiler, translator] = [
85
+ require(import_path.default.join(compilerConfigPath, "..")),
86
+ require((0, import_resolve_from.default)(
87
+ dir,
88
+ interopDefault(require(compilerConfigPath)).translator
89
+ ))
90
+ ];
91
+ const project = {
92
+ cache: /* @__PURE__ */ new Map(),
93
+ getLookup(dir2) {
94
+ const key = `taglib:${dir2}`;
95
+ let lookup = project.cache.get(key);
96
+ if (lookup === void 0) {
97
+ try {
98
+ lookup = compiler.taglib.buildLookup(dir2, translator, ignoreErrors);
99
+ } catch {
100
+ lookup = defaultProject.getLookup(dir2);
101
+ }
102
+ project.cache.set(key, lookup);
103
+ }
104
+ return lookup;
105
+ },
106
+ compiler,
107
+ translator
108
+ };
109
+ projectsByCompiler.set(compilerConfigPath, project);
110
+ return project;
111
+ } catch {
112
+ return defaultProject;
113
+ }
114
+ }
115
+ function interopDefault(mod) {
116
+ return mod.default || mod;
117
+ }
118
+
119
+ // src/utils/file.ts
120
+ var import_path2 = __toESM(require("path"));
121
+ var import_language_tools = require("@marko/language-tools");
122
+ var import_vscode_uri = require("vscode-uri");
123
+ var processorCaches = /* @__PURE__ */ new WeakMap();
124
+ function getFSPath(doc) {
125
+ return import_vscode_uri.URI.parse(doc.uri).fsPath;
126
+ }
127
+ function getMarkoFile(doc) {
128
+ const { uri } = doc;
129
+ const { fsPath: filename, scheme } = import_vscode_uri.URI.parse(uri);
130
+ const dirname = filename && import_path2.default.dirname(filename);
131
+ const project = getMarkoProject(dirname);
132
+ const cache = project.cache;
133
+ let file = cache.get(doc);
134
+ if (!file) {
135
+ const { version } = doc;
136
+ const code = doc.getText();
137
+ const parsed = (0, import_language_tools.parse)(code, filename);
138
+ const lookup = project.getLookup(dirname);
139
+ cache.set(
140
+ doc,
141
+ file = {
142
+ project,
143
+ uri,
144
+ scheme,
145
+ version,
146
+ lookup,
147
+ filename,
148
+ dirname,
149
+ parsed,
150
+ code
74
151
  }
75
- [compiler, translator] = [
76
- require((0, import_resolve_from.default)(dir, "@marko/compiler")),
77
- require((0, import_resolve_from.default)(dir, checkTranslator))
78
- ];
79
- } catch {
152
+ );
153
+ }
154
+ return file;
155
+ }
156
+ function clearMarkoCacheForFile(doc) {
157
+ const { fsPath: filename } = import_vscode_uri.URI.parse(doc.uri);
158
+ const dirname = filename && import_path2.default.dirname(filename);
159
+ const project = getMarkoProject(dirname);
160
+ const cache = project.cache;
161
+ cache.delete(doc);
162
+ }
163
+ function processDoc(doc, process2) {
164
+ const file = getMarkoFile(doc);
165
+ const cache = processorCaches.get(file.parsed);
166
+ let result;
167
+ if (cache) {
168
+ result = cache.get(process2);
169
+ if (!result) {
170
+ result = process2(file);
171
+ cache.set(process2, result);
80
172
  }
173
+ } else {
174
+ result = process2(file);
175
+ processorCaches.set(file.parsed, /* @__PURE__ */ new Map([[process2, result]]));
81
176
  }
82
- return {
83
- rootDir,
84
- cache,
85
- get lookup() {
86
- let lookup = cache.get(kTaglib);
87
- if (lookup === void 0) {
88
- try {
89
- lookup = compiler.taglib.buildLookup(dir, translator);
90
- } catch {
91
- lookup = defaultProject.lookup;
92
- }
93
- cache.set(kTaglib, lookup);
94
- }
95
- return lookup;
96
- },
97
- compiler,
98
- translator
99
- };
177
+ return result;
100
178
  }
101
179
 
102
180
  // src/utils/text-documents.ts
103
181
  var import_fs = __toESM(require("fs"));
104
- var import_vscode_uri = require("vscode-uri");
182
+ var import_vscode_uri2 = require("vscode-uri");
105
183
  var import_vscode_languageserver = require("vscode-languageserver");
106
184
  var import_vscode_languageserver_textdocument = require("vscode-languageserver-textdocument");
107
185
  var docs = /* @__PURE__ */ new Map();
@@ -119,7 +197,7 @@ function get(uri) {
119
197
  const doc = docs.get(uri);
120
198
  if (doc)
121
199
  return doc;
122
- const { fsPath, scheme } = import_vscode_uri.URI.parse(uri);
200
+ const { fsPath, scheme } = import_vscode_uri2.URI.parse(uri);
123
201
  if (scheme === "file") {
124
202
  if (fileExists.get(uri) === false)
125
203
  return void 0;
@@ -142,7 +220,7 @@ function exists(uri) {
142
220
  const cached = fileExists.get(uri);
143
221
  if (cached !== void 0)
144
222
  return cached;
145
- const { fsPath, scheme } = import_vscode_uri.URI.parse(uri);
223
+ const { fsPath, scheme } = import_vscode_uri2.URI.parse(uri);
146
224
  if (scheme === "file") {
147
225
  try {
148
226
  import_fs.default.accessSync(fsPath);
@@ -196,7 +274,7 @@ function setup(connection4) {
196
274
  if (doc) {
197
275
  projectVersion++;
198
276
  openDocs.delete(doc);
199
- if (import_vscode_uri.URI.parse(ref.uri).scheme !== "file") {
277
+ if (import_vscode_uri2.URI.parse(ref.uri).scheme !== "file") {
200
278
  docs.delete(ref.uri);
201
279
  }
202
280
  }
@@ -299,62 +377,6 @@ function display(type, data) {
299
377
  setImmediate(() => connection2.sendNotification(type, msg));
300
378
  }
301
379
 
302
- // src/utils/file.ts
303
- var import_path = __toESM(require("path"));
304
- var import_language_tools = require("@marko/language-tools");
305
- var import_vscode_uri2 = require("vscode-uri");
306
- var processorCaches = /* @__PURE__ */ new WeakMap();
307
- function getFSDir(doc) {
308
- const filename = getFSPath(doc);
309
- return filename ? import_path.default.dirname(filename) : void 0;
310
- }
311
- function getFSPath(doc) {
312
- return import_vscode_uri2.URI.parse(doc.uri).fsPath;
313
- }
314
- function getMarkoFile(doc) {
315
- const { uri } = doc;
316
- const { fsPath: filename, scheme } = import_vscode_uri2.URI.parse(uri);
317
- const dirname = filename && import_path.default.dirname(filename);
318
- const project = getMarkoProject(dirname);
319
- const cache = project.cache;
320
- let file = cache.get(doc);
321
- if (!file) {
322
- const { version } = doc;
323
- const code = doc.getText();
324
- const parsed = (0, import_language_tools.parse)(code, filename);
325
- cache.set(
326
- doc,
327
- file = {
328
- project,
329
- uri,
330
- scheme,
331
- version,
332
- filename,
333
- dirname,
334
- parsed,
335
- code
336
- }
337
- );
338
- }
339
- return file;
340
- }
341
- function processDoc(doc, process2) {
342
- const file = getMarkoFile(doc);
343
- const cache = processorCaches.get(file.parsed);
344
- let result;
345
- if (cache) {
346
- result = cache.get(process2);
347
- if (!result) {
348
- result = process2(file);
349
- cache.set(process2, result);
350
- }
351
- } else {
352
- result = process2(file);
353
- processorCaches.set(file.parsed, /* @__PURE__ */ new Map([[process2, result]]));
354
- }
355
- return result;
356
- }
357
-
358
380
  // src/service/index.ts
359
381
  var import_vscode_languageserver12 = require("vscode-languageserver");
360
382
 
@@ -366,10 +388,7 @@ var import_vscode_languageserver2 = require("vscode-languageserver");
366
388
  function AttrName({
367
389
  offset,
368
390
  node,
369
- file: {
370
- parsed,
371
- project: { lookup }
372
- }
391
+ file: { parsed, lookup }
373
392
  }) {
374
393
  let name = parsed.read(node);
375
394
  const modifierIndex = name.indexOf(":");
@@ -466,7 +485,7 @@ function isExternalModule(file) {
466
485
  }
467
486
 
468
487
  // src/service/marko/complete/AttrValue.ts
469
- var import_path2 = __toESM(require("path"));
488
+ var import_path3 = __toESM(require("path"));
470
489
  var import_vscode_languageserver3 = require("vscode-languageserver");
471
490
 
472
491
  // src/service/marko/util/is-document-link-attr.ts
@@ -576,7 +595,7 @@ async function AttrValue({
576
595
  const resolved = resolveUrl(req, uri);
577
596
  if (resolved) {
578
597
  const result = [];
579
- const curFile = req === "." ? import_path2.default.basename(uri) : void 0;
598
+ const curFile = req === "." ? import_path3.default.basename(uri) : void 0;
580
599
  const replaceRange = parsed.locationAt({
581
600
  start: start + segmentStart + 1,
582
601
  end: start + rawValue.length
@@ -609,7 +628,7 @@ async function AttrValue({
609
628
  var import_vscode_languageserver5 = require("vscode-languageserver");
610
629
 
611
630
  // src/service/marko/util/get-tag-name-completion.ts
612
- var import_path3 = __toESM(require("path"));
631
+ var import_path4 = __toESM(require("path"));
613
632
  var import_vscode_languageserver4 = require("vscode-languageserver");
614
633
  var import_vscode_uri3 = require("vscode-uri");
615
634
  var deprecated = [import_vscode_languageserver4.CompletionItemTag.Deprecated];
@@ -632,7 +651,7 @@ function getTagNameCompletion({
632
651
  kind: import_vscode_languageserver4.MarkupKind.Markdown,
633
652
  value: tag.html ? `Built in [<${tag.name}>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/${tag.name}) HTML tag.` : isCoreTag ? `Core Marko <${tag.name}> tag.` : nodeModuleName ? `Custom Marko tag discovered from the ["${nodeModuleName}"](${fileURIForTag}) npm package.` : `Custom Marko tag discovered from:
634
653
 
635
- [${importer ? import_path3.default.relative(importer, fileForTag) : fileForTag}](${fileURIForTag})`
654
+ [${importer ? import_path4.default.relative(importer, fileForTag) : fileForTag}](${fileURIForTag})`
636
655
  };
637
656
  if (tag.description) {
638
657
  documentation.value += `
@@ -669,11 +688,7 @@ ${autocomplete.description}`;
669
688
  var importTagReg = /(['"])<((?:[^\1\\>]+|\\.)*)>?\1/;
670
689
  function Import({
671
690
  node,
672
- file: {
673
- parsed,
674
- filename,
675
- project: { lookup }
676
- }
691
+ file: { parsed, filename, lookup }
677
692
  }) {
678
693
  var _a;
679
694
  const value = parsed.read(node);
@@ -705,11 +720,7 @@ function Import({
705
720
  var import_language_tools3 = require("@marko/language-tools");
706
721
  function OpenTagName({
707
722
  node,
708
- file: {
709
- parsed,
710
- filename,
711
- project: { lookup }
712
- }
723
+ file: { parsed, filename, lookup }
713
724
  }) {
714
725
  var _a;
715
726
  const tag = node.parent;
@@ -826,14 +837,14 @@ var doComplete = async (doc, params) => {
826
837
  };
827
838
 
828
839
  // src/service/marko/validate.ts
829
- var import_path4 = __toESM(require("path"));
840
+ var import_path5 = __toESM(require("path"));
830
841
  var import_vscode_languageserver7 = require("vscode-languageserver");
831
842
  var markoErrorRegExp = /^(.+?)\.marko(?:\((\d+)(?:\s*,\s*(\d+))?\))?: (.*)$/gm;
832
843
  var doValidate = (doc) => {
833
844
  const filename = getFSPath(doc);
834
845
  const diagnostics = [];
835
846
  const { compiler, translator, cache } = getMarkoProject(
836
- filename && import_path4.default.dirname(filename)
847
+ filename && import_path5.default.dirname(filename)
837
848
  );
838
849
  try {
839
850
  compiler.compileSync(doc.getText(), filename || "untitled.marko", {
@@ -889,11 +900,7 @@ var START_LOCATION = {
889
900
  // src/service/marko/hover/OpenTagName.ts
890
901
  function OpenTagName2({
891
902
  node,
892
- file: {
893
- parsed,
894
- filename,
895
- project: { lookup }
896
- }
903
+ file: { parsed, filename, lookup }
897
904
  }) {
898
905
  const tag = node.parent;
899
906
  const range = parsed.locationAt(node);
@@ -965,10 +972,7 @@ function escape(val) {
965
972
  // src/service/marko/definition/AttrName.ts
966
973
  function AttrName2({
967
974
  node,
968
- file: {
969
- parsed,
970
- project: { lookup }
971
- }
975
+ file: { parsed, lookup }
972
976
  }) {
973
977
  const tagName = node.parent.parent.nameText;
974
978
  const attrName = parsed.read(node);
@@ -1007,15 +1011,12 @@ function AttrName2({
1007
1011
 
1008
1012
  // src/service/marko/definition/OpenTagName.ts
1009
1013
  var import_fs3 = __toESM(require("fs"));
1010
- var import_path5 = __toESM(require("path"));
1014
+ var import_path6 = __toESM(require("path"));
1011
1015
  var import_vscode_uri5 = require("vscode-uri");
1012
1016
  var import_language_tools8 = require("@marko/language-tools");
1013
1017
  function OpenTagName3({
1014
1018
  node,
1015
- file: {
1016
- parsed,
1017
- project: { lookup }
1018
- }
1019
+ file: { parsed, lookup }
1019
1020
  }) {
1020
1021
  const tag = node.parent;
1021
1022
  let tagDef;
@@ -1032,7 +1033,7 @@ function OpenTagName3({
1032
1033
  return;
1033
1034
  }
1034
1035
  const tagEntryFile = tagDef.template || tagDef.renderer || tagDef.filePath;
1035
- if (!import_path5.default.isAbsolute(tagEntryFile)) {
1036
+ if (!import_path6.default.isAbsolute(tagEntryFile)) {
1036
1037
  return;
1037
1038
  }
1038
1039
  if (/\/marko(?:-tag)?\.json$/.test(tagEntryFile)) {
@@ -1087,7 +1088,7 @@ function extractDocumentLinks({
1087
1088
  scheme,
1088
1089
  parsed,
1089
1090
  code,
1090
- project: { lookup }
1091
+ lookup
1091
1092
  }) {
1092
1093
  if (scheme !== "file") {
1093
1094
  return [];
@@ -1163,7 +1164,7 @@ function extractDocumentSymbols({
1163
1164
  uri,
1164
1165
  scheme,
1165
1166
  parsed,
1166
- project: { lookup }
1167
+ lookup
1167
1168
  }) {
1168
1169
  if (scheme !== "file") {
1169
1170
  return [];
@@ -1244,7 +1245,7 @@ var marko_default = {
1244
1245
  };
1245
1246
 
1246
1247
  // src/service/script/index.ts
1247
- var import_path9 = __toESM(require("path"));
1248
+ var import_path10 = __toESM(require("path"));
1248
1249
  var import_relative_import_path = require("relative-import-path");
1249
1250
  var import_tsserverlibrary = __toESM(require("typescript/lib/tsserverlibrary"));
1250
1251
  var import_vscode_languageserver10 = require("vscode-languageserver");
@@ -1253,27 +1254,42 @@ var prettier2 = __toESM(require("prettier"));
1253
1254
  var import_language_tools14 = require("@marko/language-tools");
1254
1255
 
1255
1256
  // src/ts-plugin/host.ts
1256
- var import_path8 = __toESM(require("path"));
1257
+ var import_path9 = __toESM(require("path"));
1257
1258
  var import_language_tools13 = require("@marko/language-tools");
1258
1259
 
1259
1260
  // src/utils/get-runtime-types.ts
1260
- var import_path6 = __toESM(require("path"));
1261
- var internalTypesFile = import_path6.default.join(__dirname, "marko.internal.d.ts");
1262
- var defaultMarkoTypesFile = import_path6.default.join(__dirname, "marko.runtime.d.ts");
1263
- function getProjectTypeLibs(project, ts2, host) {
1261
+ var import_path7 = __toESM(require("path"));
1262
+ var internalTypesFile = import_path7.default.join(__dirname, "marko.internal.d.ts");
1263
+ var defaultMarkoTypesFile = import_path7.default.join(__dirname, "marko.runtime.d.ts");
1264
+ function getProjectTypeLibs(rootDir, project, ts2, host) {
1264
1265
  let cached = project.cache.get(getProjectTypeLibs);
1265
1266
  if (cached === void 0) {
1266
- const { resolvedTypeReferenceDirective } = ts2.resolveTypeReferenceDirective(
1267
+ const markoRunGeneratedTypesFile = import_path7.default.join(
1268
+ rootDir,
1269
+ ".marko-run/routes.d.ts"
1270
+ );
1271
+ const resolveFromFile = import_path7.default.join(host.getCurrentDirectory(), "_.d.ts");
1272
+ const compilerOptions = host.getCompilationSettings();
1273
+ const { resolvedTypeReferenceDirective: resolvedMarkoTypes } = ts2.resolveTypeReferenceDirective(
1267
1274
  project.translator.runtimeTypes || "marko",
1268
- import_path6.default.join(project.rootDir, "_.d.ts"),
1269
- host.getCompilationSettings(),
1275
+ resolveFromFile,
1276
+ compilerOptions,
1270
1277
  host
1271
1278
  );
1272
- const markoTypesFile = (resolvedTypeReferenceDirective == null ? void 0 : resolvedTypeReferenceDirective.resolvedFileName) || defaultMarkoTypesFile;
1279
+ const { resolvedTypeReferenceDirective: resolvedMarkoRunTypes } = ts2.resolveTypeReferenceDirective(
1280
+ "@marko/run",
1281
+ resolveFromFile,
1282
+ compilerOptions,
1283
+ host
1284
+ );
1285
+ const markoTypesFile = (resolvedMarkoTypes == null ? void 0 : resolvedMarkoTypes.resolvedFileName) || defaultMarkoTypesFile;
1286
+ const markoRunTypesFile = resolvedMarkoRunTypes == null ? void 0 : resolvedMarkoRunTypes.resolvedFileName;
1273
1287
  cached = {
1274
1288
  internalTypesFile,
1275
1289
  markoTypesFile,
1276
- markoTypesCode: host.readFile(markoTypesFile, "utf-8") || ""
1290
+ markoTypesCode: host.readFile(markoTypesFile, "utf-8") || "",
1291
+ markoRunTypesFile,
1292
+ markoRunGeneratedTypesFile: host.fileExists(markoRunGeneratedTypesFile) ? markoRunGeneratedTypesFile : void 0
1277
1293
  };
1278
1294
  project.cache.set(getProjectTypeLibs, cached);
1279
1295
  }
@@ -1304,24 +1320,29 @@ function getScriptLang(filename, ts2, host, projectScriptLang) {
1304
1320
  }
1305
1321
 
1306
1322
  // src/utils/get-component-filename.ts
1307
- var import_path7 = __toESM(require("path"));
1308
- function getComponentFilename(from, host) {
1309
- const dir = import_path7.default.dirname(from);
1310
- const nameNoExt = import_path7.default.basename(from, ".marko");
1323
+ var import_fs4 = __toESM(require("fs"));
1324
+ var import_path8 = __toESM(require("path"));
1325
+ function getComponentFilename(from) {
1326
+ const dir = import_path8.default.dirname(from);
1327
+ const nameNoExt = import_path8.default.basename(from, ".marko");
1311
1328
  const isEntry = nameNoExt === "index";
1312
- const componentFull = import_path7.default.join(dir, `${nameNoExt}.component.`);
1313
- const componentBrowserFull = import_path7.default.join(
1314
- dir,
1315
- `${nameNoExt}.component-browser.`
1316
- );
1317
- const componentPartial = isEntry ? import_path7.default.join(dir, "component.") : void 0;
1318
- const componentBrowserPartial = isEntry ? import_path7.default.join(dir, "component-browser.") : void 0;
1319
- for (const entry of host.readDirectory(dir)) {
1329
+ const componentFull = `${nameNoExt}.component.`;
1330
+ const componentBrowserFull = `${nameNoExt}.component-browser.`;
1331
+ const componentPartial = isEntry ? "component." : void 0;
1332
+ const componentBrowserPartial = isEntry ? "component-browser." : void 0;
1333
+ for (const entry of tryReaddirSync(dir)) {
1320
1334
  if (entry !== from && (isEntry && entry.startsWith(componentBrowserPartial) || entry.startsWith(componentPartial)) || entry.startsWith(componentBrowserFull) || entry.startsWith(componentFull)) {
1321
- return entry;
1335
+ return import_path8.default.join(dir, entry);
1322
1336
  }
1323
1337
  }
1324
1338
  }
1339
+ function tryReaddirSync(dir) {
1340
+ try {
1341
+ return import_fs4.default.readdirSync(dir);
1342
+ } catch {
1343
+ return [];
1344
+ }
1345
+ }
1325
1346
 
1326
1347
  // src/ts-plugin/host.ts
1327
1348
  var markoExt = ".marko";
@@ -1330,19 +1351,27 @@ var modulePartsReg = /^((?:@(?:[^/]+)\/)?(?:[^/]+))(.*)$/;
1330
1351
  var fsPathReg = /^(?:[./\\]|[A-Z]:)/i;
1331
1352
  function patch(ts2, scriptLang, cache, host) {
1332
1353
  var _a, _b, _c;
1354
+ const rootDir = host.getCurrentDirectory();
1333
1355
  const projectTypeLibs = getProjectTypeLibs(
1334
- getMarkoProject(host.getCurrentDirectory()),
1356
+ rootDir,
1357
+ getMarkoProject(rootDir),
1335
1358
  ts2,
1336
1359
  host
1337
1360
  );
1361
+ const projectTypeLibsFiles = [
1362
+ projectTypeLibs.internalTypesFile,
1363
+ projectTypeLibs.markoTypesFile
1364
+ ];
1365
+ if (projectTypeLibs.markoRunTypesFile) {
1366
+ projectTypeLibsFiles.push(projectTypeLibs.markoRunTypesFile);
1367
+ }
1368
+ if (projectTypeLibs.markoRunGeneratedTypesFile) {
1369
+ projectTypeLibsFiles.push(projectTypeLibs.markoRunGeneratedTypesFile);
1370
+ }
1338
1371
  const isMarkoTSFile = (fileName) => getScriptLang(fileName, ts2, host, scriptLang) === import_language_tools13.ScriptLang.ts;
1339
1372
  const getScriptFileNames = host.getScriptFileNames.bind(host);
1340
1373
  host.getScriptFileNames = () => [
1341
- .../* @__PURE__ */ new Set([
1342
- ...getScriptFileNames(),
1343
- projectTypeLibs.internalTypesFile,
1344
- projectTypeLibs.markoTypesFile
1345
- ])
1374
+ ...new Set(projectTypeLibsFiles.concat(getScriptFileNames()))
1346
1375
  ];
1347
1376
  const getScriptKind = (_a = host.getScriptKind) == null ? void 0 : _a.bind(host);
1348
1377
  if (getScriptKind) {
@@ -1356,27 +1385,41 @@ function patch(ts2, scriptLang, cache, host) {
1356
1385
  let cached = cache.get(filename);
1357
1386
  if (!cached) {
1358
1387
  const code = host.readFile(filename, "utf-8") || "";
1359
- const markoProject = getMarkoProject(import_path8.default.dirname(filename));
1360
- cached = (0, import_language_tools13.extractScript)({
1361
- ts: ts2,
1362
- parsed: (0, import_language_tools13.parse)(code, filename),
1363
- lookup: markoProject.lookup,
1364
- scriptLang: getScriptLang(filename, ts2, host, scriptLang),
1365
- runtimeTypesCode: projectTypeLibs.markoTypesCode,
1366
- componentFilename: getComponentFilename(filename, host)
1367
- });
1368
- cached.snapshot = ts2.ScriptSnapshot.fromString(cached.toString());
1388
+ const dir = import_path9.default.dirname(filename);
1389
+ try {
1390
+ const markoProject = getMarkoProject(dir);
1391
+ cached = (0, import_language_tools13.extractScript)({
1392
+ ts: ts2,
1393
+ parsed: (0, import_language_tools13.parse)(code, filename),
1394
+ lookup: markoProject.getLookup(dir),
1395
+ scriptLang: getScriptLang(filename, ts2, host, scriptLang),
1396
+ runtimeTypesCode: projectTypeLibs.markoTypesCode,
1397
+ componentFilename: getComponentFilename(filename)
1398
+ });
1399
+ cached.snapshot = ts2.ScriptSnapshot.fromString(cached.toString());
1400
+ } catch {
1401
+ cached = { snapshot: ts2.ScriptSnapshot.fromString("") };
1402
+ }
1369
1403
  cache.set(filename, cached);
1370
1404
  }
1371
1405
  return cached.snapshot;
1372
1406
  }
1373
1407
  return getScriptSnapshot(filename);
1374
1408
  };
1409
+ if (host.getProjectVersion) {
1410
+ const getScriptVersion = host.getScriptVersion.bind(host);
1411
+ host.getScriptVersion = (filename) => {
1412
+ if (markoExtReg.test(filename)) {
1413
+ return host.getProjectVersion();
1414
+ }
1415
+ return getScriptVersion(filename);
1416
+ };
1417
+ }
1375
1418
  const readDirectory2 = (_b = host.readDirectory) == null ? void 0 : _b.bind(host);
1376
1419
  if (readDirectory2) {
1377
- host.readDirectory = (path10, extensions, exclude, include, depth) => {
1420
+ host.readDirectory = (path11, extensions, exclude, include, depth) => {
1378
1421
  return readDirectory2(
1379
- path10,
1422
+ path11,
1380
1423
  extensions == null ? void 0 : extensions.concat(markoExt),
1381
1424
  exclude,
1382
1425
  include,
@@ -1387,31 +1430,16 @@ function patch(ts2, scriptLang, cache, host) {
1387
1430
  const resolveModuleNames = (_c = host.resolveModuleNames) == null ? void 0 : _c.bind(host);
1388
1431
  if (resolveModuleNames) {
1389
1432
  host.resolveModuleNames = (moduleNames, containingFile, reusedNames, redirectedReference, options, sourceFile) => {
1390
- const resolvedModules = resolveModuleNames(
1391
- moduleNames,
1392
- containingFile,
1393
- reusedNames,
1394
- redirectedReference,
1395
- options,
1396
- sourceFile
1397
- );
1398
- for (let i = resolvedModules.length; i--; ) {
1433
+ let normalModuleNames = moduleNames;
1434
+ let resolvedModules;
1435
+ for (let i = 0; i < moduleNames.length; i++) {
1399
1436
  const moduleName = moduleNames[i];
1400
- if (!resolvedModules[i] && markoExtReg.test(moduleName)) {
1437
+ const shouldProcess = moduleName[0] !== "*" ? markoExtReg.test(moduleName) : void 0;
1438
+ if (shouldProcess) {
1439
+ let resolvedFileName;
1401
1440
  if (fsPathReg.test(moduleName)) {
1402
- const resolvedFileName = import_path8.default.resolve(
1403
- containingFile,
1404
- "..",
1405
- moduleName
1406
- );
1407
- if (host.fileExists(resolvedFileName)) {
1408
- resolvedModules[i] = {
1409
- resolvedFileName,
1410
- extension: isMarkoTSFile(resolvedFileName) ? ts2.Extension.Ts : ts2.Extension.Js,
1411
- isExternalLibraryImport: false
1412
- };
1413
- }
1414
- } else if (moduleName[0] !== "*") {
1441
+ resolvedFileName = import_path9.default.resolve(containingFile, "..", moduleName);
1442
+ } else {
1415
1443
  const [, nodeModuleName, relativeModulePath] = modulePartsReg.exec(moduleName);
1416
1444
  const { resolvedModule } = ts2.resolveModuleName(
1417
1445
  `${nodeModuleName}/package.json`,
@@ -1420,31 +1448,70 @@ function patch(ts2, scriptLang, cache, host) {
1420
1448
  host
1421
1449
  );
1422
1450
  if (resolvedModule) {
1423
- const resolvedFileName = import_path8.default.join(
1451
+ resolvedFileName = import_path9.default.join(
1424
1452
  resolvedModule.resolvedFileName,
1425
1453
  "..",
1426
1454
  relativeModulePath
1427
1455
  );
1428
- if (host.fileExists(resolvedFileName)) {
1429
- const isTS = isMarkoTSFile(resolvedFileName);
1430
- resolvedModules[i] = {
1431
- resolvedFileName,
1432
- extension: isTS ? ts2.Extension.Ts : ts2.Extension.Js,
1433
- isExternalLibraryImport: isTS
1434
- };
1435
- }
1436
1456
  }
1437
1457
  }
1458
+ if (!resolvedModules) {
1459
+ resolvedModules = [];
1460
+ normalModuleNames = [];
1461
+ for (let j = 0; j < i; j++) {
1462
+ resolvedModules.push(void 0);
1463
+ normalModuleNames.push(moduleNames[j]);
1464
+ }
1465
+ }
1466
+ resolvedModules.push(
1467
+ resolvedFileName && host.fileExists(resolvedFileName) ? {
1468
+ resolvedFileName,
1469
+ extension: isMarkoTSFile(resolvedFileName) ? ts2.Extension.Ts : ts2.Extension.Js,
1470
+ isExternalLibraryImport: false
1471
+ } : null
1472
+ );
1473
+ } else if (resolvedModules) {
1474
+ resolvedModules.push(void 0);
1438
1475
  }
1439
1476
  }
1440
- return resolvedModules;
1477
+ const normalResolvedModules = normalModuleNames.length ? resolveModuleNames(
1478
+ normalModuleNames,
1479
+ containingFile,
1480
+ reusedNames,
1481
+ redirectedReference,
1482
+ options,
1483
+ sourceFile
1484
+ ) : void 0;
1485
+ if (resolvedModules) {
1486
+ if (normalResolvedModules) {
1487
+ for (let i = 0, j = 0; i < resolvedModules.length; i++) {
1488
+ switch (resolvedModules[i]) {
1489
+ case void 0:
1490
+ resolvedModules[i] = normalResolvedModules[j++];
1491
+ break;
1492
+ case null:
1493
+ resolvedModules[i] = void 0;
1494
+ break;
1495
+ }
1496
+ }
1497
+ } else {
1498
+ for (let i = resolvedModules.length; i--; ) {
1499
+ if (resolvedModules[i] === null) {
1500
+ resolvedModules[i] = void 0;
1501
+ }
1502
+ }
1503
+ }
1504
+ return resolvedModules;
1505
+ } else {
1506
+ return normalResolvedModules;
1507
+ }
1441
1508
  };
1442
1509
  }
1443
1510
  return host;
1444
1511
  }
1445
1512
 
1446
1513
  // src/service/script/index.ts
1447
- var IGNORE_DIAG_REG = /^(?:Expression|Identifier|['"][^\w]['"]) expected.$/;
1514
+ var IGNORE_DIAG_REG = /^(?:(?:Expression|Identifier|['"][^\w]['"]) expected|Invalid character)\b/i;
1448
1515
  var extractCache = /* @__PURE__ */ new Map();
1449
1516
  var snapshotCache = /* @__PURE__ */ new Map();
1450
1517
  var insertModuleStatementLocCache = /* @__PURE__ */ new WeakMap();
@@ -1454,6 +1521,34 @@ var optionalModifierReg = /\boptional\b/;
1454
1521
  var deprecatedModifierReg = /\bdeprecated\b/;
1455
1522
  var colorModifierReg = /\bcolor\b/;
1456
1523
  var localInternalsPrefix = "__marko_internal_";
1524
+ var requiredTSCompilerOptions = {
1525
+ module: import_tsserverlibrary.default.ModuleKind.ESNext,
1526
+ moduleResolution: import_tsserverlibrary.default.ModuleResolutionKind.NodeJs,
1527
+ noEmit: true,
1528
+ allowJs: true,
1529
+ composite: false,
1530
+ declaration: false,
1531
+ skipLibCheck: true,
1532
+ isolatedModules: true,
1533
+ resolveJsonModule: true,
1534
+ skipDefaultLibCheck: true,
1535
+ emitDeclarationOnly: false,
1536
+ allowNonTsExtensions: true,
1537
+ emitDecoratorMetadata: false
1538
+ };
1539
+ var defaultTSConfig = {
1540
+ include: [],
1541
+ compilerOptions: {
1542
+ lib: ["dom", "node", "esnext"]
1543
+ }
1544
+ };
1545
+ var extraTSCompilerExtensions = [
1546
+ {
1547
+ extension: ".marko",
1548
+ isMixedContent: false,
1549
+ scriptKind: import_tsserverlibrary.default.ScriptKind.Deferred
1550
+ }
1551
+ ];
1457
1552
  var ScriptService = {
1458
1553
  commands: {
1459
1554
  "$/showScriptOutput": async (uri) => {
@@ -1538,7 +1633,7 @@ var ScriptService = {
1538
1633
  let source = completion.source;
1539
1634
  if (source && completion.hasAction) {
1540
1635
  if (source[0] === ".") {
1541
- source = import_path9.default.resolve(fileName, "..", source);
1636
+ source = import_path10.default.resolve(fileName, "..", source);
1542
1637
  }
1543
1638
  detail = (0, import_relative_import_path.relativeImportPath)(fileName, source);
1544
1639
  sortText = `\uFFFF${sortText}`;
@@ -1842,19 +1937,26 @@ ${documentation}`;
1842
1937
  }
1843
1938
  };
1844
1939
  function processScript(doc, tsProject) {
1845
- return processDoc(doc, ({ parsed, filename, project: markoProject }) => {
1846
- var _a;
1847
- const { lookup } = markoProject;
1848
- const { host, markoScriptLang } = tsProject;
1849
- return (0, import_language_tools14.extractScript)({
1850
- ts: import_tsserverlibrary.default,
1851
- parsed,
1852
- lookup,
1853
- scriptLang: getScriptLang(filename, import_tsserverlibrary.default, host, markoScriptLang),
1854
- runtimeTypesCode: (_a = getProjectTypeLibs(markoProject, import_tsserverlibrary.default, host)) == null ? void 0 : _a.markoTypesCode,
1855
- componentFilename: getComponentFilename(filename, host)
1856
- });
1857
- });
1940
+ return processDoc(
1941
+ doc,
1942
+ ({ filename, parsed, lookup, project: markoProject }) => {
1943
+ var _a;
1944
+ const { host, markoScriptLang } = tsProject;
1945
+ return (0, import_language_tools14.extractScript)({
1946
+ ts: import_tsserverlibrary.default,
1947
+ parsed,
1948
+ lookup,
1949
+ scriptLang: getScriptLang(filename, import_tsserverlibrary.default, host, markoScriptLang),
1950
+ runtimeTypesCode: (_a = getProjectTypeLibs(
1951
+ tsProject.rootDir,
1952
+ markoProject,
1953
+ import_tsserverlibrary.default,
1954
+ host
1955
+ )) == null ? void 0 : _a.markoTypesCode,
1956
+ componentFilename: getComponentFilename(filename)
1957
+ });
1958
+ }
1959
+ );
1858
1960
  }
1859
1961
  function getInsertModuleStatementOffset(parsed) {
1860
1962
  const { program } = parsed;
@@ -1923,7 +2025,7 @@ function getTSProject(docFsPath) {
1923
2025
  );
1924
2026
  }
1925
2027
  }
1926
- const rootDir = configPath && import_path9.default.dirname(configPath) || process.cwd();
2028
+ const rootDir = configPath && import_path10.default.dirname(configPath) || process.cwd();
1927
2029
  const markoProject = getMarkoProject(configPath && rootDir);
1928
2030
  let projectCache = markoProject.cache.get(getTSProject);
1929
2031
  let cached;
@@ -1936,32 +2038,21 @@ function getTSProject(docFsPath) {
1936
2038
  markoProject.cache.set(getTSProject, projectCache);
1937
2039
  }
1938
2040
  const { fileNames, options, projectReferences } = import_tsserverlibrary.default.parseJsonConfigFileContent(
1939
- configPath && import_tsserverlibrary.default.readConfigFile(configPath, import_tsserverlibrary.default.sys.readFile).config || {
1940
- compilerOptions: { lib: ["dom", "node", "esnext"] }
1941
- },
2041
+ configPath && import_tsserverlibrary.default.readConfigFile(configPath, import_tsserverlibrary.default.sys.readFile).config || defaultTSConfig,
1942
2042
  import_tsserverlibrary.default.sys,
1943
2043
  rootDir,
1944
- void 0,
2044
+ requiredTSCompilerOptions,
1945
2045
  configPath,
1946
2046
  void 0,
1947
- [
1948
- {
1949
- extension: ".marko",
1950
- isMixedContent: false,
1951
- scriptKind: import_tsserverlibrary.default.ScriptKind.Deferred
1952
- }
1953
- ]
2047
+ extraTSCompilerExtensions
1954
2048
  );
2049
+ options.rootDir ??= rootDir;
1955
2050
  const potentialGlobalFiles = new Set(
1956
2051
  fileNames.filter((file) => /\.[cm]?ts$/.test(file))
1957
2052
  );
1958
- options.rootDir ??= rootDir;
1959
- options.module = import_tsserverlibrary.default.ModuleKind.ESNext;
1960
- options.moduleResolution = import_tsserverlibrary.default.ModuleResolutionKind.NodeJs;
1961
- options.noEmit = options.allowJs = options.declaration = options.skipLibCheck = options.isolatedModules = options.resolveJsonModule = options.skipDefaultLibCheck = options.allowNonTsExtensions = true;
1962
2053
  const tsPkgFile = configPath && ((_a = import_tsserverlibrary.default.resolveModuleName("typescript/package.json", configPath, options, import_tsserverlibrary.default.sys).resolvedModule) == null ? void 0 : _a.resolvedFileName);
1963
- const defaultLibFile = import_path9.default.join(
1964
- tsPkgFile ? import_path9.default.join(tsPkgFile, "../lib") : __dirname,
2054
+ const defaultLibFile = import_path10.default.join(
2055
+ tsPkgFile ? import_path10.default.join(tsPkgFile, "../lib") : __dirname,
1965
2056
  import_tsserverlibrary.default.getDefaultLibFileName(options)
1966
2057
  );
1967
2058
  const host = patch(
@@ -2015,12 +2106,16 @@ function getTSProject(docFsPath) {
2015
2106
  return `${((_a2 = get(filenameToURI(filename))) == null ? void 0 : _a2.version) ?? -1}`;
2016
2107
  },
2017
2108
  getScriptKind(filename) {
2018
- switch (import_path9.default.extname(filename)) {
2109
+ switch (import_path10.default.extname(filename)) {
2019
2110
  case import_tsserverlibrary.default.Extension.Js:
2111
+ case import_tsserverlibrary.default.Extension.Cjs:
2112
+ case import_tsserverlibrary.default.Extension.Mjs:
2020
2113
  return import_tsserverlibrary.default.ScriptKind.JS;
2021
2114
  case import_tsserverlibrary.default.Extension.Jsx:
2022
2115
  return import_tsserverlibrary.default.ScriptKind.JSX;
2023
2116
  case import_tsserverlibrary.default.Extension.Ts:
2117
+ case import_tsserverlibrary.default.Extension.Cts:
2118
+ case import_tsserverlibrary.default.Extension.Mts:
2024
2119
  return import_tsserverlibrary.default.ScriptKind.TS;
2025
2120
  case import_tsserverlibrary.default.Extension.Tsx:
2026
2121
  return import_tsserverlibrary.default.ScriptKind.TSX;
@@ -2049,7 +2144,12 @@ function getTSProject(docFsPath) {
2049
2144
  service: import_tsserverlibrary.default.createLanguageService(host),
2050
2145
  markoProject,
2051
2146
  markoScriptLang,
2052
- markoProjectTypeLibs: getProjectTypeLibs(markoProject, import_tsserverlibrary.default, host)
2147
+ markoProjectTypeLibs: getProjectTypeLibs(
2148
+ options.rootDir,
2149
+ markoProject,
2150
+ import_tsserverlibrary.default,
2151
+ host
2152
+ )
2053
2153
  };
2054
2154
  projectCache.set(rootDir, tsProject);
2055
2155
  return tsProject;
@@ -2516,7 +2616,7 @@ var StyleSheetService = {
2516
2616
  }
2517
2617
  };
2518
2618
  function processStyle(doc) {
2519
- return processDoc(doc, ({ uri, version, parsed, project: { lookup } }) => {
2619
+ return processDoc(doc, ({ uri, version, parsed, lookup }) => {
2520
2620
  var _a;
2521
2621
  const result = [];
2522
2622
  for (const [ext, extracted] of (0, import_language_tools15.extractStyle)({
@@ -2994,7 +3094,7 @@ setup(connection3);
2994
3094
  onFileChange((changeDoc) => {
2995
3095
  if (changeDoc) {
2996
3096
  queueDiagnostic();
2997
- getMarkoProject(getFSDir(changeDoc)).cache.delete(changeDoc);
3097
+ clearMarkoCacheForFile(changeDoc);
2998
3098
  } else {
2999
3099
  validateDocs();
3000
3100
  }
@@ -3091,10 +3191,7 @@ for (const command in service.commands) {
3091
3191
  }
3092
3192
  function validateDocs() {
3093
3193
  queueDiagnostic();
3094
- for (const project of getMarkoProjects()) {
3095
- project.cache.clear();
3096
- project.compiler.taglib.clearCaches();
3097
- }
3194
+ clearMarkoProjectCaches();
3098
3195
  }
3099
3196
  function queueDiagnostic() {
3100
3197
  clearTimeout(diagnosticTimeout);