Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect side-effects in tagged template expressions
- Loading branch information
1 parent
b7a4edf
commit 8071790
Showing
9 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import Node from '../Node.js'; | ||
import isProgramLevel from '../utils/isProgramLevel.js'; | ||
import callHasEffects from './shared/callHasEffects.js'; | ||
|
||
export default class TaggedTemplateExpression extends Node { | ||
bind ( scope ) { | ||
if ( this.tag.type === 'Identifier' ) { | ||
const declaration = scope.findDeclaration( this.tag.name ); | ||
|
||
if ( declaration.isNamespace ) { | ||
this.module.error({ | ||
code: 'CANNOT_CALL_NAMESPACE', | ||
message: `Cannot call a namespace ('${this.tag.name}')` | ||
}, this.start ); | ||
} | ||
|
||
if ( this.tag.name === 'eval' && declaration.isGlobal ) { | ||
this.module.warn({ | ||
code: 'EVAL', | ||
message: `Use of eval is strongly discouraged, as it poses security risks and may cause issues with minification`, | ||
url: 'https://github.com/rollup/rollup/wiki/Troubleshooting#avoiding-eval' | ||
}, this.start ); | ||
} | ||
} | ||
|
||
super.bind( scope ); | ||
} | ||
|
||
hasEffects ( scope ) { | ||
return this.quasi.hasEffects(scope) || callHasEffects( scope, this.tag, false ); | ||
} | ||
|
||
initialise ( scope ) { | ||
if ( isProgramLevel( this ) ) { | ||
this.module.bundle.dependentExpressions.push( this ); | ||
} | ||
super.initialise( scope ); | ||
} | ||
|
||
isUsedByBundle () { | ||
return this.hasEffects( this.findScope() ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
description: 'detects side-effects in template literals and tagged template expressions', | ||
options: { | ||
moduleName: 'myBundle' | ||
} | ||
}; |
27 changes: 27 additions & 0 deletions
27
test/form/side-effects-in-template-literals/_expected/amd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
define(['exports'], function (exports) { 'use strict'; | ||
|
||
exports.x = 0; | ||
|
||
function noEffects() {} | ||
|
||
function modifyX() { | ||
return exports.x++; | ||
} | ||
|
||
const b = `${globalFunction()}has effects`; | ||
|
||
const c = `${modifyX()}has effects`; | ||
|
||
const e = noEffects`${globalFunction()}has effects`; | ||
|
||
const f = noEffects`${modifyX()}has effects`; | ||
|
||
const g = globalFunction`has effects`; | ||
|
||
const h = globalFunction()`has effects`; | ||
|
||
const i = modifyX`has effects`; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
}); |
25 changes: 25 additions & 0 deletions
25
test/form/side-effects-in-template-literals/_expected/cjs.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
exports.x = 0; | ||
|
||
function noEffects() {} | ||
|
||
function modifyX() { | ||
return exports.x++; | ||
} | ||
|
||
const b = `${globalFunction()}has effects`; | ||
|
||
const c = `${modifyX()}has effects`; | ||
|
||
const e = noEffects`${globalFunction()}has effects`; | ||
|
||
const f = noEffects`${modifyX()}has effects`; | ||
|
||
const g = globalFunction`has effects`; | ||
|
||
const h = globalFunction()`has effects`; | ||
|
||
const i = modifyX`has effects`; |
23 changes: 23 additions & 0 deletions
23
test/form/side-effects-in-template-literals/_expected/es.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
let x = 0; | ||
|
||
function noEffects() {} | ||
|
||
function modifyX() { | ||
return x++; | ||
} | ||
|
||
const b = `${globalFunction()}has effects`; | ||
|
||
const c = `${modifyX()}has effects`; | ||
|
||
const e = noEffects`${globalFunction()}has effects`; | ||
|
||
const f = noEffects`${modifyX()}has effects`; | ||
|
||
const g = globalFunction`has effects`; | ||
|
||
const h = globalFunction()`has effects`; | ||
|
||
const i = modifyX`has effects`; | ||
|
||
export { x }; |
28 changes: 28 additions & 0 deletions
28
test/form/side-effects-in-template-literals/_expected/iife.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var myBundle = (function (exports) { | ||
'use strict'; | ||
|
||
exports.x = 0; | ||
|
||
function noEffects() {} | ||
|
||
function modifyX() { | ||
return exports.x++; | ||
} | ||
|
||
const b = `${globalFunction()}has effects`; | ||
|
||
const c = `${modifyX()}has effects`; | ||
|
||
const e = noEffects`${globalFunction()}has effects`; | ||
|
||
const f = noEffects`${modifyX()}has effects`; | ||
|
||
const g = globalFunction`has effects`; | ||
|
||
const h = globalFunction()`has effects`; | ||
|
||
const i = modifyX`has effects`; | ||
|
||
return exports; | ||
|
||
}({})); |
31 changes: 31 additions & 0 deletions
31
test/form/side-effects-in-template-literals/_expected/umd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.myBundle = {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
|
||
exports.x = 0; | ||
|
||
function noEffects() {} | ||
|
||
function modifyX() { | ||
return exports.x++; | ||
} | ||
|
||
const b = `${globalFunction()}has effects`; | ||
|
||
const c = `${modifyX()}has effects`; | ||
|
||
const e = noEffects`${globalFunction()}has effects`; | ||
|
||
const f = noEffects`${modifyX()}has effects`; | ||
|
||
const g = globalFunction`has effects`; | ||
|
||
const h = globalFunction()`has effects`; | ||
|
||
const i = modifyX`has effects`; | ||
|
||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
|
||
}))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
let x = 0; | ||
|
||
function noEffects() {} | ||
|
||
function modifyX() { | ||
return x++; | ||
} | ||
|
||
const a = `${noEffects()}is removed`; | ||
|
||
const b = `${globalFunction()}has effects`; | ||
|
||
const c = `${modifyX()}has effects`; | ||
|
||
const d = noEffects`is removed`; | ||
|
||
const e = noEffects`${globalFunction()}has effects`; | ||
|
||
const f = noEffects`${modifyX()}has effects`; | ||
|
||
const g = globalFunction`has effects`; | ||
|
||
const h = globalFunction()`has effects`; | ||
|
||
const i = modifyX`has effects`; | ||
|
||
export {x}; |