Skip to content

Fix false positives for type-only import in rules that use compilation #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions src/shared/svelte-compile-warns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ export function getSvelteCompileWarnings(
importsNotUsedAsValues: ts.ImportsNotUsedAsValues.Preserve,
sourceMap: true,
},
transformers: {
before: [createTsImportTransformer(ts)],
},
})

const outputText = `${output.outputText}\n`
Expand Down Expand Up @@ -340,38 +337,3 @@ function getWarningsFromCode(
]
}
}

/**
* @see https://github.com/sveltejs/eslint-plugin-svelte3/blob/259263ccaf69c59e473d9bfa39706b0955eccfbd/src/preprocess.js#L194
* MIT License @ Conduitry
*/
function createTsImportTransformer(
ts: TS,
): typescript.TransformerFactory<typescript.SourceFile> {
const factory = ts.factory
/**
* https://github.com/sveltejs/svelte-preprocess/blob/main/src/transformers/typescript.ts
* TypeScript transformer for preserving imports correctly when preprocessing TypeScript files
*/
return (context: typescript.TransformationContext) => {
/** visitor */
function visit(node: typescript.Node): typescript.Node {
if (ts.isImportDeclaration(node)) {
if (node.importClause && node.importClause.isTypeOnly) {
return factory.createEmptyStatement()
}

return factory.createImportDeclaration(
node.decorators,
node.modifiers,
node.importClause,
node.moduleSpecifier,
)
}

return ts.visitEachChild(node, (child) => visit(child), context)
}

return (node: typescript.SourceFile) => ts.visitNode(node, visit)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script lang="ts">
import type { Writable } from "svelte/store"
import { writable } from "svelte/store"
let a: Writable = writable(42)
</script>

{{ $a }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script lang="ts">
import { type Writable, writable } from "svelte/store"
let a: Writable = writable(42)
</script>

{{ $a }}