From 23f8450497b7545a8c97fbf64e49e5299a2e1455 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Fri, 12 Jun 2020 18:52:25 -0700 Subject: [PATCH 1/2] turn namespace assignment error into a warning --- src/ast/nodes/MemberExpression.ts | 3 +- .../_config.js | 28 ++++++++++--------- .../namespace-update-import-fails/_config.js | 26 +++++++++-------- 3 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/ast/nodes/MemberExpression.ts b/src/ast/nodes/MemberExpression.ts index 8d88d357ef3..9088848f991 100644 --- a/src/ast/nodes/MemberExpression.ts +++ b/src/ast/nodes/MemberExpression.ts @@ -262,7 +262,8 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE this.object instanceof Identifier && this.scope.findVariable(this.object.name).isNamespace ) { - return this.context.error( + this.scope.findVariable(this.object.name).include(); + this.context.warn( { code: 'ILLEGAL_NAMESPACE_REASSIGNMENT', message: `Illegal reassignment to import '${this.object.name}'` diff --git a/test/function/samples/namespace-reassign-import-fails/_config.js b/test/function/samples/namespace-reassign-import-fails/_config.js index 3ab81beb7bd..677971b7ade 100644 --- a/test/function/samples/namespace-reassign-import-fails/_config.js +++ b/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 diff --git a/test/function/samples/namespace-update-import-fails/_config.js b/test/function/samples/namespace-update-import-fails/_config.js index f5d608e7ffd..4c3da7877e6 100644 --- a/test/function/samples/namespace-update-import-fails/_config.js +++ b/test/function/samples/namespace-update-import-fails/_config.js @@ -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 From d433eda99b45a2e520bb9b8010c167de8de6ae84 Mon Sep 17 00:00:00 2001 From: Guy Bedford Date: Sat, 13 Jun 2020 00:37:13 -0700 Subject: [PATCH 2/2] refactoring --- src/ast/nodes/MemberExpression.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/ast/nodes/MemberExpression.ts b/src/ast/nodes/MemberExpression.ts index 9088848f991..7bf2bcd2301 100644 --- a/src/ast/nodes/MemberExpression.ts +++ b/src/ast/nodes/MemberExpression.ts @@ -258,18 +258,18 @@ export default class MemberExpression extends NodeBase implements DeoptimizableE } private disallowNamespaceReassignment() { - if ( - this.object instanceof Identifier && - this.scope.findVariable(this.object.name).isNamespace - ) { - this.scope.findVariable(this.object.name).include(); - this.context.warn( - { - 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 + ); + } } }