diff --git a/src/Declaration.js b/src/Declaration.js index c383081..a75253b 100644 --- a/src/Declaration.js +++ b/src/Declaration.js @@ -200,8 +200,8 @@ export class SyntheticNamespaceDeclaration { // the reference by pointing directly to `bar` if ( reference.parts.length ) { const ref = reference.parts.shift(); - reference.name = ref.name; - reference.end = ref.end; + reference.name = ref.type === 'Literal' ? ref.value : ref.name; + reference.end = reference.node.end; const original = this.originals[ reference.name ]; diff --git a/src/ast/flatten.js b/src/ast/flatten.js index 2000c89..abc14e9 100644 --- a/src/ast/flatten.js +++ b/src/ast/flatten.js @@ -1,7 +1,11 @@ export default function flatten ( node ) { const parts = []; while ( node.type === 'MemberExpression' ) { - if ( node.computed ) return null; + if ( node.computed ) { + if ( node.property.type !== 'Literal' || typeof node.property.value !== 'string' ) { + return null; + } + } parts.unshift( node.property.name ); node = node.object; diff --git a/src/ast/isReference.js b/src/ast/isReference.js index c2e6c89..b8ac7e7 100644 --- a/src/ast/isReference.js +++ b/src/ast/isReference.js @@ -1,6 +1,11 @@ export default function isReference ( node, parent ) { if ( node.type === 'MemberExpression' ) { - return !node.computed && isReference( node.object, node ); + if ( node.computed ) { + if ( node.property.type !== 'Literal' || typeof node.property.value !== 'string' ) { + return false + } + } + return isReference( node.object, node ); } if ( node.type === 'Identifier' ) { diff --git a/test/form/namespace-optimization-computed-string/_config.js b/test/form/namespace-optimization-computed-string/_config.js new file mode 100644 index 0000000..2eaeb36 --- /dev/null +++ b/test/form/namespace-optimization-computed-string/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'it does dynamic lookup optimization of internal namespaces for string-literal keys' +}; diff --git a/test/form/namespace-optimization-computed-string/_expected/amd.js b/test/form/namespace-optimization-computed-string/_expected/amd.js new file mode 100644 index 0000000..a244c47 --- /dev/null +++ b/test/form/namespace-optimization-computed-string/_expected/amd.js @@ -0,0 +1,7 @@ +define(function () { 'use strict'; + + function a () {} + + a(); + +}); diff --git a/test/form/namespace-optimization-computed-string/_expected/cjs.js b/test/form/namespace-optimization-computed-string/_expected/cjs.js new file mode 100644 index 0000000..b52a7e5 --- /dev/null +++ b/test/form/namespace-optimization-computed-string/_expected/cjs.js @@ -0,0 +1,5 @@ +'use strict'; + +function a () {} + +a(); diff --git a/test/form/namespace-optimization-computed-string/_expected/es.js b/test/form/namespace-optimization-computed-string/_expected/es.js new file mode 100644 index 0000000..8bee044 --- /dev/null +++ b/test/form/namespace-optimization-computed-string/_expected/es.js @@ -0,0 +1,3 @@ +function a () {} + +a(); diff --git a/test/form/namespace-optimization-computed-string/_expected/iife.js b/test/form/namespace-optimization-computed-string/_expected/iife.js new file mode 100644 index 0000000..206c237 --- /dev/null +++ b/test/form/namespace-optimization-computed-string/_expected/iife.js @@ -0,0 +1,8 @@ +(function () { + 'use strict'; + + function a () {} + + a(); + +}()); diff --git a/test/form/namespace-optimization-computed-string/_expected/umd.js b/test/form/namespace-optimization-computed-string/_expected/umd.js new file mode 100644 index 0000000..67adf52 --- /dev/null +++ b/test/form/namespace-optimization-computed-string/_expected/umd.js @@ -0,0 +1,11 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : + typeof define === 'function' && define.amd ? define(factory) : + (factory()); +}(this, function () { 'use strict'; + + function a () {} + + a(); + +})); \ No newline at end of file diff --git a/test/form/namespace-optimization-computed-string/bar.js b/test/form/namespace-optimization-computed-string/bar.js new file mode 100644 index 0000000..9920490 --- /dev/null +++ b/test/form/namespace-optimization-computed-string/bar.js @@ -0,0 +1,3 @@ +import * as quux from './quux.js'; + +export { quux }; diff --git a/test/form/namespace-optimization-computed-string/foo.js b/test/form/namespace-optimization-computed-string/foo.js new file mode 100644 index 0000000..ec3731e --- /dev/null +++ b/test/form/namespace-optimization-computed-string/foo.js @@ -0,0 +1,3 @@ +import * as bar from './bar.js'; + +export { bar }; diff --git a/test/form/namespace-optimization-computed-string/main.js b/test/form/namespace-optimization-computed-string/main.js new file mode 100644 index 0000000..8dcfe71 --- /dev/null +++ b/test/form/namespace-optimization-computed-string/main.js @@ -0,0 +1,3 @@ +import * as foo from './foo.js'; + +foo['bar']['quux']['a'](); diff --git a/test/form/namespace-optimization-computed-string/quux.js b/test/form/namespace-optimization-computed-string/quux.js new file mode 100644 index 0000000..103a9f0 --- /dev/null +++ b/test/form/namespace-optimization-computed-string/quux.js @@ -0,0 +1 @@ +export function a () {}