Skip to content

Support root-level attributes, close #32 #36

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

Merged
merged 1 commit into from
Jan 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/babel-plugin-transform-vue-jsx/src/index.js
Original file line number Diff line number Diff line change
@@ -158,6 +158,9 @@ const parseAttributeJSXAttribute = (t, path, attributes, tagName, elementType) =
} else {
name = namePath.get('name').node
}
if (prefixes.includes(name) && t.isJSXExpressionContainer(path.get('value'))) {
return t.JSXSpreadAttribute(t.objectExpression([t.objectProperty(t.stringLiteral(name), path.get('value').node.expression)]))
}

;[name, ...modifiers] = name.split('_')
;[name, argument] = name.split(':')
@@ -233,16 +236,24 @@ const parseAttributeJSXSpreadAttribute = (t, path, attributes, attributesArray)
* @param t
* @param paths Array<JSXAttribute | JSXSpreadAttribute>
* @param tag Identifier | StringLiteral | MemberExpression
* @param openingElementPath JSXOpeningElement
* @returns Array<Expression>
*/
const getAttributes = (t, paths, tag) => {
const getAttributes = (t, paths, tag, openingElementPath) => {
const attributesArray = []
let attributes = {}

const { tagName, canContainDomProps, elementType } = parseMagicDomPropsInfo(t, paths, tag)
paths.forEach(path => {
if (t.isJSXAttribute(path)) {
parseAttributeJSXAttribute(t, path, attributes, tagName, elementType)
const possibleSpreadNode = parseAttributeJSXAttribute(t, path, attributes, tagName, elementType)
if (possibleSpreadNode) {
openingElementPath.node.attributes.push(possibleSpreadNode)
const attributePaths = openingElementPath.get('attributes')
const lastAttributePath = attributePaths[attributePaths.length - 1]
attributes = parseAttributeJSXSpreadAttribute(t, lastAttributePath, attributes, attributesArray)
lastAttributePath.remove()
}
return
}
/* istanbul ignore else */
@@ -328,7 +339,8 @@ const transformAttributes = (t, attributes) =>
const transformJSXElement = (t, path) => {
const tag = getTag(t, path.get('openingElement'))
const children = getChildren(t, path.get('children'))
const attributes = getAttributes(t, path.get('openingElement.attributes'), tag)
const openingElementPath = path.get('openingElement')
const attributes = getAttributes(t, openingElementPath.get('attributes'), tag, openingElementPath)

const args = [tag]
if (attributes) {
15 changes: 15 additions & 0 deletions packages/babel-plugin-transform-vue-jsx/test/snapshot.js
Original file line number Diff line number Diff line change
@@ -301,6 +301,21 @@ render(h => h("div", _mergeJSXProps([{}, spread, {
}
});`,
},
{
name: 'Root attribute',
from: `<MyComponent propsProp1="foo" props={{ prop1: 'alpha', prop2: 'beta' }} />`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming propsProp1="foo" is same as props={ prop1: 'foo' }.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the generated code depend on prop order?

<MyComponent props={{ prop1: 'alpha', prop2: 'beta' }} propsProp1="foo" />

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@znck, yes, it will be merged in the end to something that behaves like:

{
  ...{ prop1: 'alpha', prop2: 'beta' }
  ...{ prop1: 'foo' }
}

Just like it normally would work if you declare a JS object with same key twice...

to: `import _mergeJSXProps from "@vue/babel-helper-vue-jsx-merge-props";
h("MyComponent", _mergeJSXProps([{
"props": {
"prop1": "foo"
}
}, {
"props": {
prop1: 'alpha',
prop2: 'beta'
}
}]));`,
},
]

tests.forEach(({ name, from, to }) => test(name, async t => t.is(await transpile(from), to)))