Skip to content

Commit 565235a

Browse files
authored
Merge pull request #27 from vuejs/feat/component-in-scope
Treat string as component if declared in scope
2 parents f96600c + 12a311e commit 565235a

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

packages/babel-plugin-transform-vue-jsx/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
"@babel/helper-module-imports": "^7.0.0",
3939
"@babel/plugin-syntax-jsx": "^7.2.0",
4040
"@vue/babel-helper-vue-jsx-merge-props": "^0.1.0",
41-
"lodash.kebabcase": "^4.1.1"
41+
"html-tags": "^2.0.0",
42+
"lodash.kebabcase": "^4.1.1",
43+
"svg-tags": "^1.0.0"
4244
},
4345
"peerDependencies": {
4446
"@vue/babel-helper-vue-jsx-merge-props": "^0.1.0"

packages/babel-plugin-transform-vue-jsx/src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import syntaxJsx from '@babel/plugin-syntax-jsx'
22
import { addDefault } from '@babel/helper-module-imports'
33
import kebabcase from 'lodash.kebabcase'
4+
import htmlTags from 'html-tags'
5+
import svgTags from 'svg-tags'
46

57
const xlinkRE = /^xlink([A-Z])/
68
const rootAttributes = ['staticClass', 'class', 'style', 'key', 'ref', 'refInFor', 'slot', 'scopedSlots', 'model']
@@ -40,7 +42,7 @@ const getTag = (t, path) => {
4042
const namePath = path.get('name')
4143
if (t.isJSXIdentifier(namePath)) {
4244
const name = namePath.get('name').node
43-
if (name[0] > 'A' && name[0] < 'Z') {
45+
if (path.scope.hasBinding(name) && !htmlTags.includes(name) && !svgTags.includes(name)) {
4446
return t.identifier(name)
4547
} else {
4648
return t.stringLiteral(name)

packages/babel-plugin-transform-vue-jsx/test/snapshot.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@ const tests = [
2525
to: `render(h => h("div", ["test"]));`,
2626
},
2727
{
28-
name: 'Uppercase component',
29-
from: `render(h => <Div>test</Div>)`,
30-
to: `render(h => h(Div, ["test"]));`,
28+
name: 'HTML tag if variable in scope',
29+
from: `const div = {}; render(h => <div>test</div>)`,
30+
to: `const div = {};
31+
render(h => h("div", ["test"]));`,
32+
},
33+
{
34+
name: 'Tag & Component',
35+
from: `const Alpha = {}; render(h => [<Alpha>test</Alpha>, <Beta>test</Beta>])`,
36+
to: `const Alpha = {};
37+
render(h => [h(Alpha, ["test"]), h("Beta", ["test"])]);`,
3138
},
3239
{
3340
name: 'MemberExpression component',

packages/babel-plugin-transform-vue-jsx/yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2922,6 +2922,11 @@ html-encoding-sniffer@^1.0.2:
29222922
dependencies:
29232923
whatwg-encoding "^1.0.1"
29242924

2925+
html-tags@^2.0.0:
2926+
version "2.0.0"
2927+
resolved "https://registry.yarnpkg.com/html-tags/-/html-tags-2.0.0.tgz#10b30a386085f43cede353cc8fa7cb0deeea668b"
2928+
integrity sha1-ELMKOGCF9Dzt41PMj6fLDe7qZos=
2929+
29252930
http-signature@~1.2.0:
29262931
version "1.2.0"
29272932
resolved "https://registry.yarnpkg.com/http-signature/-/http-signature-1.2.0.tgz#9aecd925114772f3d95b65a60abb8f7c18fbace1"
@@ -5249,6 +5254,11 @@ supports-color@^5.0.0, supports-color@^5.3.0:
52495254
dependencies:
52505255
has-flag "^3.0.0"
52515256

5257+
svg-tags@^1.0.0:
5258+
version "1.0.0"
5259+
resolved "https://registry.yarnpkg.com/svg-tags/-/svg-tags-1.0.0.tgz#58f71cee3bd519b59d4b2a843b6c7de64ac04764"
5260+
integrity sha1-WPcc7jvVGbWdSyqEO2x95krAR2Q=
5261+
52525262
symbol-observable@^0.2.2:
52535263
version "0.2.4"
52545264
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-0.2.4.tgz#95a83db26186d6af7e7a18dbd9760a2f86d08f40"

0 commit comments

Comments
 (0)