Skip to content

Commit 0f3ebaa

Browse files
authored
fix: fade unused TypeScript variables (#728)
1 parent c33f730 commit 0f3ebaa

5 files changed

Lines changed: 59 additions & 5 deletions

File tree

‎package-lock.json‎

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/e2e/src/typescript.diagnostics-unused-local.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ export const test: Test = async ({ Command, Editor, expect, FileSystem, Locator,
66
// arrange
77
const fixtureUrl = import.meta.resolve('../fixtures/diagnostics-unused')
88
const workspaceUrl = await FileSystem.loadFixture(fixtureUrl)
9-
await Workspace.setPath(workspaceUrl)
9+
await Workspace.setUri(workspaceUrl)
1010
await Settings.update({ 'editor.diagnostics': true })
1111

1212
// act
1313
await Main.openUri(`${workspaceUrl}/src/test.ts`)
1414
await Command.execute('Editor.waitForDiagnostics')
1515

1616
// assert
17+
const diagnostics = Locator('.Diagnostic')
18+
const unnecessaryTokens = Locator('.EditorTokenUnnecessary')
1719
const expectedDiagnostics = [
1820
{
1921
code: 6133,
@@ -23,10 +25,24 @@ export const test: Test = async ({ Command, Editor, expect, FileSystem, Locator,
2325
message: "'unused' is declared but its value is never read.",
2426
rowIndex: 1,
2527
source: 'ts',
28+
tags: [1],
2629
type: 'warning',
2730
uri: `${workspaceUrl}/src/test.ts`,
2831
},
2932
] as const
30-
await expect(Locator('.Diagnostic')).toHaveCount(expectedDiagnostics.length)
33+
await expect(diagnostics).toHaveCount(expectedDiagnostics.length)
3134
await Editor.shouldHaveDiagnostics(expectedDiagnostics)
35+
await expect(unnecessaryTokens).toHaveText('unused')
36+
37+
// Make the variable used and verify the unnecessary styling is removed without reopening the file.
38+
await Editor.setText('export const fixture = true\nconst unused = 1\nvoid unused\n\nexport {}')
39+
await Command.execute('Editor.waitForDiagnostics')
40+
await Editor.shouldHaveDiagnostics([])
41+
await expect(unnecessaryTokens).toHaveCount(0)
42+
43+
// Restore the unused declaration and verify the styling returns.
44+
await Editor.setText('export const fixture = true\nconst unused = 1\n\nexport {}')
45+
await Command.execute('Editor.waitForDiagnostics')
46+
await Editor.shouldHaveDiagnostics(expectedDiagnostics)
47+
await expect(unnecessaryTokens).toHaveText('unused')
3248
}

‎packages/typescript-worker/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
}
3636
},
3737
"dependencies": {
38+
"@lvce-editor/api": "^9.42.1",
3839
"@noble/hashes": "2.4.0"
3940
}
4041
}

‎packages/typescript-worker/src/parts/GetDiagnosticFromTsResult2/GetDiagnosticFromTsResult2.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DiagnosticTag } from '@lvce-editor/api'
12
import * as GetDiagnosticSeverity from '../GetDiagnosticSeverity/GetDiagnosticSeverity.ts'
23
import { getPositionAt } from '../GetPositionAt/GetPositionAt.ts'
34

@@ -13,6 +14,7 @@ interface TypeScriptDiagnostic {
1314
readonly length: number | undefined
1415
readonly messageText: string | TypeScriptDiagnosticMessageChain
1516
readonly relatedInformation?: readonly TypeScriptDiagnostic[]
17+
readonly reportsUnnecessary?: object
1618
readonly start: number | undefined
1719
}
1820

@@ -31,6 +33,7 @@ interface ConvertedDiagnostic {
3133
readonly relatedInformation?: readonly ConvertedRelatedInformation[]
3234
readonly rowIndex: number
3335
readonly source: 'ts'
36+
readonly tags?: readonly number[]
3437
readonly type: 'error' | 'warning'
3538
readonly uri: string
3639
}
@@ -98,7 +101,11 @@ const convertTsDiagnostic = (text: string, diagnostic: TypeScriptDiagnosticWithL
98101
const convertedItem = convertRelatedInformation(item)
99102
return convertedItem ? [convertedItem] : []
100103
})
101-
return relatedInformation && relatedInformation.length > 0 ? { ...converted, relatedInformation } : converted
104+
const withRelatedInformation =
105+
relatedInformation && relatedInformation.length > 0 ? { ...converted, relatedInformation } : converted
106+
return diagnostic.reportsUnnecessary
107+
? { ...withRelatedInformation, tags: [DiagnosticTag.Unnecessary] }
108+
: withRelatedInformation
102109
}
103110

104111
const hasLocation = (diagnostic: TypeScriptDiagnostic): diagnostic is TypeScriptDiagnosticWithLocation => {

‎packages/typescript-worker/test/GetDiagnosticFromTsResult2.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,37 @@ test('converts diagnostics with a source location', () => {
4646
])
4747
})
4848

49+
test('preserves unnecessary tags from TypeScript diagnostics', () => {
50+
const diagnostics = [
51+
{
52+
category: 1,
53+
code: 6133,
54+
file: {
55+
fileName: '/test.ts',
56+
} as ts.SourceFile,
57+
length: 6,
58+
messageText: "'unused' is declared but its value is never read.",
59+
reportsUnnecessary: {},
60+
start: 6,
61+
},
62+
] as const
63+
64+
expect(getDiagnosticsFromTsResult2('const unused = 1', diagnostics)).toEqual([
65+
{
66+
code: 6133,
67+
columnIndex: 6,
68+
endColumnIndex: 12,
69+
endRowIndex: 0,
70+
message: "'unused' is declared but its value is never read.",
71+
rowIndex: 0,
72+
source: 'ts',
73+
tags: [1],
74+
type: 'warning',
75+
uri: '/test.ts',
76+
},
77+
])
78+
})
79+
4980
test('flattens diagnostic message chains', () => {
5081
const diagnostics = [
5182
{

0 commit comments

Comments
 (0)