From 2eb5c8f0f55b4e746ae628acb0acf40406f4eb28 Mon Sep 17 00:00:00 2001 From: Brian Donovan Date: Fri, 8 Jan 2016 17:35:55 -0800 Subject: [PATCH] Ensure computed property keys are considered references. Fixes #434 --- src/ast/isReference.js | 4 ++-- test/form/computed-properties/_config.js | 6 ++++++ test/form/computed-properties/_expected/amd.js | 9 +++++++++ test/form/computed-properties/_expected/cjs.js | 7 +++++++ test/form/computed-properties/_expected/es6.js | 5 +++++ test/form/computed-properties/_expected/iife.js | 10 ++++++++++ test/form/computed-properties/_expected/umd.js | 13 +++++++++++++ test/form/computed-properties/main.js | 3 +++ 8 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 test/form/computed-properties/_config.js create mode 100644 test/form/computed-properties/_expected/amd.js create mode 100644 test/form/computed-properties/_expected/cjs.js create mode 100644 test/form/computed-properties/_expected/es6.js create mode 100644 test/form/computed-properties/_expected/iife.js create mode 100644 test/form/computed-properties/_expected/umd.js create mode 100644 test/form/computed-properties/main.js diff --git a/src/ast/isReference.js b/src/ast/isReference.js index 98d8df5..0c9a9d7 100644 --- a/src/ast/isReference.js +++ b/src/ast/isReference.js @@ -12,8 +12,8 @@ export default function isReference ( node, parent ) { // TODO is this right? if ( parent.type === 'MemberExpression' ) return parent.computed || node === parent.object; - // disregard the `bar` in { bar: foo } - if ( parent.type === 'Property' && node !== parent.value ) return false; + // disregard the `bar` in `{ bar: foo }`, but keep it in `{ [bar]: foo }` + if ( parent.type === 'Property' ) return parent.computed || node === parent.value; // disregard the `bar` in `class Foo { bar () {...} }` if ( parent.type === 'MethodDefinition' ) return false; diff --git a/test/form/computed-properties/_config.js b/test/form/computed-properties/_config.js new file mode 100644 index 0000000..34cbc26 --- /dev/null +++ b/test/form/computed-properties/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'computed property keys include declarations of referenced identifiers', + options: { + moduleName: 'computedProperties' + } +}; diff --git a/test/form/computed-properties/_expected/amd.js b/test/form/computed-properties/_expected/amd.js new file mode 100644 index 0000000..4fd933f --- /dev/null +++ b/test/form/computed-properties/_expected/amd.js @@ -0,0 +1,9 @@ +define(['exports'], function (exports) { 'use strict'; + + var foo = 'foo'; + + var x = {[foo]: 'bar'}; + + exports.x = x; + +}); \ No newline at end of file diff --git a/test/form/computed-properties/_expected/cjs.js b/test/form/computed-properties/_expected/cjs.js new file mode 100644 index 0000000..22126d8 --- /dev/null +++ b/test/form/computed-properties/_expected/cjs.js @@ -0,0 +1,7 @@ +'use strict'; + +var foo = 'foo'; + +var x = {[foo]: 'bar'}; + +exports.x = x; \ No newline at end of file diff --git a/test/form/computed-properties/_expected/es6.js b/test/form/computed-properties/_expected/es6.js new file mode 100644 index 0000000..6dc09ca --- /dev/null +++ b/test/form/computed-properties/_expected/es6.js @@ -0,0 +1,5 @@ +var foo = 'foo'; + +var x = {[foo]: 'bar'}; + +export { x }; \ No newline at end of file diff --git a/test/form/computed-properties/_expected/iife.js b/test/form/computed-properties/_expected/iife.js new file mode 100644 index 0000000..35b80af --- /dev/null +++ b/test/form/computed-properties/_expected/iife.js @@ -0,0 +1,10 @@ +(function (exports) { + 'use strict'; + + var foo = 'foo'; + + var x = {[foo]: 'bar'}; + + exports.x = x; + +}((this.computedProperties = {}))); \ No newline at end of file diff --git a/test/form/computed-properties/_expected/umd.js b/test/form/computed-properties/_expected/umd.js new file mode 100644 index 0000000..5f7d146 --- /dev/null +++ b/test/form/computed-properties/_expected/umd.js @@ -0,0 +1,13 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (factory((global.computedProperties = {}))); +}(this, function (exports) { 'use strict'; + + var foo = 'foo'; + + var x = {[foo]: 'bar'}; + + exports.x = x; + +})); \ No newline at end of file diff --git a/test/form/computed-properties/main.js b/test/form/computed-properties/main.js new file mode 100644 index 0000000..65bb97c --- /dev/null +++ b/test/form/computed-properties/main.js @@ -0,0 +1,3 @@ +var foo = 'foo'; + +export var x = {[foo]: 'bar'};