Browse Source

error if namespace is called (#446)

gh-669
Rich-Harris 9 years ago
parent
commit
9d91ecaa71
  1. 2
      src/Declaration.js
  2. 9
      src/utils/error.js
  3. 16
      src/utils/run.js
  4. 12
      test/function/cannot-call-external-namespace/_config.js
  5. 2
      test/function/cannot-call-external-namespace/main.js
  6. 12
      test/function/cannot-call-internal-namespace/_config.js
  7. 1
      test/function/cannot-call-internal-namespace/foo.js
  8. 2
      test/function/cannot-call-internal-namespace/main.js

2
src/Declaration.js

@ -134,6 +134,7 @@ export class SyntheticDefaultDeclaration {
export class SyntheticNamespaceDeclaration {
constructor ( module ) {
this.isNamespace = true;
this.module = module;
this.name = null;
@ -221,6 +222,7 @@ export class ExternalDeclaration {
this.module = module;
this.name = name;
this.isExternal = true;
this.isNamespace = name === '*';
}
addAlias () {

9
src/utils/error.js

@ -0,0 +1,9 @@
export default function error ( props ) {
const err = new Error( props.message );
Object.keys( props ).forEach( key => {
err[ key ] = props[ key ];
});
throw err;
}

16
src/utils/run.js

@ -3,6 +3,8 @@ import modifierNodes, { isModifierNode } from '../ast/modifierNodes.js';
import isReference from '../ast/isReference.js';
import flatten from '../ast/flatten';
import pureFunctions from './pureFunctions.js';
import getLocation from './getLocation.js';
import error from './error.js';
function call ( callee, scope, statement, strongDependencies ) {
while ( callee.type === 'ParenthesizedExpression' ) callee = callee.expression;
@ -11,7 +13,19 @@ function call ( callee, scope, statement, strongDependencies ) {
const declaration = scope.findDeclaration( callee.name ) ||
statement.module.trace( callee.name );
if ( declaration ) return declaration.run( strongDependencies );
if ( declaration ) {
if ( declaration.isNamespace ) {
error({
message: `Cannot call a namespace ('${callee.name}')`,
file: statement.module.id,
pos: callee.start,
loc: getLocation( statement.module.code, callee.start )
});
}
return declaration.run( strongDependencies );
}
return !pureFunctions[ callee.name ];
}

12
test/function/cannot-call-external-namespace/_config.js

@ -0,0 +1,12 @@
var path = require( 'path' );
var assert = require( 'assert' );
module.exports = {
description: 'errors if code calls an external namespace',
error: function ( err ) {
assert.equal( err.message, 'Cannot call a namespace (\'foo\')' );
assert.equal( err.file, path.resolve( __dirname, 'main.js' ) );
assert.equal( err.pos, 28 );
assert.deepEqual( err.loc, { line: 2, column: 0 });
}
};

2
test/function/cannot-call-external-namespace/main.js

@ -0,0 +1,2 @@
import * as foo from 'foo';
foo();

12
test/function/cannot-call-internal-namespace/_config.js

@ -0,0 +1,12 @@
var path = require( 'path' );
var assert = require( 'assert' );
module.exports = {
description: 'errors if code calls an internal namespace',
error: function ( err ) {
assert.equal( err.message, 'Cannot call a namespace (\'foo\')' );
assert.equal( err.file, path.resolve( __dirname, 'main.js' ) );
assert.equal( err.pos, 33 );
assert.deepEqual( err.loc, { line: 2, column: 0 });
}
};

1
test/function/cannot-call-internal-namespace/foo.js

@ -0,0 +1 @@
export const a = 1;

2
test/function/cannot-call-internal-namespace/main.js

@ -0,0 +1,2 @@
import * as foo from './foo.js';
foo();
Loading…
Cancel
Save