Skip to content

Commit

Permalink
Turn namespace assignment error into a warning (#3633)
Browse files Browse the repository at this point in the history
* turn namespace assignment error into a warning

* refactoring
  • Loading branch information
guybedford committed Jun 13, 2020
1 parent d613137 commit 2cf243a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 36 deletions.
23 changes: 12 additions & 11 deletions src/ast/nodes/MemberExpression.ts
Expand Up @@ -258,17 +258,18 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE
}

private disallowNamespaceReassignment() {
if (
this.object instanceof Identifier &&
this.scope.findVariable(this.object.name).isNamespace
) {
return this.context.error(
{
code: 'ILLEGAL_NAMESPACE_REASSIGNMENT',
message: `Illegal reassignment to import '${this.object.name}'`
},
this.start
);
if (this.object instanceof Identifier) {
const variable = this.scope.findVariable(this.object.name);
if (variable.isNamespace) {
variable.include();
this.context.warn(
{
code: 'ILLEGAL_NAMESPACE_REASSIGNMENT',
message: `Illegal reassignment to import '${this.object.name}'`
},
this.start
);
}
}
}

Expand Down
28 changes: 15 additions & 13 deletions test/function/samples/namespace-reassign-import-fails/_config.js
@@ -1,24 +1,26 @@
const path = require('path');

module.exports = {
description: 'disallows reassignments to namespace exports',
error: {
code: 'ILLEGAL_NAMESPACE_REASSIGNMENT',
message: `Illegal reassignment to import 'exp'`,
pos: 31,
watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'foo.js')],
loc: {
file: path.resolve(__dirname, 'main.js'),
line: 3,
column: 0
},
frame: `
description: 'warns for reassignments to namespace exports',
warnings: [
{
code: 'ILLEGAL_NAMESPACE_REASSIGNMENT',
message: `Illegal reassignment to import 'exp'`,
id: path.resolve(__dirname, 'main.js'),
pos: 31,
loc: {
file: path.resolve(__dirname, 'main.js'),
line: 3,
column: 0
},
frame: `
1: import * as exp from './foo';
2:
3: exp.foo = 2;
^
`
}
}
]
};

// test copied from https://github.com/esnext/es6-module-transpiler/tree/master/test/examples/namespace-reassign-import-fails
26 changes: 14 additions & 12 deletions test/function/samples/namespace-update-import-fails/_config.js
Expand Up @@ -2,23 +2,25 @@ const path = require('path');

module.exports = {
description: 'disallows updates to namespace exports',
error: {
code: 'ILLEGAL_NAMESPACE_REASSIGNMENT',
message: `Illegal reassignment to import 'exp'`,
pos: 31,
watchFiles: [path.resolve(__dirname, 'main.js'), path.resolve(__dirname, 'foo.js')],
loc: {
file: path.resolve(__dirname, 'main.js'),
line: 3,
column: 0
},
frame: `
warnings: [
{
code: 'ILLEGAL_NAMESPACE_REASSIGNMENT',
message: `Illegal reassignment to import 'exp'`,
id: path.resolve(__dirname, 'main.js'),
pos: 31,
loc: {
file: path.resolve(__dirname, 'main.js'),
line: 3,
column: 0
},
frame: `
1: import * as exp from './foo';
2:
3: exp['foo']++;
^
`
}
}
]
};

// test copied from https://github.com/esnext/es6-module-transpiler/tree/master/test/examples/namespace-update-import-fails

0 comments on commit 2cf243a

Please sign in to comment.