Browse Source

Optimize dynamic access of namespace imports with literal keys

This doesn’t fully work yet since it causes the erroneous-nested-member-expression test to regress. Any thoughts how to make these play nice together?
semi-dynamic-namespace-imports
Brian Donovan 9 years ago
parent
commit
1bee1027d6
  1. 4
      src/Declaration.js
  2. 6
      src/ast/flatten.js
  3. 7
      src/ast/isReference.js
  4. 3
      test/form/namespace-optimization-computed-string/_config.js
  5. 7
      test/form/namespace-optimization-computed-string/_expected/amd.js
  6. 5
      test/form/namespace-optimization-computed-string/_expected/cjs.js
  7. 3
      test/form/namespace-optimization-computed-string/_expected/es.js
  8. 8
      test/form/namespace-optimization-computed-string/_expected/iife.js
  9. 11
      test/form/namespace-optimization-computed-string/_expected/umd.js
  10. 3
      test/form/namespace-optimization-computed-string/bar.js
  11. 3
      test/form/namespace-optimization-computed-string/foo.js
  12. 3
      test/form/namespace-optimization-computed-string/main.js
  13. 1
      test/form/namespace-optimization-computed-string/quux.js

4
src/Declaration.js

@ -200,8 +200,8 @@ export class SyntheticNamespaceDeclaration {
// the reference by pointing directly to `bar` // the reference by pointing directly to `bar`
if ( reference.parts.length ) { if ( reference.parts.length ) {
const ref = reference.parts.shift(); const ref = reference.parts.shift();
reference.name = ref.name; reference.name = ref.type === 'Literal' ? ref.value : ref.name;
reference.end = ref.end; reference.end = reference.node.end;
const original = this.originals[ reference.name ]; const original = this.originals[ reference.name ];

6
src/ast/flatten.js

@ -1,7 +1,11 @@
export default function flatten ( node ) { export default function flatten ( node ) {
const parts = []; const parts = [];
while ( node.type === 'MemberExpression' ) { 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 ); parts.unshift( node.property.name );
node = node.object; node = node.object;

7
src/ast/isReference.js

@ -1,6 +1,11 @@
export default function isReference ( node, parent ) { export default function isReference ( node, parent ) {
if ( node.type === 'MemberExpression' ) { 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' ) { if ( node.type === 'Identifier' ) {

3
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'
};

7
test/form/namespace-optimization-computed-string/_expected/amd.js

@ -0,0 +1,7 @@
define(function () { 'use strict';
function a () {}
a();
});

5
test/form/namespace-optimization-computed-string/_expected/cjs.js

@ -0,0 +1,5 @@
'use strict';
function a () {}
a();

3
test/form/namespace-optimization-computed-string/_expected/es.js

@ -0,0 +1,3 @@
function a () {}
a();

8
test/form/namespace-optimization-computed-string/_expected/iife.js

@ -0,0 +1,8 @@
(function () {
'use strict';
function a () {}
a();
}());

11
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();
}));

3
test/form/namespace-optimization-computed-string/bar.js

@ -0,0 +1,3 @@
import * as quux from './quux.js';
export { quux };

3
test/form/namespace-optimization-computed-string/foo.js

@ -0,0 +1,3 @@
import * as bar from './bar.js';
export { bar };

3
test/form/namespace-optimization-computed-string/main.js

@ -0,0 +1,3 @@
import * as foo from './foo.js';
foo['bar']['quux']['a']();

1
test/form/namespace-optimization-computed-string/quux.js

@ -0,0 +1 @@
export function a () {}
Loading…
Cancel
Save