codemodsquad / astx

Super powerful structural search and replace for JavaScript and TypeScript to automate your refactoring
MIT License
92 stars 6 forks source link

Duplicate code appearing in output #9

Closed cowboy closed 2 years ago

cowboy commented 2 years ago

I'm not sure if this is a bug or if I'm just using it incorrectly, but if I have

test.js

/* eslint-disable no-console */

const testLib = require('test-lib')

const funcOne = (foo) => {
  console.log('foo', foo)
  return testLib(foo)
}

const funcTwo = (bar) => {
  console.log('bar', bar)
  return testLib(bar)
}

module.exports = (on, config) => {
  on('event', () => {
    funcOne(config.foo)
    funcTwo(config.bar)
  })
}

const json = {
  "foo": 123,
  "bar": true
}

astx.js

exports.find = `
$$header
module.exports = (on, config) => {
  $$functionBody
}
const json = { $$json }
`

exports.replace = `
const { func } = require('some-module')

$$header

module.exports = func({
  $$json,
  events: {
    setupEvents(on, config) {
      $$functionBody
    }
  }
})
`

The output of astx -y test.js is this (everything before module.exports from the source file seems to be at the top of the output)

/* eslint-disable no-console */

const testLib = require('test-lib')

const funcOne = (foo) => {
  console.log('foo', foo)
  return testLib(foo)
}

const funcTwo = (bar) => {
  console.log('bar', bar)
  return testLib(bar)
}

const {
  func
} = require("some-module");

/* eslint-disable no-console */
const testLib = require("test-lib");

const funcOne = foo => {
  console.log("foo", foo);
  return testLib(foo);
};

const funcTwo = bar => {
  console.log("bar", bar);
  return testLib(bar);
};

module.exports = func({
  "foo": 123,
  "bar": true,
  events: {
    setupEvents(on, config) {
      on("event", () => {
        funcOne(config.foo);
        funcTwo(config.bar);
      });
    }
  }
});

But I was expecting something more like this:

const {
  func
} = require("some-module");

/* eslint-disable no-console */
const testLib = require("test-lib");

const funcOne = foo => {
  console.log("foo", foo);
  return testLib(foo);
};

const funcTwo = bar => {
  console.log("bar", bar);
  return testLib(bar);
};

module.exports = func({
  "foo": 123,
  "bar": true,
  events: {
    setupEvents(on, config) {
      on("event", () => {
        funcOne(config.foo);
        funcTwo(config.bar);
      });
    }
  }
});
jedwards1211 commented 2 years ago

yup looks like a bug, let me investigate

jedwards1211 commented 2 years ago

fix will get released to the beta once CI passes

jedwards1211 commented 2 years ago

:tada: This issue has been resolved in version 2.0.0-beta.5 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

jedwards1211 commented 1 year ago

:tada: This issue has been resolved in version 2.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: