Repository navigation
fix(js): recognize inline and statement type-only imports and exports - #166
Conversation
- Parse 'type' keyword in AST builder for both statement-level and inline specifiers (import & export) - Propagate IsTypeOnly and IsType flags to domain.Import and domain.Export models - Mark type-only statement imports as ImportTypeTypeOnly - Ensure unused code analyzer respects type-only specifiers and statement-level exports - Add unit tests for parser and end-to-end analyzer detection
DaisukeYoda
left a comment
There was a problem hiding this comment.
The parser changes are correct and both symptoms in #164 are gone. I built the branch and confirmed the repro from the issue now reports nothing, and ./internal/js/... is green.
One regression needs a fix before merge. Overriding ImportType with TypeOnly removes import type { X } from the import graph, because the switch in BuildImportGraph (unused_code.go:76) has no case for it. The IsTypeOnly guard at unused_code.go:94 fires for the first time and drops type-only re-export edges as well, which undoes a568633 for type-only barrels.
Measured with the built binary:
// lib.ts
export class Widget { x = 1 }
// app.ts
import type { Widget } from './lib';
export const f = (w: Widget) => w;main : (no findings)
this : unused_exported_function | Exported function 'Widget' is not imported by any other analyzed file
The same finding appears when the class is re-exported through export type { Widget } from './lib'. import type * as ns from './x' is also affected, since it is now classified as TypeOnly instead of Namespace and no longer registers "*".
Suggested fix: leave ImportType alone and rely on imp.IsTypeOnly, which unused_code.go:161 already reads, then drop || exp.IsTypeOnly from unused_code.go:94 so type-only re-exports keep registering their edges and names.
One more cleanup worth folding in: detectTypeOnlyImportLines (unused_code.go:233) is a textual strings.HasPrefix(line, "import type ") heuristic that this PR makes redundant, since the parser now sets IsTypeOnly for every statement form including import type X from and import type * as ns.
| // Determine import type | ||
| if hasNamespace { | ||
| if node.IsTypeOnly { | ||
| imp.ImportType = domain.ImportTypeTypeOnly |
There was a problem hiding this comment.
This is the regression. ImportType is only ever read by the switch at unused_code.go:76, which has no ImportTypeTypeOnly case, so import type { X } from './lib' stops registering X as an imported name and lib.ts starts reporting X as an unused export. It also reclassifies import type * as ns from Namespace to TypeOnly, which loses the "*" entry and exposes every export of the target.
imp.IsTypeOnly is set two lines up at 298 and is already checked by unused_code.go:161, so the skip in DetectUnusedImports works without touching ImportType. Please drop this branch.
|
|
||
| // getExportedNames extracts the exported name(s) from an export declaration. | ||
| func getExportedNames(exp *domain.Export) []string { | ||
| if exp.IsTypeOnly { |
There was a problem hiding this comment.
This guard is redundant. Both callers that produce findings (:296 and :549) already skip type-only exports a few lines earlier, and I confirmed the analyzer tests stay green with this hunk reverted.
The only call site it does affect is :107, where export * expands the target's exported names, and there it drops usage information rather than adding it. I would remove it.
| continue | ||
| } | ||
|
|
||
| if child.Type() == "type" { |
There was a problem hiding this comment.
Minor: for export type { X } the type token is a direct child of export_statement, so buildExportStatement already handles it and this branch never fires. Worth confirming and removing if it is dead.
…ort barrels - Retain structural ImportType (Named, Namespace, etc.) and rely on IsTypeOnly flag - Allow type-only re-exports to register import graph edges and names - Remove obsolete detectTypeOnlyImportLines textual heuristic - Add tests covering type-only imports and re-export barrel usage
|
Thanks for the thorough review and clear explanation, @DaisukeYoda! I've updated the branch with the requested changes:
|
There was a problem hiding this comment.
The regression is fixed. Thanks for the quick turnaround.
I rebuilt both binaries and ran a differential scan of --select deadcode against three real TypeScript projects. The branch removes findings and adds none:
| project | findings on main | on this branch | added |
|---|---|---|---|
| opencode (479 .ts) | 1539 | 1496 | 0 |
| civic-ai (117 .ts) | 31 | 29 | 0 |
| supplement-guide (93 .ts) | 23 | 22 | 0 |
| nativeCandidate := filepath.FromSlash(candidate) | ||
| slashCandidate := filepath.ToSlash(candidate) | ||
| if knownFiles[nativeCandidate] { | ||
| return nativeCandidate |
There was a problem hiding this comment.
This hunk looks unrelated to #164. filepath.FromSlash and filepath.ToSlash are the identity function on everything except Windows, so on Linux and macOS the two lookups are the same lookup. I reverted just this hunk and the whole suite still passes, so nothing here needs it.
That makes it a Windows-only path resolution change riding along in a TypeScript parser fix, with no test covering it. I would drop it and file it separately if the Windows behaviour is actually wrong.
| } | ||
| // Skip type-only imports (import type { Foo } from 'bar') | ||
| if imp.IsTypeOnly || imp.ImportType == domain.ImportTypeTypeOnly || typeOnlyImportLines[imp.Location.StartLine] { | ||
| if imp.IsTypeOnly || imp.ImportType == domain.ImportTypeTypeOnly { |
There was a problem hiding this comment.
Now that processImportDeclaration no longer assigns ImportTypeTypeOnly, the second half of this condition is dead. The only remaining reference to the constant is a hand-built fixture at unused_code_test.go:212. Either drop the ImportType check and keep imp.IsTypeOnly, or keep it deliberately and say so in the comment.
|
|
||
| // getExportedNames extracts the exported name(s) from an export declaration. | ||
| func getExportedNames(exp *domain.Export) []string { | ||
| if exp.IsTypeOnly { |
There was a problem hiding this comment.
Still redundant, as in the previous round. Both callers that emit findings skip type-only exports before reaching here, and the suite passes with this guard removed. The remaining caller at :107 expands export *, where returning nil only discards usage information.
| continue | ||
| } | ||
|
|
||
| if child.Type() == "type" { |
There was a problem hiding this comment.
Confirmed dead. I deleted this branch and every parser and analyzer test still passes, because the type token in export type { X } is a direct child of export_statement and buildExportStatement already handles it at line 1128.
|
Thanks! LGTM! |
Resolves #164
Summary
Addresses #164 where TypeScript type-only imports and exports (both statement-level and inline specifiers) were incorrectly flagged as unused code.
Changes
ast_builder.go,ast.go):typetoken inbuildImportStatementandextractImportClauseto setNode.IsTypeOnly.typetoken inbuildImportSpecifierto setNode.IsType.typetoken inbuildExportStatementandextractExportClauseto setNode.IsTypeOnlyandNode.IsType.module_analyzer.go,unused_code.go):IsTypeOnlyandIsTypetodomain.Import/domain.Exportand their respective specifiers.ImportType = domain.ImportTypeTypeOnlywhennode.IsTypeOnlyis true.getExportedNamesso that statement-level type exports (e.g.export type { X }) are not flagged as unused runtime exports.TestParseTypeOnlyImportsAndExportsininternal/js/parser/parser_test.gocovering statement and inline type-only imports/exports.TestDetectUnusedImports_TypeOnlyStatementAndInline_ParsedandTestDetectUnusedExports_TypeOnlyStatementAndInline_Parsedininternal/js/analyzer/unused_code_test.goverifying that unused code analysis skips both statement and inline type-only symbols parsed from source.