Skip to content

Commit

Permalink
chore: fix compatibility with verbatimModuleSyntax
Browse files Browse the repository at this point in the history
  • Loading branch information
wessberg committed Oct 11, 2024
1 parent 1ff651f commit 83dd1ee
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"pnpm": "^9.12.1",
"prettier": "^3.3.3",
"rimraf": "^6.0.1",
"sandhog": "^3.0.0",
"sandhog": "^3.0.1",
"semver": "^7.6.3",
"standard-changelog": "^6.0.0",
"tsup": "^8.3.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/transformer/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function transform(source: string, filenameOrOptions: string | TransformO
// By preserving value imports, we can avoid the `after` transformer entirely,
// as well as adding/tracking imports,since nothing will be stripped away.
// eslint-disable-next-line @typescript-eslint/no-deprecated
baseVisitorContext.compilerOptions.verbatimModuleSyntax = baseVisitorContext.compilerOptions.preserveValueImports = true;
baseVisitorContext.compilerOptions.preserveValueImports = true;

const {compilerOptions} = baseVisitorContext;
const typescript = baseVisitorContext.typescript as TSExtended;
Expand Down
2 changes: 1 addition & 1 deletion src/util/ts-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function needsImportPreservationLogic(

// If value imports shouldn't always be preserved, we'll have to perform import preservation logic
// eslint-disable-next-line @typescript-eslint/no-deprecated
const preserveValueImports = Boolean(compilerOptions.preserveValueImports) || Boolean(compilerOptions.verbatimModuleSyntax);
const preserveValueImports = Boolean(compilerOptions.preserveValueImports);
if (!Boolean(preserveValueImports)) return true;

// Only TypeScript v4.5 and newer supports the `preserValueImports` Compiler option
Expand Down
48 changes: 48 additions & 0 deletions test/verbatim-module-syntax.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {generateCustomTransformerResult} from "./setup/setup-custom-transformer.js";
import {generateTransformResult} from "./setup/setup-transform.js";
import {formatCode} from "./util/format-code.js";
import {test} from "./util/test-runner.js";
Expand Down Expand Up @@ -29,3 +30,50 @@ test("Relevant type-only imports for DIContainer are preserved, but replaced, un
`)
);
});

test("Relevant type-only imports for DIContainer are preserved, but replaced, under verbatim module syntax. #2", ">3.7", (_, {typescript, useProgram}) => {
const bundle = generateCustomTransformerResult(
[
{
entry: true,
fileName: "index.ts",
text: `
import {DIContainer} from "@wessberg/di";
import type {IFoo, Foo} from "./foo";
const container = new DIContainer();
container.registerSingleton<IFoo, Foo>();
`
},
{
entry: false,
fileName: "foo.ts",
text: `
export interface IFoo {}
export class Foo implements IFoo {}
`
}
],
{
typescript,
useProgram,
compilerOptions: {
sourceMap: false,
verbatimModuleSyntax: true
}
}
);
const file = bundle.find(({fileName}) => fileName.includes("index.js"))!;

console.log(formatCode(file.text));

assert.deepEqual(
formatCode(file.text),
formatCode(`\
import { Foo } from "./foo";
import { DIContainer } from "@wessberg/di";
const container = new DIContainer();
container.registerSingleton(undefined, { identifier: \`IFoo\`, implementation: Foo });
`)
);
});

0 comments on commit 83dd1ee

Please sign in to comment.