mirror of https://github.com/lukechilds/rollup.git
Rich-Harris
9 years ago
57 changed files with 630 additions and 218 deletions
@ -0,0 +1,30 @@ |
|||
export class Reference { |
|||
constructor ( node, scope, statement ) { |
|||
this.node = node; |
|||
this.scope = scope; |
|||
this.statement = statement; |
|||
|
|||
this.declaration = null; // bound later
|
|||
|
|||
this.parts = []; |
|||
|
|||
let root = node; |
|||
while ( root.type === 'MemberExpression' ) { |
|||
this.parts.unshift( root.property.name ); |
|||
root = root.object; |
|||
} |
|||
|
|||
this.name = root.name; |
|||
|
|||
this.start = node.start; |
|||
this.end = node.start + this.name.length; // can be overridden in the case of namespace members
|
|||
this.rewritten = false; |
|||
} |
|||
} |
|||
|
|||
export class SyntheticReference { |
|||
constructor ( name ) { |
|||
this.name = name; |
|||
this.parts = []; |
|||
} |
|||
} |
@ -0,0 +1,42 @@ |
|||
let pureFunctions = {}; |
|||
|
|||
const arrayTypes = 'Array Int8Array Uint8Array Uint8ClampedArray Int16Array Uint16Array Int32Array Uint32Array Float32Array Float64Array'.split( ' ' ); |
|||
const simdTypes = 'Int8x16 Int16x8 Int32x4 Float32x4 Float64x2'.split( ' ' ); |
|||
const simdMethods = 'abs add and bool check div equal extractLane fromFloat32x4 fromFloat32x4Bits fromFloat64x2 fromFloat64x2Bits fromInt16x8Bits fromInt32x4 fromInt32x4Bits fromInt8x16Bits greaterThan greaterThanOrEqual lessThan lessThanOrEqual load max maxNum min minNum mul neg not notEqual or reciprocalApproximation reciprocalSqrtApproximation replaceLane select selectBits shiftLeftByScalar shiftRightArithmeticByScalar shiftRightLogicalByScalar shuffle splat sqrt store sub swizzle xor'.split( ' ' ); |
|||
let allSimdMethods = []; |
|||
simdTypes.forEach( t => { |
|||
simdMethods.forEach( m => { |
|||
allSimdMethods.push( `SIMD.${t}.${m}` ); |
|||
}); |
|||
}); |
|||
|
|||
[ |
|||
'Array.isArray', |
|||
'Error', 'EvalError', 'InternalError', 'RangeError', 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', |
|||
'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'decodeURI', 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', 'escape', 'unescape', |
|||
'Object', 'Object.create', 'Object.getNotifier', 'Object.getOwn', 'Object.getOwnPropertyDescriptor', 'Object.getOwnPropertyNames', 'Object.getOwnPropertySymbols', 'Object.getPrototypeOf', 'Object.is', 'Object.isExtensible', 'Object.isFrozen', 'Object.isSealed', 'Object.keys', |
|||
'Function', 'Boolean', |
|||
'Number', 'Number.isFinite', 'Number.isInteger', 'Number.isNaN', 'Number.isSafeInteger', 'Number.parseFloat', 'Number.parseInt', |
|||
'Symbol', 'Symbol.for', 'Symbol.keyFor', |
|||
'Math.abs', 'Math.acos', 'Math.acosh', 'Math.asin', 'Math.asinh', 'Math.atan', 'Math.atan2', 'Math.atanh', 'Math.cbrt', 'Math.ceil', 'Math.clz32', 'Math.cos', 'Math.cosh', 'Math.exp', 'Math.expm1', 'Math.floor', 'Math.fround', 'Math.hypot', 'Math.imul', 'Math.log', 'Math.log10', 'Math.log1p', 'Math.log2', 'Math.max', 'Math.min', 'Math.pow', 'Math.random', 'Math.round', 'Math.sign', 'Math.sin', 'Math.sinh', 'Math.sqrt', 'Math.tan', 'Math.tanh', 'Math.trunc', |
|||
'Date', 'Date.UTC', 'Date.now', 'Date.parse', |
|||
'String', 'String.fromCharCode', 'String.fromCodePoint', 'String.raw', |
|||
'RegExp', |
|||
'Map', 'Set', 'WeakMap', 'WeakSet', |
|||
'ArrayBuffer', 'ArrayBuffer.isView', |
|||
'DataView', |
|||
'JSON.parse', 'JSON.stringify', |
|||
'Promise', 'Promise.all', 'Promise.race', 'Promise.reject', 'Promise.resolve', |
|||
'Intl.Collator', 'Intl.Collator.supportedLocalesOf', 'Intl.DateTimeFormat', 'Intl.DateTimeFormat.supportedLocalesOf', 'Intl.NumberFormat', 'Intl.NumberFormat.supportedLocalesOf' |
|||
|
|||
// TODO properties of e.g. window...
|
|||
].concat( |
|||
arrayTypes, |
|||
arrayTypes.map( t => `${t}.from` ), |
|||
arrayTypes.map( t => `${t}.of` ), |
|||
simdTypes.map( t => `SIMD.${t}` ), |
|||
allSimdMethods |
|||
).forEach( name => pureFunctions[ name ] = true ); |
|||
// TODO add others to this list from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
|
|||
|
|||
export default pureFunctions; |
@ -0,0 +1,6 @@ |
|||
module.exports = { |
|||
description: 'computed property keys include declarations of referenced identifiers', |
|||
options: { |
|||
moduleName: 'computedProperties' |
|||
} |
|||
}; |
@ -0,0 +1,9 @@ |
|||
define(['exports'], function (exports) { 'use strict'; |
|||
|
|||
var foo = 'foo'; |
|||
|
|||
var x = {[foo]: 'bar'}; |
|||
|
|||
exports.x = x; |
|||
|
|||
}); |
@ -0,0 +1,7 @@ |
|||
'use strict'; |
|||
|
|||
var foo = 'foo'; |
|||
|
|||
var x = {[foo]: 'bar'}; |
|||
|
|||
exports.x = x; |
@ -0,0 +1,5 @@ |
|||
var foo = 'foo'; |
|||
|
|||
var x = {[foo]: 'bar'}; |
|||
|
|||
export { x }; |
@ -0,0 +1,10 @@ |
|||
(function (exports) { |
|||
'use strict'; |
|||
|
|||
var foo = 'foo'; |
|||
|
|||
var x = {[foo]: 'bar'}; |
|||
|
|||
exports.x = x; |
|||
|
|||
}((this.computedProperties = {}))); |
@ -0,0 +1,13 @@ |
|||
(function (global, factory) { |
|||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : |
|||
typeof define === 'function' && define.amd ? define(['exports'], factory) : |
|||
(factory((global.computedProperties = {}))); |
|||
}(this, function (exports) { 'use strict'; |
|||
|
|||
var foo = 'foo'; |
|||
|
|||
var x = {[foo]: 'bar'}; |
|||
|
|||
exports.x = x; |
|||
|
|||
})); |
@ -0,0 +1,3 @@ |
|||
var foo = 'foo'; |
|||
|
|||
export var x = {[foo]: 'bar'}; |
@ -0,0 +1,6 @@ |
|||
module.exports = { |
|||
description: 'handles external aliased named imports that shadow another name', |
|||
options: { |
|||
external: [ 'acorn' ] |
|||
} |
|||
}; |
@ -0,0 +1,9 @@ |
|||
define(['acorn'], function (acorn) { 'use strict'; |
|||
|
|||
function parse$1(source) { |
|||
return acorn.parse(source, { ecmaVersion: 6 }); |
|||
} |
|||
|
|||
console.log(parse$1('foo')); |
|||
|
|||
}); |
@ -0,0 +1,9 @@ |
|||
'use strict'; |
|||
|
|||
var acorn = require('acorn'); |
|||
|
|||
function parse$1(source) { |
|||
return acorn.parse(source, { ecmaVersion: 6 }); |
|||
} |
|||
|
|||
console.log(parse$1('foo')); |
@ -0,0 +1,7 @@ |
|||
import { parse } from 'acorn'; |
|||
|
|||
function parse$1(source) { |
|||
return parse(source, { ecmaVersion: 6 }); |
|||
} |
|||
|
|||
console.log(parse$1('foo')); |
@ -0,0 +1,10 @@ |
|||
(function (acorn) { |
|||
'use strict'; |
|||
|
|||
function parse$1(source) { |
|||
return acorn.parse(source, { ecmaVersion: 6 }); |
|||
} |
|||
|
|||
console.log(parse$1('foo')); |
|||
|
|||
}(acorn)); |
@ -0,0 +1,13 @@ |
|||
(function (global, factory) { |
|||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('acorn')) : |
|||
typeof define === 'function' && define.amd ? define(['acorn'], factory) : |
|||
(factory(global.acorn)); |
|||
}(this, function (acorn) { 'use strict'; |
|||
|
|||
function parse$1(source) { |
|||
return acorn.parse(source, { ecmaVersion: 6 }); |
|||
} |
|||
|
|||
console.log(parse$1('foo')); |
|||
|
|||
})); |
@ -0,0 +1,3 @@ |
|||
import parse from './parse'; |
|||
|
|||
console.log(parse('foo')); |
@ -0,0 +1,5 @@ |
|||
import { parse as acornParse } from 'acorn'; |
|||
|
|||
export default function parse(source) { |
|||
return acornParse(source, { ecmaVersion: 6 }); |
|||
} |
@ -1,3 +1,3 @@ |
|||
module.exports = { |
|||
description: 'object destructuring default values are preserved' |
|||
description: 'object destructuring default values are preserved' |
|||
}; |
|||
|
@ -0,0 +1,3 @@ |
|||
module.exports = { |
|||
description: 'detects side-effects in complex call expressions' |
|||
}; |
@ -0,0 +1,13 @@ |
|||
define(function () { 'use strict'; |
|||
|
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
( Math.random() < 0.5 ? foo : bar )(); |
|||
|
|||
}); |
@ -0,0 +1,11 @@ |
|||
'use strict'; |
|||
|
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
( Math.random() < 0.5 ? foo : bar )(); |
@ -0,0 +1,9 @@ |
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
( Math.random() < 0.5 ? foo : bar )(); |
@ -0,0 +1,14 @@ |
|||
(function () { |
|||
'use strict'; |
|||
|
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
( Math.random() < 0.5 ? foo : bar )(); |
|||
|
|||
}()); |
@ -0,0 +1,17 @@ |
|||
(function (global, factory) { |
|||
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : |
|||
typeof define === 'function' && define.amd ? define(factory) : |
|||
(factory()); |
|||
}(this, function () { 'use strict'; |
|||
|
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
( Math.random() < 0.5 ? foo : bar )(); |
|||
|
|||
})); |
@ -0,0 +1,9 @@ |
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
( Math.random() < 0.5 ? foo : bar )(); |
@ -0,0 +1,3 @@ |
|||
module.exports = { |
|||
description: 'detects side-effects in complex call expressions' |
|||
}; |
@ -0,0 +1,17 @@ |
|||
define(function () { 'use strict'; |
|||
|
|||
function fn () { |
|||
return Math.random() < 0.5 ? foo : bar; |
|||
} |
|||
|
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
fn()(); |
|||
|
|||
}); |
@ -0,0 +1,15 @@ |
|||
'use strict'; |
|||
|
|||
function fn () { |
|||
return Math.random() < 0.5 ? foo : bar; |
|||
} |
|||
|
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
fn()(); |
@ -0,0 +1,13 @@ |
|||
function fn () { |
|||
return Math.random() < 0.5 ? foo : bar; |
|||
} |
|||
|
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
fn()(); |
@ -0,0 +1,18 @@ |
|||
(function () { |
|||
'use strict'; |
|||
|
|||
function fn () { |
|||
return Math.random() < 0.5 ? foo : bar; |
|||
} |
|||
|
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
fn()(); |
|||
|
|||
}()); |
@ -0,0 +1,21 @@ |
|||
(function (global, factory) { |
|||
typeof exports === 'object' && typeof module !== 'undefined' ? factory() : |
|||
typeof define === 'function' && define.amd ? define(factory) : |
|||
(factory()); |
|||
}(this, function () { 'use strict'; |
|||
|
|||
function fn () { |
|||
return Math.random() < 0.5 ? foo : bar; |
|||
} |
|||
|
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
fn()(); |
|||
|
|||
})); |
@ -0,0 +1,13 @@ |
|||
function fn () { |
|||
return Math.random() < 0.5 ? foo : bar; |
|||
} |
|||
|
|||
function foo () { |
|||
console.log( 'foo' ); |
|||
} |
|||
|
|||
function bar () { |
|||
console.log( 'bar' ); |
|||
} |
|||
|
|||
fn()(); |
@ -0,0 +1,3 @@ |
|||
module.exports = { |
|||
description: 'calls non-function default exports' |
|||
}; |
@ -0,0 +1,9 @@ |
|||
function x () { |
|||
global.answer = 'x'; |
|||
} |
|||
|
|||
function y () { |
|||
global.answer = 'y'; |
|||
} |
|||
|
|||
export default Math.random() < 0.5 ? x : y; |
@ -0,0 +1,4 @@ |
|||
import foo from './foo.js'; |
|||
foo(); |
|||
|
|||
assert.ok( /[xy]/.test( global.answer ) ); |
@ -1,18 +1,17 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
var warned; |
|||
|
|||
module.exports = { |
|||
description: 'resolves pathological cyclical dependencies gracefully', |
|||
babel: true, |
|||
exports: function ( exports ) { |
|||
assert.ok( exports.a.isA ); |
|||
assert.ok( exports.b1.isA ); |
|||
assert.ok( exports.b1.isB ); |
|||
assert.ok( exports.b2.isA ); |
|||
assert.ok( exports.b2.isB ); |
|||
assert.ok( exports.c1.isC ); |
|||
assert.ok( exports.c1.isD ); |
|||
assert.ok( exports.c2.isC ); |
|||
assert.ok( exports.c2.isD ); |
|||
assert.ok( exports.d.isD ); |
|||
options: { |
|||
onwarn: function ( message ) { |
|||
assert.ok( /Module .+B\.js may be unable to evaluate without .+A\.js, but is included first due to a cyclical dependency. Consider swapping the import statements in .+main\.js to ensure correct ordering/.test( message ) ); |
|||
warned = true; |
|||
} |
|||
}, |
|||
runtimeError: function () { |
|||
assert.ok( warned ); |
|||
} |
|||
}; |
|||
|
@ -0,0 +1,10 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'allows destructuring in exported variable declarations, synthetic or otherwise', |
|||
babel: true, |
|||
exports: function ( exports ) { |
|||
assert.equal( exports.a, 1 ); |
|||
assert.equal( exports.d, 4 ); |
|||
} |
|||
}; |
@ -0,0 +1,11 @@ |
|||
let { a, b } = c(), [ d, e ] = f(); |
|||
|
|||
function c () { |
|||
return { a: 1, b: 2 }; |
|||
} |
|||
|
|||
function f () { |
|||
return [ 4, 5 ]; |
|||
} |
|||
|
|||
export { a, d }; |
@ -0,0 +1,3 @@ |
|||
module.exports = { |
|||
description: 'handles default function exports wrapped in parens' |
|||
}; |
@ -0,0 +1,3 @@ |
|||
export default (function () { |
|||
global.answer = 42; |
|||
}); |
@ -0,0 +1,3 @@ |
|||
import foo from './foo.js'; |
|||
foo(); |
|||
assert.equal( global.answer, 42 ); |
@ -1,15 +1,16 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
var warned; |
|||
|
|||
module.exports = { |
|||
description: 'does not treat references inside IIFEs as weak dependencies', // edge case encountered in THREE.js codebase
|
|||
exports: function ( exports ) { |
|||
assert.ok( exports.a1.isA ); |
|||
assert.ok( exports.b1.isB ); |
|||
assert.ok( exports.c1.isC ); |
|||
assert.ok( exports.d1.isD ); |
|||
assert.ok( exports.a2.isA ); |
|||
assert.ok( exports.b2.isB ); |
|||
assert.ok( exports.c2.isC ); |
|||
assert.ok( exports.d2.isD ); |
|||
options: { |
|||
onwarn: function ( message ) { |
|||
assert.ok( /Module .+D\.js may be unable to evaluate without .+C\.js, but is included first due to a cyclical dependency. Consider swapping the import statements in .+main\.js to ensure correct ordering/.test( message ) ); |
|||
warned = true; |
|||
} |
|||
}, |
|||
runtimeError: function () { |
|||
assert.ok( warned ); |
|||
} |
|||
}; |
|||
|
@ -0,0 +1,3 @@ |
|||
module.exports = { |
|||
description: 'imports as- chained exports' |
|||
}; |
@ -0,0 +1 @@ |
|||
export var value = 42; |
@ -0,0 +1,5 @@ |
|||
import * as second from './second'; |
|||
|
|||
assert.equal( second.first.value, 42 ); |
|||
console.log( 'second', second ) |
|||
assert.deepEqual( second, { first: { value: 42 } }); |
@ -0,0 +1,2 @@ |
|||
import * as first from './first'; |
|||
export { first }; |
@ -0,0 +1,8 @@ |
|||
var assert = require( 'assert' ); |
|||
|
|||
module.exports = { |
|||
description: 'removes empty exported var declarations', |
|||
exports: function ( exports ) { |
|||
assert.equal( exports.foo, 42 ); |
|||
} |
|||
}; |
@ -0,0 +1,4 @@ |
|||
var foo; |
|||
foo = 42; |
|||
|
|||
export { foo }; |
Loading…
Reference in new issue