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 ) {