From 4a8ce666abcf47471e0726d0fba135ece1ccf8f9 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Thu, 7 Jul 2016 23:22:34 -0400 Subject: [PATCH] =?UTF-8?q?warn=20on=20top-level=20`this`=20=E2=80=93=20fi?= =?UTF-8?q?xes=20#770?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Statement.js | 1 + test/form/this-is-undefined/_config.js | 5 ++++- test/function/warn-on-top-level-this/_config.js | 13 +++++++++++++ test/function/warn-on-top-level-this/main.js | 1 + test/test.js | 4 ++-- 5 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 test/function/warn-on-top-level-this/_config.js create mode 100644 test/function/warn-on-top-level-this/main.js diff --git a/src/Statement.js b/src/Statement.js index 8723ad5..c9b01f8 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -61,6 +61,7 @@ export default class Statement { if ( node.type === 'ThisExpression' && contextDepth === 0 ) { module.magicString.overwrite( node.start, node.end, 'undefined' ); + module.bundle.onwarn( 'The `this` keyword is equivalent to `undefined` at the top level of an ES module, and has been rewritten' ); } if ( node._scope ) scope = node._scope; diff --git a/test/form/this-is-undefined/_config.js b/test/form/this-is-undefined/_config.js index 155461a..7513402 100644 --- a/test/form/this-is-undefined/_config.js +++ b/test/form/this-is-undefined/_config.js @@ -1,3 +1,6 @@ module.exports = { - description: 'top-level `this` expression is rewritten as `undefined`' + description: 'top-level `this` expression is rewritten as `undefined`', + options: { + onwarn: () => {} + } }; diff --git a/test/function/warn-on-top-level-this/_config.js b/test/function/warn-on-top-level-this/_config.js new file mode 100644 index 0000000..c667044 --- /dev/null +++ b/test/function/warn-on-top-level-this/_config.js @@ -0,0 +1,13 @@ +const assert = require( 'assert' ); + +module.exports = { + description: 'warns on top-level this (#770)', + warnings: warnings => { + assert.deepEqual( warnings, [ + 'The `this` keyword is equivalent to `undefined` at the top level of an ES module, and has been rewritten' + ]); + }, + runtimeError: err => { + assert.equal( err.message, `Cannot set property 'foo' of undefined` ); + } +}; diff --git a/test/function/warn-on-top-level-this/main.js b/test/function/warn-on-top-level-this/main.js new file mode 100644 index 0000000..3853449 --- /dev/null +++ b/test/function/warn-on-top-level-this/main.js @@ -0,0 +1 @@ +this.foo = 'bar'; diff --git a/test/test.js b/test/test.js index 8996b27..0ed678e 100644 --- a/test/test.js +++ b/test/test.js @@ -279,14 +279,14 @@ describe( 'rollup', function () { if ( config.skipIfWindows && process.platform === 'win32' ) return; - var options = extend( {}, config.options, { + var options = extend( {}, { entry: FORM + '/' + dir + '/main.js', onwarn: msg => { if ( /No name was provided for/.test( msg ) ) return; if ( /as external dependency/.test( msg ) ) return; console.error( msg ); } - }); + }, config.options ); ( config.skip ? describe.skip : config.solo ? describe.only : describe)( dir, function () { PROFILES.forEach( function ( profile ) {