-
-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow dynamic properties in replaceAttrValues option
Closes #205
- Loading branch information
Showing
9 changed files
with
153 additions
and
44 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
48 changes: 35 additions & 13 deletions
48
packages/babel-plugin-replace-jsx-attribute-value/src/index.js
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 |
---|---|---|
@@ -1,15 +1,37 @@ | ||
const plugin = ({ types: t }, opts) => ({ | ||
visitor: { | ||
JSXAttribute(path) { | ||
const value = path.get('value') | ||
if (!value.isStringLiteral()) return | ||
|
||
Object.keys(opts.values).forEach(key => { | ||
if (!value.isStringLiteral({ value: key })) return | ||
value.replaceWith(t.stringLiteral(opts.values[key])) | ||
}) | ||
const addJSXAttribute = ({ types: t, template }, opts) => { | ||
function getAttributeValue(value, literal) { | ||
if (typeof value === 'string' && literal) { | ||
return t.jsxExpressionContainer(template.ast(value).expression) | ||
} | ||
|
||
if (typeof value === 'string') { | ||
return t.stringLiteral(value) | ||
} | ||
|
||
if (typeof value === 'boolean') { | ||
return t.jsxExpressionContainer(t.booleanLiteral(value)) | ||
} | ||
|
||
if (typeof value === 'number') { | ||
return t.jsxExpressionContainer(t.numericLiteral(value)) | ||
} | ||
|
||
return null | ||
} | ||
|
||
return { | ||
visitor: { | ||
JSXAttribute(path) { | ||
const valuePath = path.get('value') | ||
if (!valuePath.isStringLiteral()) return | ||
|
||
opts.values.forEach(({ value, newValue, literal }) => { | ||
if (!valuePath.isStringLiteral({ value })) return | ||
valuePath.replaceWith(getAttributeValue(newValue, literal)) | ||
}) | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
} | ||
|
||
export default plugin | ||
export default addJSXAttribute |
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
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
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