Skip to content

Commit

Permalink
fix: use simple assign for computed properties (fix #13)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuniorTour committed Sep 14, 2021
1 parent 8c3d97e commit b5e8bd1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ function renderCompiler(code, options) {
filename: 'compiledTemplate',
// not enable strict mode, in order to parse WithStatement
sourceType: 'script',
plugins: ['@babel/plugin-proposal-nullish-coalescing-operator', '@babel/plugin-proposal-optional-chaining', '@babel/plugin-transform-block-scoping', '@babel/plugin-transform-destructuring', "@babel/plugin-transform-spread", ["@babel/plugin-proposal-object-rest-spread", {
assumptions: {
setComputedProperties: true
},
plugins: ['@babel/plugin-transform-computed-properties', '@babel/plugin-proposal-nullish-coalescing-operator', '@babel/plugin-proposal-optional-chaining', '@babel/plugin-transform-block-scoping', '@babel/plugin-transform-destructuring', "@babel/plugin-transform-spread", ["@babel/plugin-proposal-object-rest-spread", {
loose: true,
useBuiltIns: true
}], '@babel/plugin-transform-arrow-functions', '@babel/plugin-transform-parameters', parseWithStatementToVm]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@babel/plugin-proposal-optional-chaining": "^7.14.2",
"@babel/plugin-transform-arrow-functions": "^7.14.5",
"@babel/plugin-transform-block-scoping": "^7.14.5",
"@babel/plugin-transform-computed-properties": "^7.14.5",
"@babel/plugin-transform-destructuring": "^7.14.5",
"@babel/plugin-transform-parameters": "^7.14.5",
"@babel/plugin-transform-spread": "^7.14.5",
Expand Down
4 changes: 4 additions & 0 deletions src/renderCompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export function renderCompiler(code, options) {
filename: 'compiledTemplate',
// not enable strict mode, in order to parse WithStatement
sourceType: 'script',
assumptions: {
setComputedProperties: true
},
plugins: [
'@babel/plugin-transform-computed-properties',
'@babel/plugin-proposal-nullish-coalescing-operator',
'@babel/plugin-proposal-optional-chaining',
'@babel/plugin-transform-block-scoping',
Expand Down
10 changes: 10 additions & 0 deletions test/compileTemplate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ test('should use object.assign for object rest spread', () => {
expect(errors.length === 0).toBeTruthy()
expect(render).toMatch(`Object.assign({}, _vm.a) ? _c('h3', [_vm._v("object rest spread")]) : _vm._e()`)
})

test('should use simple assign for computed properties', () => {
// https://github.com/JuniorTour/vue-template-babel-compiler/issues/13
const {ast, render, staticRenderFns, tips, errors} = templateCompiler.compile(
'<div :class="{[`${foo}_bar`]: true}"></div>'
)

expect(errors.length === 0).toBeTruthy()
expect(render).toMatch('class: (_class = {}, _class[`${_vm.foo}_bar`] = true, _class)')
})
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@

"@babel/plugin-transform-computed-properties@^7.14.5":
version "7.14.5"
resolved "https://registry.npmjs.org/@babel/plugin-transform-computed-properties/-/plugin-transform-computed-properties-7.14.5.tgz"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-computed-properties/-/plugin-transform-computed-properties-7.14.5.tgz#1b9d78987420d11223d41195461cc43b974b204f"
integrity sha512-pWM+E4283UxaVzLb8UBXv4EIxMovU4zxT1OPnpHJcmnvyY9QbPPTKZfEj31EUvG3/EQRbYAGaYEUZ4yWOBC2xg==
dependencies:
"@babel/helper-plugin-utils" "^7.14.5"
Expand Down

0 comments on commit b5e8bd1

Please sign in to comment.