Skip to content
Open
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
15 changes: 15 additions & 0 deletions src/__tests__/babel/no-filename.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { transformSync } from "@babel/core";

import plugin from "../../babel/import-plugin";

test("a file transformed without a filename is left as written", () => {
const source = `import { View } from "react-native";`;

const output = transformSync(source, {
configFile: false,
babelrc: false,
plugins: [plugin],
})?.code;

expect(output).toBe(source);
});
2 changes: 1 addition & 1 deletion src/babel/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface PluginOpts {

export interface PluginState {
opts?: PluginOpts;
filename: string;
filename: string | undefined;
}

export function getInteropRequireDefaultSource(
Expand Down
28 changes: 16 additions & 12 deletions src/babel/import-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function ({
}: {
types: BabelTypes;
}): PluginObj<PluginState> {
const processed = new WeakSet();
const processed = new WeakSet<Statement>();

const packageRoot = resolve(
__dirname,
Expand All @@ -44,17 +44,19 @@ export default function ({
name: "Rewrite react-native to react-native-css",
visitor: {
ImportDeclaration(path, state): void {
const { filename } = state;

if (
processed.has(path) ||
filename === undefined ||
processed.has(path.node) ||
isFromThisModule(state.filename)
isFromThisModule(filename)
) {
return;
}

const statements =
handleReactNativeImport(path.node, t, state.filename) ??
handleReactNativeWebImport(path.node, t, state.filename);
handleReactNativeImport(path.node, t, filename) ??
handleReactNativeWebImport(path.node, t, filename);

if (!statements) {
return;
Expand All @@ -67,10 +69,12 @@ export default function ({
path.replaceWithMultiple(statements);
},
VariableDeclaration(path, state): void {
const { filename } = state;

if (
processed.has(path) ||
filename === undefined ||
processed.has(path.node) ||
isFromThisModule(state.filename)
isFromThisModule(filename)
) {
return;
}
Expand Down Expand Up @@ -124,14 +128,14 @@ export default function ({
t,
id.name,
initArg.value,
state.filename,
filename,
) ??
handleReactNativeWebIdentifierRequire(
path,
t,
id.name,
initArg.value,
state.filename,
filename,
);
} else if (
t.isObjectPattern(id) &&
Expand All @@ -144,14 +148,14 @@ export default function ({
t,
id,
initArg.value,
state.filename,
filename,
) ??
handleReactNativeWebObjectPatternRequire(
path,
t,
id,
initArg.value,
state.filename,
filename,
);
} else if (
t.isIdentifier(id) &&
Expand All @@ -168,7 +172,7 @@ export default function ({
id.name,
init,
source,
state.filename,
filename,
);
}

Expand Down