Skip to content

Commit 861e042

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

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-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: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,27 @@ 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.replace(process.cwd(), '.');
57+
return truncateIssueMessage(`TS${diag.code}: ${text}`);
58+
}
59+
4960
/**
5061
* Get the issue from the TypeScript diagnostic.
5162
* @param diag - The TypeScript diagnostic.
5263
* @returns The issue.
5364
* @throws Error if the diagnostic is global (e.g., invalid compiler option).
5465
*/
5566
export function getIssueFromDiagnostic(diag: Diagnostic) {
56-
const message = `${flattenDiagnosticMessageText(diag.messageText, '\n')}`;
57-
5867
const issue: Issue = {
5968
severity: getSeverity(diag.category),
60-
message: truncateIssueMessage(`TS${diag.code}: ${message}`),
69+
message: getMessage(diag),
6170
};
6271

6372
// 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)