Skip to content

Commit

Permalink
fix(require): interop with destructuring transform (#51)
Browse files Browse the repository at this point in the history
The `transform-destructuring` Babel plugin uses the `VariableDeclaration`
visitor which comes before `CallExpression`. So by the time `macros` got
to the `require` call it has already been transformed and no longer looks
like a `require`. Use `VariableDeclaration` to fix.
  • Loading branch information
haltcase authored and Kent C. Dodds committed Jan 27, 2018
1 parent 9438118 commit ec116e4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@
"doc",
"plugin"
]
},
{
"login": "citycide",
"name": "Bo Lingen",
"avatar_url": "https://avatars2.githubusercontent.com/u/16605186?v=4",
"profile": "https://github.com/citycide",
"contributions": [
"code"
]
}
]
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Enables zero-config, importable babel plugins
[![downloads][downloads-badge]][npmchart]
[![MIT License][license-badge]][license]

[![All Contributors](https://img.shields.io/badge/all_contributors-10-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-11-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Donate][donate-badge]][donate]
[![Code of Conduct][coc-badge]][coc]
Expand Down Expand Up @@ -336,7 +336,7 @@ Thanks goes to these people ([emoji key][emojis]):
<!-- prettier-ignore -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/babel-plugin-macros/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/18808?v=3" width="100px;"/><br /><sub><b>Sunil Pai</b></sub>](https://github.com/threepointone)<br />[🤔](#ideas-threepointone "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/1341513?v=3" width="100px;"/><br /><sub><b>Stephen Scott</b></sub>](http://suchipi.com/)<br />[💬](#question-suchipi "Answering Questions") [📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=suchipi "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/767261?v=4" width="100px;"/><br /><sub><b>Michiel Dral</b></sub>](http://twitter.com/dralletje)<br />[🤔](#ideas-dralletje "Ideas, Planning, & Feedback") | [<img src="https://avatars2.githubusercontent.com/u/662750?v=4" width="100px;"/><br /><sub><b>Kye Hohenberger</b></sub>](https://github.com/tkh44)<br />[🤔](#ideas-tkh44 "Ideas, Planning, & Feedback") | [<img src="https://avatars1.githubusercontent.com/u/11481355?v=4" width="100px;"/><br /><sub><b>Mitchell Hamilton</b></sub>](https://hamil.town)<br />[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=mitchellhamilton "Code") [⚠️](https://github.com/kentcdodds/babel-plugin-macros/commits?author=mitchellhamilton "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=wKovacs64 "Documentation") |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| [<img src="https://avatars3.githubusercontent.com/u/1903016?v=4" width="100px;"/><br /><sub><b>Brian Pedersen</b></sub>](https://github.com/PiereDome)<br />[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=PiereDome "Code") [📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=PiereDome "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/4495237?v=4" width="100px;"/><br /><sub><b>Andrew Palm</b></sub>](https://github.com/apalm)<br />[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=apalm "Code") | [<img src="https://avatars1.githubusercontent.com/u/1527371?v=4" width="100px;"/><br /><sub><b>Michael Hsu</b></sub>](https://michaelhsu.tw/)<br />[📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=evenchange4 "Documentation") [🔌](#plugin-evenchange4 "Plugin/utility libraries") |
| [<img src="https://avatars3.githubusercontent.com/u/1903016?v=4" width="100px;"/><br /><sub><b>Brian Pedersen</b></sub>](https://github.com/PiereDome)<br />[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=PiereDome "Code") [📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=PiereDome "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/4495237?v=4" width="100px;"/><br /><sub><b>Andrew Palm</b></sub>](https://github.com/apalm)<br />[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=apalm "Code") | [<img src="https://avatars1.githubusercontent.com/u/1527371?v=4" width="100px;"/><br /><sub><b>Michael Hsu</b></sub>](https://michaelhsu.tw/)<br />[📖](https://github.com/kentcdodds/babel-plugin-macros/commits?author=evenchange4 "Documentation") [🔌](#plugin-evenchange4 "Plugin/utility libraries") | [<img src="https://avatars2.githubusercontent.com/u/16605186?v=4" width="100px;"/><br /><sub><b>Bo Lingen</b></sub>](https://github.com/citycide)<br />[💻](https://github.com/kentcdodds/babel-plugin-macros/commits?author=citycide "Code") |

<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down
68 changes: 37 additions & 31 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,38 +71,44 @@ function macrosPlugin(babel) {
})
path.remove()
},
CallExpression(path, state) {
const isMacros = looksLike(path, {
node: {
callee: {
type: 'Identifier',
name: 'require',
VariableDeclaration(path, state) {
const isMacros = child =>
looksLike(child, {
node: {
init: {
callee: {
type: 'Identifier',
name: 'require',
},
arguments: args =>
args.length === 1 && macrosRegex.test(args[0].value),
},
},
arguments: args =>
args.length === 1 && macrosRegex.test(args[0].value),
},
parent: {
type: 'VariableDeclarator',
},
})
if (!isMacros) {
return
}
const imports = path.parent.id.name
? [{localName: path.parent.id.name, importedName: 'default'}]
: path.parent.id.properties.map(property => ({
localName: property.value.name,
importedName: property.key.name,
}))
const source = path.node.arguments[0].value
applyMacros({
path,
imports,
source,
state,
babel,
})
path.parentPath.remove()
})

path
.get('declarations')
.filter(isMacros)
.forEach(child => {
const imports = child.node.id.name
? [{localName: child.node.id.name, importedName: 'default'}]
: child.node.id.properties.map(property => ({
localName: property.value.name,
importedName: property.key.name,
}))

const call = child.get('init')
const source = call.node.arguments[0].value
applyMacros({
path: call,
imports,
source,
state,
babel,
})

child.remove()
})
},
},
}
Expand Down

0 comments on commit ec116e4

Please sign in to comment.