Skip to content

Commit 4aed3c3

Browse files
committed
fix(plugin-typescript): prevent absolute paths in issue message
1 parent f9089ee commit 4aed3c3

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

e2e/plugin-typescript-e2e/tests/__snapshots__/collect.e2e.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ exports[`PLUGIN collect report with typescript-plugin NPM package > should run p
121121
"details": {
122122
"issues": [
123123
{
124-
"message": "TS6059: File '/home/matejchalk/Projects/quality-metrics-cli/tmp/e2e/plugin-typescript-e2e/exclude/utils.ts' is not under 'rootDir' 'src'. 'rootDir' is expected to contain all source files.",
124+
"message": "TS6059: File './exclude/utils.ts' is not under 'rootDir' 'src'. 'rootDir' is expected to contain all source files.",
125125
"severity": "error",
126126
"source": {
127127
"file": "tmp/e2e/plugin-typescript-e2e/src/6-configuration-errors.ts",

packages/plugin-typescript/src/lib/runner/utils.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,29 @@ export function getSeverity(category: DiagnosticCategory): Issue['severity'] {
4646
}
4747
}
4848

49+
/**
50+
* Format issue message from the TypeScript diagnostic.
51+
* @param diag - The TypeScript diagnostic.
52+
* @returns The issue message.
53+
*/
54+
export function getMessage(diag: Diagnostic): string {
55+
const flattened = flattenDiagnosticMessageText(diag.messageText, '\n');
56+
const text = flattened
57+
.replace(process.cwd(), '.')
58+
.replace(process.cwd().replace(/\\/g, '/'), '.');
59+
return truncateIssueMessage(`TS${diag.code}: ${text}`);
60+
}
61+
4962
/**
5063
* Get the issue from the TypeScript diagnostic.
5164
* @param diag - The TypeScript diagnostic.
5265
* @returns The issue.
5366
* @throws Error if the diagnostic is global (e.g., invalid compiler option).
5467
*/
5568
export function getIssueFromDiagnostic(diag: Diagnostic) {
56-
const message = `${flattenDiagnosticMessageText(diag.messageText, '\n')}`;
57-
5869
const issue: Issue = {
5970
severity: getSeverity(diag.category),
60-
message: truncateIssueMessage(`TS${diag.code}: ${message}`),
71+
message: getMessage(diag),
6172
};
6273

6374
// If undefined, the error might be global (e.g., invalid compiler option).

packages/plugin-typescript/src/lib/runner/utils.unit.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ describe('getIssueFromDiagnostic', () => {
9494
);
9595
});
9696

97+
it('should replace absolute paths in message', () => {
98+
expect(
99+
getIssueFromDiagnostic({
100+
...diagnosticMock,
101+
code: 6059,
102+
messageText: `File '${process.cwd()}/tools/publish.ts' is not under 'rootDir' 'src'. 'rootDir' is expected to contain all source files.`,
103+
}),
104+
).toStrictEqual(
105+
expect.objectContaining({
106+
message: `TS6059: File './tools/publish.ts' is not under 'rootDir' 'src'. 'rootDir' is expected to contain all source files.`,
107+
}),
108+
);
109+
});
110+
97111
it('should return issue without position if file is undefined', () => {
98112
expect(
99113
getIssueFromDiagnostic({ ...diagnosticMock, file: undefined }),

0 commit comments

Comments
 (0)