-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix rewriting of components for custom elements #2101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
11b96d3
1c86a45
f1205fe
389b5a5
135aa2d
0553e0c
8f5fbe0
a3bd1de
2ec33fa
5878dd8
451a76f
e547aa8
7572cdc
604e6db
24fb34d
d051f7c
f7aea66
55414b4
a9d93b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,6 +72,8 @@ export function recmaJsxRewrite(options = {}) { | |
| let createErrorHelper | ||
| /** @type {Scope|null} */ | ||
| let currentScope | ||
| /** @type {Map<string | number, string>} */ | ||
| const idToInvalidComponentName = new Map() | ||
|
|
||
| walk(tree, { | ||
| enter(_node) { | ||
|
|
@@ -193,16 +195,24 @@ export function recmaJsxRewrite(options = {}) { | |
| fnScope.tags.push(id) | ||
| } | ||
|
|
||
| node.openingElement.name = toJsxIdOrMemberExpression([ | ||
| '_components', | ||
| id | ||
| ]) | ||
| /** @type {(string | number)[]} */ | ||
| let jsxIdExpression = ['_components', id] | ||
| if (isIdentifierName(id) === false) { | ||
| let invalidComponentName = idToInvalidComponentName.get(id) | ||
| if (invalidComponentName === undefined) { | ||
| invalidComponentName = `_component${idToInvalidComponentName.size}` | ||
| idToInvalidComponentName.set(id, invalidComponentName) | ||
| } | ||
|
|
||
| jsxIdExpression = [invalidComponentName] | ||
| } | ||
|
|
||
| node.openingElement.name = | ||
| toJsxIdOrMemberExpression(jsxIdExpression) | ||
|
|
||
| if (node.closingElement) { | ||
| node.closingElement.name = toJsxIdOrMemberExpression([ | ||
| '_components', | ||
| id | ||
| ]) | ||
| node.closingElement.name = | ||
| toJsxIdOrMemberExpression(jsxIdExpression) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -259,7 +269,11 @@ export function recmaJsxRewrite(options = {}) { | |
| /** @type {Array<Statement>} */ | ||
| const statements = [] | ||
|
|
||
| if (defaults.length > 0 || actual.length > 0) { | ||
| if ( | ||
| defaults.length > 0 || | ||
| actual.length > 0 || | ||
| idToInvalidComponentName.size > 0 | ||
| ) { | ||
| if (providerImportSource) { | ||
| importProvider = true | ||
| parameters.push({ | ||
|
|
@@ -344,6 +358,27 @@ export function recmaJsxRewrite(options = {}) { | |
| componentsInit = {type: 'Identifier', name: '_components'} | ||
| } | ||
|
|
||
| for (const [ | ||
| _componentsId, | ||
| _componentName | ||
| ] of idToInvalidComponentName) { | ||
|
||
| // For component IDs that render invalid JSX (ex. <_components.custom-element>), | ||
| // Generate a separate variable to index into `_components` | ||
| // i.e. `const _component0 = _components['custom-element']` | ||
| // `return <_component0>...` | ||
bholmesdev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| declarations.push({ | ||
| type: 'VariableDeclarator', | ||
| id: {type: 'Identifier', name: _componentName}, | ||
| init: { | ||
| type: 'MemberExpression', | ||
| object: {type: 'Identifier', name: '_components'}, | ||
| property: {type: 'Literal', value: _componentsId}, | ||
| computed: true, | ||
| optional: false | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| if (componentsPattern) { | ||
| declarations.push({ | ||
| type: 'VariableDeclarator', | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.