Skip to content

Commit c07193e

Browse files
committed
feat: add generate trace api
1 parent 145d1a7 commit c07193e

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

.changeset/open-mails-lick.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@marko/language-server": minor
3+
"@marko/type-check": minor
4+
---
5+
6+
Add generateTrace api.

packages/language-server/src/service/script/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ const optionalModifierReg = /\boptional\b/;
5555
const deprecatedModifierReg = /\bdeprecated\b/;
5656
const colorModifierReg = /\bcolor\b/;
5757
const localInternalsPrefix = "__marko_internal_";
58+
const getCanonicalFileName = ts.sys.useCaseSensitiveFileNames
59+
? (fileName: string) => fileName
60+
: (fileName: string) => fileName.toLocaleLowerCase();
5861
const requiredTSCompilerOptions: ts.CompilerOptions = {
5962
module: ts.ModuleKind.ESNext,
6063
moduleResolution: ts.ModuleResolutionKind.Bundler,
@@ -1010,8 +1013,4 @@ function getTSTriggerChar(char: string | undefined) {
10101013
return char as ts.CompletionsTriggerCharacter;
10111014
}
10121015

1013-
function getCanonicalFileName(fileName: string) {
1014-
return ts.sys.useCaseSensitiveFileNames ? fileName : fileName.toLowerCase();
1015-
}
1016-
10171016
export { ScriptService as default };

packages/type-check/src/cli.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const args = arg(
1212
{
1313
"--project": String,
1414
"--display": String,
15+
"--generateTrace": String,
1516
"--help": Boolean,
1617
"--version": Boolean,
1718
"-p": "--project",
@@ -72,6 +73,7 @@ For more information, visit ${color.blue(
7273
);
7374
} else {
7475
const {
76+
"--generateTrace": generateTrace,
7577
"--display": display = process.env.CI
7678
? Display.condensed
7779
: Display.codeframe,
@@ -86,7 +88,7 @@ For more information, visit ${color.blue(
8688
}
8789

8890
checkDisplay(display);
89-
run({ project, display });
91+
run({ generateTrace, project, display });
9092
}
9193

9294
function checkDisplay(

packages/type-check/src/run.ts

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface Options {
2424
project?: string;
2525
display?: Display;
2626
emit?: boolean;
27+
generateTrace?: string;
2728
}
2829

2930
interface Report {
@@ -45,8 +46,8 @@ const isSourceMapExtensionReg = /\.map$/;
4546
const skipRemapExtensionsReg =
4647
/\.(?:[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)$/;
4748

48-
const extractCache = new WeakMap<
49-
ts.SourceFile,
49+
const extractCache = new Map<
50+
string,
5051
ReturnType<Processors.Processor["extract"]>
5152
>();
5253
const requiredTSCompilerOptions: ts.CompilerOptions = {
@@ -66,8 +67,55 @@ export default function run(opts: Options) {
6667
findRootConfigFile("jsconfig.json"),
6768
} = opts;
6869

69-
if (!configFile)
70+
if (!configFile) {
7071
throw new Error("Could not find tsconfig.json or jsconfig.json");
72+
}
73+
74+
if (opts.generateTrace) {
75+
(ts as any).startTracing?.(
76+
"build",
77+
path.resolve(opts.generateTrace),
78+
configFile,
79+
);
80+
const tracing = (ts as any).tracing;
81+
if (!tracing) {
82+
throw new Error("generateTrace not available in TypeScript compiler.");
83+
}
84+
85+
const { push } = tracing;
86+
tracing.push = (
87+
phase: unknown,
88+
name: unknown,
89+
args?: unknown,
90+
separateBeginAndEnd?: unknown,
91+
) => {
92+
if (
93+
args &&
94+
typeof args === "object" &&
95+
"pos" in args &&
96+
typeof args.pos === "number" &&
97+
"end" in args &&
98+
typeof args.end === "number"
99+
) {
100+
const fileName =
101+
"path" in args
102+
? args.path
103+
: "fileName" in args
104+
? args.fileName
105+
: undefined;
106+
if (typeof fileName === "string") {
107+
const extracted = extractCache.get(getCanonicalFileName(fileName));
108+
if (extracted) {
109+
(args as any).generatedPos = args.pos;
110+
(args as any).generatedEnd = args.end;
111+
args.pos = extracted.sourceOffsetAt(args.pos);
112+
args.end = extracted.sourceOffsetAt(args.end);
113+
}
114+
}
115+
}
116+
return push.call(tracing, phase, name, args, separateBeginAndEnd);
117+
};
118+
}
71119

72120
const formatSettings = ts.getDefaultFormatCodeSettings(
73121
ts.sys.newLine,
@@ -332,7 +380,7 @@ export default function run(opts: Options) {
332380
.createHash("md5")
333381
.update(extractedCode)
334382
.digest("hex");
335-
extractCache.set(sourceFile, extracted);
383+
extractCache.set(getCanonicalFileName(fileName), extracted);
336384
return sourceFile;
337385
}
338386

@@ -433,7 +481,7 @@ export default function run(opts: Options) {
433481
}
434482

435483
const extracted = extractCache.get(
436-
program.getSourceFile(sourceFile.fileName)!,
484+
getCanonicalFileName(sourceFile.fileName),
437485
)!;
438486
const printContext: Processors.PrintContext = {
439487
extracted,
@@ -542,7 +590,9 @@ function reportDiagnostic(report: Report, diag: ts.Diagnostic) {
542590
let loc: Location | void = undefined;
543591

544592
if (diag.start !== undefined) {
545-
const extracted = extractCache.get(diag.file);
593+
const extracted = extractCache.get(
594+
getCanonicalFileName(diag.file.fileName),
595+
);
546596

547597
if (extracted) {
548598
loc = extracted.sourceLocationAt(

0 commit comments

Comments
 (0)