From 13d5c3ceecfe4d15822477bc878e1ac5da054853 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Fri, 30 Oct 2015 19:32:35 -0400 Subject: [PATCH 1/2] warn on eval --- src/Statement.js | 5 +++++ test/function/warn-on-eval/_config.js | 17 +++++++++++++++++ test/function/warn-on-eval/main.js | 1 + 3 files changed, 23 insertions(+) create mode 100644 test/function/warn-on-eval/_config.js create mode 100644 test/function/warn-on-eval/main.js diff --git a/src/Statement.js b/src/Statement.js index c6c72f2..d5ec856 100644 --- a/src/Statement.js +++ b/src/Statement.js @@ -95,6 +95,11 @@ export default class Statement { walk( this.node, { enter ( node, parent ) { + // warn about eval + if ( node.type === 'CallExpression' && node.callee.name === 'eval' && !scope.contains( 'eval' ) ) { + module.bundle.onwarn( `Use of \`eval\` (in ${module.id}) is discouraged, as it may cause issues with minification. See https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval for more details` ); + } + if ( node.type === 'TemplateElement' ) stringLiteralRanges.push([ node.start, node.end ]); if ( node.type === 'Literal' && typeof node.value === 'string' && /\n/.test( node.raw ) ) { stringLiteralRanges.push([ node.start + 1, node.end - 1 ]); diff --git a/test/function/warn-on-eval/_config.js b/test/function/warn-on-eval/_config.js new file mode 100644 index 0000000..2c4dae9 --- /dev/null +++ b/test/function/warn-on-eval/_config.js @@ -0,0 +1,17 @@ +var path = require( 'path' ); +var assert = require( 'assert' ); + +var warned = false; + +module.exports = { + description: 'warns about use of eval', + options: { + onwarn: function ( message ) { + warned = true; + assert.equal( message, 'Use of `eval` (in ' + path.resolve( __dirname, 'main.js' ) + ') is discouraged, as it may cause issues with minification. See https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval for more details' ); + } + }, + exports: function () { + assert.ok( warned, 'did not warn' ); + } +}; diff --git a/test/function/warn-on-eval/main.js b/test/function/warn-on-eval/main.js new file mode 100644 index 0000000..e6f891c --- /dev/null +++ b/test/function/warn-on-eval/main.js @@ -0,0 +1 @@ +var result = eval( '1 + 1' ); From b754c0c9b49f4c9ca1a9dcaf3a17c1aad6c4672a Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Fri, 30 Oct 2015 21:31:39 -0400 Subject: [PATCH 2/2] fix test in windows (hopefully) --- test/function/warn-on-eval/_config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/function/warn-on-eval/_config.js b/test/function/warn-on-eval/_config.js index 2c4dae9..62a3ee4 100644 --- a/test/function/warn-on-eval/_config.js +++ b/test/function/warn-on-eval/_config.js @@ -8,7 +8,7 @@ module.exports = { options: { onwarn: function ( message ) { warned = true; - assert.equal( message, 'Use of `eval` (in ' + path.resolve( __dirname, 'main.js' ) + ') is discouraged, as it may cause issues with minification. See https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval for more details' ); + assert.ok( /Use of `eval` \(in .+?main\.js\) is discouraged, as it may cause issues with minification\. See https:\/\/github.com\/rollup\/rollup\/wiki\/Troubleshooting#avoiding-eval for more details/.test( message ) ); } }, exports: function () {