Skip to content
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

feat(transformer): add defaulted Module::Preserve option #7225

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
3 changes: 2 additions & 1 deletion crates/oxc_transformer/src/options/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::options::babel::BabelModule;
#[non_exhaustive]
pub enum Module {
#[default]
Preserve,
ESM,
CommonJS,
}
Expand All @@ -36,7 +37,7 @@ impl TryFrom<BabelModule> for Module {
fn try_from(value: BabelModule) -> Result<Self, Self::Error> {
match value {
BabelModule::Commonjs => Ok(Self::CommonJS),
BabelModule::Auto | BabelModule::Boolean(false) => Ok(Self::ESM),
BabelModule::Auto | BabelModule::Boolean(false) => Ok(Self::Preserve),
_ => Err(Error::msg(format!("{value:?} module is not implemented."))),
}
}
Expand Down
19 changes: 19 additions & 0 deletions napi/transform/test/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ describe('transform', () => {
});
});

describe('modules', () => {
it('should transform export = and import ', () => {
const code = `
export = function foo (): void {}
import bar = require('bar')
`;
const ret = oxc.transform('test.ts', code, {
typescript: {
declaration: true,
},
});
assert.deepEqual(ret, {
code: 'module.exports = function foo() {};\nconst bar = require("bar");\n',
declaration: 'declare const _default: () => void;\nexport = _default;\n',
errors: [],
});
});
});

describe('react refresh plugin', () => {
const code = `import { useState } from "react";
export const App = () => {
Expand Down
74 changes: 65 additions & 9 deletions tasks/coverage/snapshots/semantic_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,14 @@ after transform: ["Y", "foo"]
rebuilt : []

tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/import-require/input.js
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
semantic error: Missing SymbolId: "x"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/estree/typescript/literals/input.js
semantic error: Bindings mismatch:
Expand Down Expand Up @@ -1100,10 +1107,14 @@ after transform: ScopeId(0): [ScopeId(1), ScopeId(2), ScopeId(3), ScopeId(4), Sc
rebuilt : ScopeId(0): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/equals/input.ts
semantic error: Export assignment cannot be used when targeting ECMAScript modules.
semantic error: Reference flags mismatch for "f":
after transform: ReferenceId(0): ReferenceFlags(Read | Type)
rebuilt : ReferenceId(1): ReferenceFlags(Read)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/equals-in-unambiguous/input.ts
semantic error: Export assignment cannot be used when targeting ECMAScript modules.
semantic error: Reference flags mismatch for "f":
after transform: ReferenceId(0): ReferenceFlags(Read | Type)
rebuilt : ReferenceId(1): ReferenceFlags(Read)

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/export/export-type/input.ts
semantic error: Bindings mismatch:
Expand Down Expand Up @@ -1223,10 +1234,24 @@ after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require/input.ts
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
semantic error: Missing SymbolId: "a"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/equals-require-in-unambiguous/input.ts
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
semantic error: Missing SymbolId: "a"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import/input.ts
semantic error: Missing SymbolId: "A"
Expand All @@ -1235,18 +1260,42 @@ after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-require/input.ts
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
semantic error: Missing SymbolId: "a"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-type-as-identifier/input.ts
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
semantic error: Missing SymbolId: "type"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-import-type-require/input.ts
semantic error: Bindings mismatch:
after transform: ScopeId(0): ["a"]
rebuilt : ScopeId(0): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/export-named-import-require/input.ts
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
semantic error: Missing SymbolId: "a"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Reference symbol mismatch for "a":
after transform: SymbolId(0) "a"
rebuilt : SymbolId(0) "a"
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/import-default-and-named-id-type/input.ts
semantic error: Bindings mismatch:
Expand All @@ -1269,7 +1318,14 @@ after transform: ScopeId(0): ["a"]
rebuilt : ScopeId(0): []

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/import-type-as-identifier/input.ts
semantic error: Import assignment cannot be used when targeting ECMAScript modules.
semantic error: Missing SymbolId: "type"
Missing ReferenceId: "require"
Binding symbols mismatch:
after transform: ScopeId(0): [SymbolId(0)]
rebuilt : ScopeId(0): [SymbolId(0)]
Unresolved references mismatch:
after transform: []
rebuilt : ["require"]

tasks/coverage/babel/packages/babel-parser/test/fixtures/typescript/import/internal-comments/input.ts
semantic error: Bindings mismatch:
Expand Down
Loading
Loading