123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- import { transform } from "../lib/index";
- import Plugin from "../lib/config/plugin";
- describe("traversal path", function() {
- it("replaceWithSourceString", function() {
- const expectCode = "function foo() {}";
- const actualCode = transform(expectCode, {
- cwd: __dirname,
- plugins: [
- new Plugin({
- 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 = transform(expectCode, {
- cwd: __dirname,
- plugins: [
- new Plugin({
- 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 = transform(expectCode, {
- cwd: __dirname,
- plugins: [
- new Plugin({
- 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 = transform(expectCode, {
- cwd: __dirname,
- plugins: [
- new Plugin({
- 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 = transform(expectCode, {
- cwd: __dirname,
- plugins: [
- new Plugin({
- 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 = transform(expectCode, {
- cwd: __dirname,
- plugins: [
- new Plugin({
- 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 = transform(expectCode, {
- cwd: __dirname,
- plugins: [
- new Plugin({
- visitor: {
- ForStatement: function(path) {
- path.get("init").replaceWith({
- type: "Identifier",
- name: "KEY",
- });
- },
- },
- }),
- ],
- }).code;
- expect(actualCode).toBe("for (KEY;;);");
- });
- });
|