/
githubmirror
/
babel
Обзор
Документация
Войти
/
githubmirror
/
babel
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/babel-core/test/path.js
184 строки
5 KB
Nicolò Ribaudo
Use `package.json#imports` for browser-specific code (#17756)
31 янв 2026, 01:13
Не верифицирован
31 янв 2026, 01:13
237db3e
Код
Авторство
О чём код?
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;;);"); }); });