Browse Source

Added missing test for dynamic namespace lookups

gh-109
Oskar Segersvärd 9 years ago
parent
commit
da828f31f8
  1. 11
      src/Statement.js
  2. 3
      test/function/dynamic-namespace-lookup/_config.js
  3. 2
      test/function/dynamic-namespace-lookup/foo.js
  4. 8
      test/function/dynamic-namespace-lookup/main.js

11
src/Statement.js

@ -172,7 +172,7 @@ export default class Statement {
if ( node._scope ) scope = scope.parent;
// Optimize namespace lookups, which manifests as MemberExpressions.
// Optimize namespace lookups, which manifest as MemberExpressions.
if ( node.type === 'MemberExpression' && ( !currentMemberExpression || node.object === currentMemberExpression ) ) {
currentMemberExpression = node;
@ -196,16 +196,19 @@ export default class Statement {
// Extract the name of the accessed property, from and Identifier or Literal.
// Any eventual Literal value is converted to a string.
const name = node.property.name ||
const name = !node.computed ? node.property.name :
( node.property.type === 'Literal' ? String( node.property.value ) : null );
// If we can't resolve the name being accessed,
// If we can't resolve the name being accessed statically,
// we require the namespace to be dynamically accessible.
//
// // resolvable
// console.log( javascript.keywords.for )
// console.log( javascript.keywords[ 'for' ] )
// console.log( javascript.keywords[ 6 ] )
//
// // unresolvable
// console.log( javascript.keywords[ index ] )
// console.log( javascript.keywords[ 1 + 5 ] )
if ( name === null ) {
namespace.dynamicAccess();
@ -217,6 +220,8 @@ export default class Statement {
const id = namespace.exports.lookup( name );
// If the namespace doesn't define the given name,
// we can throw an error (even for nested namespaces).
if ( !id ) {
throw new Error( `Module doesn't define "${name}"!` );
}

3
test/function/dynamic-namespace-lookup/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'does namespace optimization when possible, but not for dynamic lookups'
};

2
test/function/dynamic-namespace-lookup/foo.js

@ -0,0 +1,2 @@
export var bar = 'bar';
export var baz = 'baz';

8
test/function/dynamic-namespace-lookup/main.js

@ -0,0 +1,8 @@
import * as foo from './foo';
var bar = 'baz';
assert.equal( foo.bar, 'bar' );
assert.equal( foo.baz, 'baz' );
assert.equal( foo[ bar ], 'baz' );
Loading…
Cancel
Save