codemodsquad / astx

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

Unexpected error #10

Closed cowboy closed 2 years ago

cowboy commented 2 years ago

I'm using 2.0.0-beta.5. If I have this file.js

module.exports = () => {
  return global.rules.find(
    (rule) =>
      typeof rule.loader === 'string' && /babel-loader/.test(rule.loader)
  )
}

And run this bash script

#!/usr/bin/env bash

read -r -d '' find <<'EOF'
module.exports = () => {
  $$functionBody
}
EOF

read -r -d '' replace <<'EOF'
module.exports = {
  fn() {
    $$functionBody
  }
}
EOF

astx -y -f "$find" -r "$replace" file.js

I get this error


==========================================
file.js
==========================================
TypeError: Cannot read property 'raw' of undefined
    at genericPrintNoParens (/path/to/astx/lib/node_modules/astx/node_modules/recast/lib/printer.js:677:47)
    at genericPrint (/path/to/astx/lib/node_modules/astx/node_modules/recast/lib/printer.js:123:30)
    at print (/path/to/astx/lib/node_modules/astx/node_modules/recast/lib/printer.js:77:15)
    at /path/to/astx/lib/node_modules/astx/node_modules/recast/lib/printer.js:49:41
    at Object.printComments (/path/to/astx/lib/node_modules/astx/node_modules/recast/lib/comments.js:281:22)
    at print (/path/to/astx/lib/node_modules/astx/node_modules/recast/lib/printer.js:55:31)
    at /path/to/astx/lib/node_modules/astx/node_modules/recast/lib/printer.js:49:41
    at FastPath.call (/path/to/astx/lib/node_modules/astx/node_modules/recast/lib/fast-path.js:125:18)
    at genericPrintNoParens (/path/to/astx/lib/node_modules/astx/node_modules/recast/lib/printer.js:200:29)
    at genericPrint (/path/to/astx/lib/node_modules/astx/node_modules/recast/lib/printer.js:123:30)
0 files changed
0 files unchanged
1 file errored```
cowboy commented 2 years ago

I also get the Cannot read property 'raw' of undefined error with this file, so maybe it's a general problem with RegExp parsing:

module.exports = () => {
  const re = /foo/
}
jedwards1211 commented 2 years ago

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

The release is available on:

Your semantic-release bot :package::rocket:

jedwards1211 commented 2 years ago

Sorry about that!

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: