@marko/type-check 1.0.6 → 1.1.0

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.
Files changed (2) hide show
  1. package/dist/cli.js +84 -38
  2. package/package.json +2 -2
package/dist/cli.js CHANGED
@@ -45,6 +45,8 @@ var currentDirectory = import_tsserverlibrary.default.sys.getCurrentDirectory();
45
45
  var getCanonicalFileName = import_tsserverlibrary.default.sys.useCaseSensitiveFileNames ? (fileName) => fileName : (fileName) => fileName.toLowerCase();
46
46
  var fsPathReg = /^(?:[./\\]|[A-Z]:)/i;
47
47
  var modulePartsReg = /^((?:@(?:[^/]+)\/)?(?:[^/]+))(.*)$/;
48
+ var isRemapExtensionReg = /\.ts$/;
49
+ var skipRemapExtensionsReg = /\.(?:[cm]?jsx?|json|marko|css|less|sass|scss|styl|stylus|pcss|postcss|sss|a?png|jpe?g|jfif|pipeg|pjp|gif|svg|ico|web[pm]|avif|mp4|ogg|mp3|wav|flac|aac|opus|woff2?|eot|[ot]tf|webmanifest|pdf|txt)$/;
48
50
  var extractCache = /* @__PURE__ */ new WeakMap();
49
51
  var requiredTSCompilerOptions = {
50
52
  allowJs: true,
@@ -87,6 +89,43 @@ function run(opts) {
87
89
  getCanonicalFileName,
88
90
  options
89
91
  );
92
+ const getJSFileIfTSExists = (source, importer) => compilerHost.fileExists(import_path.default.join(importer, "..", `${source}.ts`)) && `${source}.js`;
93
+ const customTransformers = {
94
+ after: [
95
+ (ctx) => (sourceFile) => {
96
+ return import_tsserverlibrary.default.visitNode(sourceFile, visit);
97
+ function visit(node) {
98
+ if (import_tsserverlibrary.default.isSourceFile(node)) {
99
+ return import_tsserverlibrary.default.visitEachChild(node, visit, ctx);
100
+ }
101
+ if ((import_tsserverlibrary.default.isImportDeclaration(node) || import_tsserverlibrary.default.isExportDeclaration(node)) && node.moduleSpecifier && import_tsserverlibrary.default.isStringLiteral(node.moduleSpecifier)) {
102
+ const value = node.moduleSpecifier.text;
103
+ if (value[0] === "." && !skipRemapExtensionsReg.test(value)) {
104
+ const { fileName } = sourceFile;
105
+ const remap = isRemapExtensionReg.test(value) ? `${value.slice(0, -2)}js` : getJSFileIfTSExists(value, fileName) || getJSFileIfTSExists(`${value}/index`, fileName);
106
+ if (remap) {
107
+ return import_tsserverlibrary.default.isImportDeclaration(node) ? ctx.factory.updateImportDeclaration(
108
+ node,
109
+ node.modifiers,
110
+ node.importClause,
111
+ ctx.factory.createStringLiteral(remap),
112
+ node.attributes
113
+ ) : ctx.factory.updateExportDeclaration(
114
+ node,
115
+ node.modifiers,
116
+ node.isTypeOnly,
117
+ node.exportClause,
118
+ ctx.factory.createStringLiteral(remap),
119
+ node.attributes
120
+ );
121
+ }
122
+ }
123
+ }
124
+ return node;
125
+ }
126
+ }
127
+ ]
128
+ };
90
129
  const { readDirectory = import_tsserverlibrary.default.sys.readDirectory } = compilerHost;
91
130
  compilerHost.readDirectory = (path3, extensions, exclude, include, depth) => readDirectory(
92
131
  path3,
@@ -224,7 +263,7 @@ function run(opts) {
224
263
  );
225
264
  const program = builderProgram.getProgram();
226
265
  const builderEmit = builderProgram.emit.bind(builderProgram);
227
- builderProgram.emit = (targetSourceFile, _writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers) => {
266
+ builderProgram.emit = (targetSourceFile, _writeFile, cancellationToken, emitOnlyDtsFiles) => {
228
267
  let writeFile = _writeFile;
229
268
  if (_writeFile) {
230
269
  const typeChecker = program.getTypeChecker();
@@ -354,48 +393,55 @@ function run(opts) {
354
393
  );
355
394
  }
356
395
  function reportDiagnostic(report, diag) {
357
- if (diag.file && diag.start !== void 0) {
358
- const extracted = extractCache.get(diag.file);
396
+ const diagMessage = flattenDiagnosticMessage(
397
+ diag.messageText,
398
+ report.display,
399
+ report.formatSettings.newLineCharacter
400
+ );
401
+ if (diag.file) {
359
402
  let code = diag.file.text;
360
- let loc;
361
- if (extracted) {
362
- loc = extracted.sourceLocationAt(
363
- diag.start,
364
- diag.start + (diag.length || 0)
403
+ let loc = void 0;
404
+ if (diag.start !== void 0) {
405
+ const extracted = extractCache.get(diag.file);
406
+ if (extracted) {
407
+ loc = extracted.sourceLocationAt(
408
+ diag.start,
409
+ diag.start + (diag.length || 0)
410
+ );
411
+ code = extracted.parsed.code;
412
+ } else {
413
+ const start = import_tsserverlibrary.default.getLineAndCharacterOfPosition(diag.file, diag.start);
414
+ const end = diag.length ? import_tsserverlibrary.default.getLineAndCharacterOfPosition(
415
+ diag.file,
416
+ diag.start + diag.length
417
+ ) : start;
418
+ loc = {
419
+ start,
420
+ end
421
+ };
422
+ }
423
+ }
424
+ if (loc) {
425
+ report.out.push(
426
+ `${import_kleur.default.cyan(
427
+ import_path.default.relative(currentDirectory, diag.file.fileName)
428
+ )}:${import_kleur.default.yellow(loc.start.line + 1)}:${import_kleur.default.yellow(
429
+ loc.start.character + 1
430
+ )} - ${coloredDiagnosticCategory(diag.category)} ${import_kleur.default.dim(
431
+ `TS${diag.code}`
432
+ )}${report.formatSettings.newLineCharacter}${report.display === Display.codeframe ? report.formatSettings.newLineCharacter + formatCodeFrameMessage(code, loc, diagMessage) : diagMessage}`
365
433
  );
366
- code = extracted.parsed.code;
367
- if (!loc)
368
- return;
369
434
  } else {
370
- const start = import_tsserverlibrary.default.getLineAndCharacterOfPosition(diag.file, diag.start);
371
- const end = diag.length ? import_tsserverlibrary.default.getLineAndCharacterOfPosition(diag.file, diag.start + diag.length) : start;
372
- loc = {
373
- start,
374
- end
375
- };
435
+ report.out.push(
436
+ `${import_kleur.default.cyan(
437
+ import_path.default.relative(currentDirectory, diag.file.fileName)
438
+ )} - ${coloredDiagnosticCategory(diag.category)} ${import_kleur.default.dim(
439
+ `TS${diag.code}`
440
+ )}${report.formatSettings.newLineCharacter}${diagMessage}`
441
+ );
376
442
  }
377
- const diagMessage = flattenDiagnosticMessage(
378
- diag.messageText,
379
- report.display,
380
- report.formatSettings.newLineCharacter
381
- );
382
- report.out.push(
383
- `${import_kleur.default.cyan(
384
- import_path.default.relative(currentDirectory, diag.file.fileName)
385
- )}:${import_kleur.default.yellow(loc.start.line + 1)}:${import_kleur.default.yellow(
386
- loc.start.character + 1
387
- )} - ${coloredDiagnosticCategory(diag.category)} ${import_kleur.default.dim(
388
- `TS${diag.code}`
389
- )}${report.formatSettings.newLineCharacter}${report.display === Display.codeframe ? report.formatSettings.newLineCharacter + formatCodeFrameMessage(code, loc, diagMessage) : diagMessage}`
390
- );
391
443
  } else {
392
- report.out.push(
393
- flattenDiagnosticMessage(
394
- diag.messageText,
395
- report.display,
396
- report.formatSettings.newLineCharacter
397
- )
398
- );
444
+ report.out.push(diagMessage);
399
445
  }
400
446
  if (!report.hasErrors && diag.category === import_tsserverlibrary.default.DiagnosticCategory.Error) {
401
447
  report.hasErrors = true;
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@marko/type-check",
3
3
  "description": "A CLI to type check Marko projects",
4
- "version": "1.0.6",
4
+ "version": "1.1.0",
5
5
  "bugs": "https://github.com/marko-js/language-server/issues/new?template=Bug_report.md",
6
6
  "dependencies": {
7
7
  "@babel/code-frame": "^7.23.5",
8
- "@marko/language-tools": "^2.1.10",
8
+ "@marko/language-tools": "^2.2.0",
9
9
  "arg": "^5.0.2",
10
10
  "kleur": "^4.1.5",
11
11
  "strip-json-comments": "^3.1.1",