-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: order of application in non-macro transforms chain (#96)
* Order of application in non-macro transforms chain * plugins order test and coverage fix for #96
- Loading branch information
Showing
5 changed files
with
133 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// adds "prefix-" to each `id` attribute | ||
const {createMacro} = require('../../') | ||
|
||
module.exports = createMacro(wrapWidget) | ||
|
||
function wrapWidget({references, babel}) { | ||
const {types: t} = babel | ||
references.default.forEach(wrap => { | ||
wrap.parentPath.traverse({ | ||
JSXAttribute(path) { | ||
const name = path.get('name') | ||
if (t.isJSXIdentifier(name) && name.node.name === 'id') { | ||
const value = path.get('value') | ||
if (t.isStringLiteral(value)) | ||
value.replaceWith(t.stringLiteral(`macro-${value.node.value}`)) | ||
} | ||
}, | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// babel-plugin adding `plugin-` prefix to each "id" JSX attribute | ||
module.exports = main | ||
|
||
function main({types: t}) { | ||
return { | ||
visitor: { | ||
// intentionally traversing from Program, | ||
// if it matches JSXAttribute here the issue won't be reproduced | ||
Program(progPath) { | ||
progPath.traverse({ | ||
JSXAttribute(path) { | ||
const name = path.get('name') | ||
if (t.isJSXIdentifier(name) && name.node.name === 'id') { | ||
const value = path.get('value') | ||
if (t.isStringLiteral(value)) | ||
value.replaceWith(t.stringLiteral(`plugin-${value.node.value}`)) | ||
} | ||
}, | ||
}) | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters