Files
Nicolò RibaudoandGitHub 8b840a8568 Use package.json#imports for browser-specific code (#17756)
* Use `package.json#imports` for browser-specific code

* Fix dts bundling

* Support bundling packages with multiple entry points

* Do not inline `process.env.NODE_ENV` in normal builds
2026-01-30 23:13:10 +01:00

185 lines
4.6 KiB
JavaScript

import { transformSync } from "../lib/index.js";
import { fileURLToPath } from "node:url";
import path from "node:path";
const cwd = path.dirname(fileURLToPath(import.meta.url));
describe("traversal path", function () {
it("replaceWithSourceString", function () {
const expectCode = "function foo() {}";
const actualCode = transformSync(expectCode, {
cwd,
plugins: [
() => ({
visitor: {
FunctionDeclaration: function (path) {
path.replaceWithSourceString("console.whatever()");
},
},
}),
],
}).code;
expect(actualCode).toBe("console.whatever();");
});
it("replaceWith (arrow expression body to block statement body)", function () {
const expectCode = "var fn = () => true;";
const actualCode = transformSync(expectCode, {
cwd,
plugins: [
() => ({
visitor: {
ArrowFunctionExpression: function (path) {
path.get("body").replaceWith({
type: "BlockStatement",
body: [
{
type: "ReturnStatement",
argument: {
type: "BooleanLiteral",
value: true,
},
},
],
});
},
},
}),
],
}).code;
expect(actualCode).toBe("var fn = () => {\n return true;\n};");
});
it("replaceWith (arrow block statement body to expression body)", function () {
const expectCode = "var fn = () => { return true; }";
const actualCode = transformSync(expectCode, {
cwd,
plugins: [
() => ({
visitor: {
ArrowFunctionExpression: function (path) {
path.get("body").replaceWith({
type: "BooleanLiteral",
value: true,
});
},
},
}),
],
}).code;
expect(actualCode).toBe("var fn = () => true;");
});
it("replaceWith (for-in left expression to variable declaration)", function () {
const expectCode = "for (KEY in right);";
const actualCode = transformSync(expectCode, {
cwd,
plugins: [
() => ({
visitor: {
ForInStatement: function (path) {
path.get("left").replaceWith({
type: "VariableDeclaration",
kind: "var",
declarations: [
{
type: "VariableDeclarator",
id: {
type: "Identifier",
name: "KEY",
},
},
],
});
},
},
}),
],
}).code;
expect(actualCode).toBe("for (var KEY in right);");
});
it("replaceWith (for-in left variable declaration to expression)", function () {
const expectCode = "for (var KEY in right);";
const actualCode = transformSync(expectCode, {
cwd,
plugins: [
() => ({
visitor: {
ForInStatement: function (path) {
path.get("left").replaceWith({
type: "Identifier",
name: "KEY",
});
},
},
}),
],
}).code;
expect(actualCode).toBe("for (KEY in right);");
});
it("replaceWith (for-loop left expression to variable declaration)", function () {
const expectCode = "for (KEY;;);";
const actualCode = transformSync(expectCode, {
cwd,
plugins: [
() => ({
visitor: {
ForStatement: function (path) {
path.get("init").replaceWith({
type: "VariableDeclaration",
kind: "var",
declarations: [
{
type: "VariableDeclarator",
id: {
type: "Identifier",
name: "KEY",
},
},
],
});
},
},
}),
],
}).code;
expect(actualCode).toBe("for (var KEY;;);");
});
it("replaceWith (for-loop left variable declaration to expression)", function () {
const expectCode = "for (var KEY;;);";
const actualCode = transformSync(expectCode, {
cwd,
plugins: [
() => ({
visitor: {
ForStatement: function (path) {
path.get("init").replaceWith({
type: "Identifier",
name: "KEY",
});
},
},
}),
],
}).code;
expect(actualCode).toBe("for (KEY;;);");
});
});