Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn namespace assignment error into a warning #3633

Merged
merged 2 commits into from Jun 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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