Skip to content

fix(js): recognize inline and statement type-only imports and exports - #166

Merged
DaisukeYoda merged 2 commits into
ludo-technologies:mainfrom
isaim0011:fix/ts-type-only-import-export-specifiers
Sep 21, 2026
Merged

DaisukeYoda merged 2 commits into
ludo-technologies:mainfrom
isaim0011:fix/ts-type-only-import-export-specifiers

Conversation

@isaim0011

@isaim0011 isaim0011 commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Parser (ast_builder.go, ast.go):
    • Recognize the type token in buildImportStatement and extractImportClause to set Node.IsTypeOnly.
    • Recognize the type token in buildImportSpecifier to set Node.IsType.
    • Recognize the type token in buildExportStatement and extractExportClause to set Node.IsTypeOnly and Node.IsType.
  • Analyzer (module_analyzer.go, unused_code.go):
    • Propagate IsTypeOnly and IsType to domain.Import / domain.Export and their respective specifiers.
    • Set ImportType = domain.ImportTypeTypeOnly when node.IsTypeOnly is true.
    • Skip type-only exports in getExportedNames so that statement-level type exports (e.g. export type { X }) are not flagged as unused runtime exports.
  • Tests:
    • Added unit test TestParseTypeOnlyImportsAndExports in internal/js/parser/parser_test.go covering statement and inline type-only imports/exports.
    • Added unit tests TestDetectUnusedImports_TypeOnlyStatementAndInline_Parsed and TestDetectUnusedExports_TypeOnlyStatementAndInline_Parsed in internal/js/analyzer/unused_code_test.go verifying that unused code analysis skips both statement and inline type-only symbols parsed from source.

- 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 DaisukeYoda left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@isaim0011

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review and clear explanation, @DaisukeYoda!

I've updated the branch with the requested changes:

  1. Preserve structural ImportType: Retained Named, Namespace, etc., instead of overriding with TypeOnly, relying purely on imp.IsTypeOnly so import graph classification remains intact.
  2. Type-only re-export edges: Dropped || exp.IsTypeOnly at unused_code.go:94 and allowed re-exported specifiers to register their names in BuildImportGraph, keeping barrel re-export edges intact.
  3. Removed redundant text check: Cleaned up detectTypeOnlyImportLines.
  4. Regression tests: Added tests covering both import type usage counting for exported classes/functions and type-only barrel re-export edge propagation.

@DaisukeYoda
DaisukeYoda self-requested a review September 21, 2026 09:55

@DaisukeYoda DaisukeYoda left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@DaisukeYoda

Copy link
Copy Markdown
Member

Thanks! LGTM!

@DaisukeYoda
DaisukeYoda merged commit d48481a into ludo-technologies:main Sep 21, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG][auto] ts/inline-type-import-specifier-flag-never-set: import { type X } is treated as a value import, so type-only names are reported unused

2 participants