|
@ -1,8 +1,7 @@ |
|
|
import { blank, keys } from './utils/object.js'; |
|
|
import { assign, blank, keys } from './utils/object.js'; |
|
|
import run from './utils/run.js'; |
|
|
import run from './utils/run.js'; |
|
|
|
|
|
|
|
|
export default class Declaration { |
|
|
export default function Declaration ( node, isParam, statement ) { |
|
|
constructor ( node, isParam, statement ) { |
|
|
|
|
|
if ( node ) { |
|
|
if ( node ) { |
|
|
if ( node.type === 'FunctionDeclaration' ) { |
|
|
if ( node.type === 'FunctionDeclaration' ) { |
|
|
this.isFunctionDeclaration = true; |
|
|
this.isFunctionDeclaration = true; |
|
@ -23,23 +22,24 @@ export default class Declaration { |
|
|
this.isUsed = false; |
|
|
this.isUsed = false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
assign( Declaration.prototype, { |
|
|
addAlias ( declaration ) { |
|
|
addAlias ( declaration ) { |
|
|
this.aliases.push( declaration ); |
|
|
this.aliases.push( declaration ); |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
addReference ( reference ) { |
|
|
addReference ( reference ) { |
|
|
reference.declaration = this; |
|
|
reference.declaration = this; |
|
|
this.name = reference.name; // TODO handle differences of opinion
|
|
|
this.name = reference.name; // TODO handle differences of opinion
|
|
|
|
|
|
|
|
|
if ( reference.isReassignment ) this.isReassigned = true; |
|
|
if ( reference.isReassignment ) this.isReassigned = true; |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
render ( es6 ) { |
|
|
render ( es6 ) { |
|
|
if ( es6 ) return this.name; |
|
|
if ( es6 ) return this.name; |
|
|
if ( !this.isReassigned || !this.isExported ) return this.name; |
|
|
if ( !this.isReassigned || !this.isExported ) return this.name; |
|
|
|
|
|
|
|
|
return `exports.${this.name}`; |
|
|
return `exports.${this.name}`; |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
run ( strongDependencies ) { |
|
|
run ( strongDependencies ) { |
|
|
if ( this.tested ) return this.hasSideEffects; |
|
|
if ( this.tested ) return this.hasSideEffects; |
|
@ -52,7 +52,7 @@ export default class Declaration { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return this.hasSideEffects; |
|
|
return this.hasSideEffects; |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
use () { |
|
|
use () { |
|
|
if ( this.isUsed ) return; |
|
|
if ( this.isUsed ) return; |
|
@ -62,10 +62,9 @@ export default class Declaration { |
|
|
|
|
|
|
|
|
this.aliases.forEach( alias => alias.use() ); |
|
|
this.aliases.forEach( alias => alias.use() ); |
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
export class SyntheticDefaultDeclaration { |
|
|
export function SyntheticDefaultDeclaration ( node, statement, name ) { |
|
|
constructor ( node, statement, name ) { |
|
|
|
|
|
this.node = node; |
|
|
this.node = node; |
|
|
this.statement = statement; |
|
|
this.statement = statement; |
|
|
this.name = name; |
|
|
this.name = name; |
|
@ -75,9 +74,10 @@ export class SyntheticDefaultDeclaration { |
|
|
this.aliases = []; |
|
|
this.aliases = []; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
assign( SyntheticDefaultDeclaration.prototype, { |
|
|
addAlias ( declaration ) { |
|
|
addAlias ( declaration ) { |
|
|
this.aliases.push( declaration ); |
|
|
this.aliases.push( declaration ); |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
addReference ( reference ) { |
|
|
addReference ( reference ) { |
|
|
// Bind the reference to `this` declaration.
|
|
|
// Bind the reference to `this` declaration.
|
|
@ -87,17 +87,17 @@ export class SyntheticDefaultDeclaration { |
|
|
if ( reference.name === 'default' ) return; |
|
|
if ( reference.name === 'default' ) return; |
|
|
|
|
|
|
|
|
this.name = reference.name; |
|
|
this.name = reference.name; |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
bind ( declaration ) { |
|
|
bind ( declaration ) { |
|
|
this.original = declaration; |
|
|
this.original = declaration; |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
render () { |
|
|
render () { |
|
|
return !this.original || this.original.isReassigned ? |
|
|
return !this.original || this.original.isReassigned ? |
|
|
this.name : |
|
|
this.name : |
|
|
this.original.render(); |
|
|
this.original.render(); |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
run ( strongDependencies ) { |
|
|
run ( strongDependencies ) { |
|
|
if ( this.original ) { |
|
|
if ( this.original ) { |
|
@ -107,7 +107,7 @@ export class SyntheticDefaultDeclaration { |
|
|
if ( /FunctionExpression/.test( this.node.declaration.type ) ) { |
|
|
if ( /FunctionExpression/.test( this.node.declaration.type ) ) { |
|
|
return run( this.node.declaration.body, this.statement.scope, this.statement, strongDependencies, false ); |
|
|
return run( this.node.declaration.body, this.statement.scope, this.statement, strongDependencies, false ); |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
use () { |
|
|
use () { |
|
|
this.isUsed = true; |
|
|
this.isUsed = true; |
|
@ -117,10 +117,9 @@ export class SyntheticDefaultDeclaration { |
|
|
|
|
|
|
|
|
this.aliases.forEach( alias => alias.use() ); |
|
|
this.aliases.forEach( alias => alias.use() ); |
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
export class SyntheticNamespaceDeclaration { |
|
|
export function SyntheticNamespaceDeclaration ( module ) { |
|
|
constructor ( module ) { |
|
|
|
|
|
this.module = module; |
|
|
this.module = module; |
|
|
this.name = null; |
|
|
this.name = null; |
|
|
|
|
|
|
|
@ -133,9 +132,10 @@ export class SyntheticNamespaceDeclaration { |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
assign( SyntheticNamespaceDeclaration.prototype, { |
|
|
addAlias ( declaration ) { |
|
|
addAlias ( declaration ) { |
|
|
this.aliases.push( declaration ); |
|
|
this.aliases.push( declaration ); |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
addReference ( reference ) { |
|
|
addReference ( reference ) { |
|
|
// if we have e.g. `foo.bar`, we can optimise
|
|
|
// if we have e.g. `foo.bar`, we can optimise
|
|
@ -168,7 +168,7 @@ export class SyntheticNamespaceDeclaration { |
|
|
|
|
|
|
|
|
reference.declaration = this; |
|
|
reference.declaration = this; |
|
|
this.name = reference.name; |
|
|
this.name = reference.name; |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
renderBlock ( indentString ) { |
|
|
renderBlock ( indentString ) { |
|
|
const members = keys( this.originals ).map( name => { |
|
|
const members = keys( this.originals ).map( name => { |
|
@ -182,11 +182,11 @@ export class SyntheticNamespaceDeclaration { |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
return `var ${this.render()} = Object.freeze({\n${members.join( ',\n' )}\n});\n\n`; |
|
|
return `var ${this.render()} = Object.freeze({\n${members.join( ',\n' )}\n});\n\n`; |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
render () { |
|
|
render () { |
|
|
return this.name; |
|
|
return this.name; |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
use () { |
|
|
use () { |
|
|
keys( this.originals ).forEach( name => { |
|
|
keys( this.originals ).forEach( name => { |
|
@ -195,18 +195,18 @@ export class SyntheticNamespaceDeclaration { |
|
|
|
|
|
|
|
|
this.aliases.forEach( alias => alias.use() ); |
|
|
this.aliases.forEach( alias => alias.use() ); |
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
|
|
|
export class ExternalDeclaration { |
|
|
export function ExternalDeclaration ( module, name ) { |
|
|
constructor ( module, name ) { |
|
|
|
|
|
this.module = module; |
|
|
this.module = module; |
|
|
this.name = name; |
|
|
this.name = name; |
|
|
this.isExternal = true; |
|
|
this.isExternal = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
assign( ExternalDeclaration.prototype, { |
|
|
addAlias () { |
|
|
addAlias () { |
|
|
// noop
|
|
|
// noop
|
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
addReference ( reference ) { |
|
|
addReference ( reference ) { |
|
|
reference.declaration = this; |
|
|
reference.declaration = this; |
|
@ -214,7 +214,7 @@ export class ExternalDeclaration { |
|
|
if ( this.name === 'default' || this.name === '*' ) { |
|
|
if ( this.name === 'default' || this.name === '*' ) { |
|
|
this.module.suggestName( reference.name ); |
|
|
this.module.suggestName( reference.name ); |
|
|
} |
|
|
} |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
render ( es6 ) { |
|
|
render ( es6 ) { |
|
|
if ( this.name === '*' ) { |
|
|
if ( this.name === '*' ) { |
|
@ -228,13 +228,13 @@ export class ExternalDeclaration { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return es6 ? this.name : `${this.module.name}.${this.name}`; |
|
|
return es6 ? this.name : `${this.module.name}.${this.name}`; |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
run () { |
|
|
run () { |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
use () { |
|
|
use () { |
|
|
// noop?
|
|
|
// noop?
|
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|