Skip to content
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

feat(babel-sugar-inject-h): setup method support #76

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion packages/babel-sugar-inject-h/src/index.js
@@ -1,5 +1,7 @@
import syntaxJsx from '@babel/plugin-syntax-jsx'

const entry = 'vue-function-api'

/**
* Check if first parameter is `h`
* @param t
Expand Down Expand Up @@ -62,6 +64,7 @@ export default babel => {
}

const isRender = path.node.key.name === 'render'
const isSetup = path.type === 'ObjectMethod' && path.node.key.name === 'setup'

path
.get('body')
Expand All @@ -70,7 +73,12 @@ export default babel => {
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('h'),
isRender
isSetup
? t.memberExpression(
t.callExpression(t.identifier('require'), [t.stringLiteral(entry)]),
t.identifier('createElement'),
)
: isRender
? t.memberExpression(t.identifier('arguments'), t.numericLiteral(0), true)
: t.memberExpression(t.thisExpression(), t.identifier('$createElement')),
),
Expand Down
16 changes: 16 additions & 0 deletions packages/babel-sugar-inject-h/test/test.js
Expand Up @@ -101,6 +101,22 @@ const tests = [
return <div>test</div>;
}

};`,
},
{
name: 'Simple injection in object setup methods',
from: `const obj = {
setup() {
return () => <div>test</div>
}
}`,
to: `const obj = {
setup() {
const h = require("vue-function-api").createElement;

return () => <div>test</div>;
}

};`,
},
]
Expand Down