Browse Source

Merge pull request #775 from rollup/gh-770

warn on top-level `this` – fixes #770
semi-dynamic-namespace-imports
Rich Harris 9 years ago
committed by GitHub
parent
commit
734702bbee
  1. 1
      src/Statement.js
  2. 5
      test/form/this-is-undefined/_config.js
  3. 13
      test/function/warn-on-top-level-this/_config.js
  4. 1
      test/function/warn-on-top-level-this/main.js
  5. 4
      test/test.js

1
src/Statement.js

@ -61,6 +61,7 @@ export default class Statement {
if ( node.type === 'ThisExpression' && contextDepth === 0 ) { if ( node.type === 'ThisExpression' && contextDepth === 0 ) {
module.magicString.overwrite( node.start, node.end, 'undefined' ); 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; if ( node._scope ) scope = node._scope;

5
test/form/this-is-undefined/_config.js

@ -1,3 +1,6 @@
module.exports = { module.exports = {
description: 'top-level `this` expression is rewritten as `undefined`' description: 'top-level `this` expression is rewritten as `undefined`',
options: {
onwarn: () => {}
}
}; };

13
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` );
}
};

1
test/function/warn-on-top-level-this/main.js

@ -0,0 +1 @@
this.foo = 'bar';

4
test/test.js

@ -279,14 +279,14 @@ describe( 'rollup', function () {
if ( config.skipIfWindows && process.platform === 'win32' ) return; if ( config.skipIfWindows && process.platform === 'win32' ) return;
var options = extend( {}, config.options, { var options = extend( {}, {
entry: FORM + '/' + dir + '/main.js', entry: FORM + '/' + dir + '/main.js',
onwarn: msg => { onwarn: msg => {
if ( /No name was provided for/.test( msg ) ) return; if ( /No name was provided for/.test( msg ) ) return;
if ( /as external dependency/.test( msg ) ) return; if ( /as external dependency/.test( msg ) ) return;
console.error( msg ); console.error( msg );
} }
}); }, config.options );
( config.skip ? describe.skip : config.solo ? describe.only : describe)( dir, function () { ( config.skip ? describe.skip : config.solo ? describe.only : describe)( dir, function () {
PROFILES.forEach( function ( profile ) { PROFILES.forEach( function ( profile ) {

Loading…
Cancel
Save