From d3f092a071419edb4101bb71da30ba26d7e4d351 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Wed, 27 Apr 2016 15:57:08 -0400 Subject: [PATCH] create new scope for classes, with class expression ID (if any) as sole name. fixes #626 --- src/ast/attachScopes.js | 4 ++-- .../assignment-to-exports-class-declaration/_config.js | 6 ++++++ .../_expected/amd.js | 6 ++++++ .../_expected/cjs.js | 4 ++++ .../_expected/es6.js | 4 ++++ .../_expected/iife.js | 7 +++++++ .../_expected/umd.js | 10 ++++++++++ .../assignment-to-exports-class-declaration/main.js | 2 ++ 8 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 test/form/assignment-to-exports-class-declaration/_config.js create mode 100644 test/form/assignment-to-exports-class-declaration/_expected/amd.js create mode 100644 test/form/assignment-to-exports-class-declaration/_expected/cjs.js create mode 100644 test/form/assignment-to-exports-class-declaration/_expected/es6.js create mode 100644 test/form/assignment-to-exports-class-declaration/_expected/iife.js create mode 100644 test/form/assignment-to-exports-class-declaration/_expected/umd.js create mode 100644 test/form/assignment-to-exports-class-declaration/main.js diff --git a/src/ast/attachScopes.js b/src/ast/attachScopes.js index 92798e8..51fc28a 100644 --- a/src/ast/attachScopes.js +++ b/src/ast/attachScopes.js @@ -29,7 +29,7 @@ export default function attachScopes ( statement ) { let newScope; // create new function scope - if ( /Function/.test( node.type ) ) { + if ( /(Function|Class)/.test( node.type ) ) { newScope = new Scope({ parent: scope, block: false, @@ -38,7 +38,7 @@ export default function attachScopes ( statement ) { // named function expressions - the name is considered // part of the function's scope - if ( node.type === 'FunctionExpression' && node.id ) { + if ( /(Function|Class)Expression/.test( node.type ) && node.id ) { newScope.addDeclaration( node, false, false ); } } diff --git a/test/form/assignment-to-exports-class-declaration/_config.js b/test/form/assignment-to-exports-class-declaration/_config.js new file mode 100644 index 0000000..0f3d2ef --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'does not rewrite class declaration IDs', + options: { + moduleName: 'myModule' + } +}; diff --git a/test/form/assignment-to-exports-class-declaration/_expected/amd.js b/test/form/assignment-to-exports-class-declaration/_expected/amd.js new file mode 100644 index 0000000..c2d0691 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/amd.js @@ -0,0 +1,6 @@ +define(['exports'], function (exports) { 'use strict'; + + exports.Foo = class Foo {} + exports.Foo = lol( exports.Foo ); + +}); diff --git a/test/form/assignment-to-exports-class-declaration/_expected/cjs.js b/test/form/assignment-to-exports-class-declaration/_expected/cjs.js new file mode 100644 index 0000000..b062474 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/cjs.js @@ -0,0 +1,4 @@ +'use strict'; + +exports.Foo = class Foo {} +exports.Foo = lol( exports.Foo ); diff --git a/test/form/assignment-to-exports-class-declaration/_expected/es6.js b/test/form/assignment-to-exports-class-declaration/_expected/es6.js new file mode 100644 index 0000000..08e82c9 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/es6.js @@ -0,0 +1,4 @@ +Foo = class Foo {} +Foo = lol( Foo ); + +export { Foo }; \ No newline at end of file diff --git a/test/form/assignment-to-exports-class-declaration/_expected/iife.js b/test/form/assignment-to-exports-class-declaration/_expected/iife.js new file mode 100644 index 0000000..fa37538 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/iife.js @@ -0,0 +1,7 @@ +(function (exports) { + 'use strict'; + + exports.Foo = class Foo {} + exports.Foo = lol( exports.Foo ); + +}((this.myModule = this.myModule || {}))); diff --git a/test/form/assignment-to-exports-class-declaration/_expected/umd.js b/test/form/assignment-to-exports-class-declaration/_expected/umd.js new file mode 100644 index 0000000..bbefc87 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/umd.js @@ -0,0 +1,10 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (factory((global.myModule = global.myModule || {}))); +}(this, function (exports) { 'use strict'; + + exports.Foo = class Foo {} + exports.Foo = lol( exports.Foo ); + +})); diff --git a/test/form/assignment-to-exports-class-declaration/main.js b/test/form/assignment-to-exports-class-declaration/main.js new file mode 100644 index 0000000..ceae376 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/main.js @@ -0,0 +1,2 @@ +export let Foo = class Foo {} +Foo = lol( Foo );