Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix: prevent inject duplicate h variable #298

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
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
36 changes: 35 additions & 1 deletion 36 packages/babel-sugar-inject-h/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@ const isInsideJSXExpression = (t, path) => {
return isInsideJSXExpression(t, path.parentPath)
}

/**
* check if has Defined H Variable And assigned $createElement in method or arguments[0] in render
* @param t
* @param path ObjectMethod | ClassMethod
* @returns boolean
*/
const hasDefinedHVariable = (t, path) => {
let result = {
hasDefined: false
}

const inRender = path.node.key.name === 'render'

path.traverse(
{
VariableDeclarator(path, state) {
if (state.hasDefined || path.node.id.name !== 'h') {
return
}

const { init } = path.node
state.hasDefined = t.isMemberExpression(init) &&
(inRender ?
t.isIdentifier(init.object) && init.object.name === 'arguments' && t.isNumericLiteral(init.property) && init.property.value === 0 :
t.isThisExpression(init.object) && t.isIdentifier(init.property) && init.property.name === '$createElement'
)
}
},
result
)

return result.hasDefined
}

export default babel => {
const t = babel.types

Expand All @@ -57,7 +91,7 @@ export default babel => {
Program(path1) {
path1.traverse({
'ObjectMethod|ClassMethod'(path) {
if (firstParamIsH(t, path) || !hasJSX(t, path) || isInsideJSXExpression(t, path)) {
if (firstParamIsH(t, path) || !hasJSX(t, path) || isInsideJSXExpression(t, path) || hasDefinedHVariable(t, path)) {
return
}

Expand Down
49 changes: 49 additions & 0 deletions 49 packages/babel-sugar-inject-h/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,55 @@ const tests = [

};`,
},
{
name: 'Has Defined H Variable in object method',
from: `const obj = {
getTextNode() {
const h = this.$createElement
return <div>test</div>
}
}`,
to: `const obj = {
getTextNode() {
const h = this.$createElement;
return <div>test</div>;
}

};`
},
{
name: 'Has Defined H Variable in render method',
from: `const obj = {
render() {
const h = arguments[0];
return <div>test</div>
}
}`,
to: `const obj = {
render() {
const h = arguments[0];
return <div>test</div>;
}

};`,
},
{
name: 'Has Defined not H Variable in render method',
from: `const obj = {
render() {
const a = arguments[0];
return <div>test</div>
}
}`,
to: `const obj = {
render() {
const h = arguments[0];
const a = arguments[0];
return <div>test</div>;
}

};`
}
]

tests.forEach(({ name, from, to }) => test(name, async t => t.is(await transpile(from), to)))
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.