Browse Source

allow user-specified acorn options (#564)

ghi-672
Rich Harris 9 years ago
parent
commit
1d11282d6e
  1. 1
      src/Bundle.js
  2. 6
      src/Module.js
  3. 1
      src/rollup.js
  4. 10
      src/utils/object.js
  5. 9
      test/function/allow-reserved/_config.js
  6. 2
      test/function/allow-reserved/main.js

1
src/Bundle.js

@ -82,6 +82,7 @@ export default class Bundle {
[ 'module', 'exports', '_interopDefault' ].forEach( global => this.assumedGlobals[ global ] = true );
this.varOrConst = options.preferConst ? 'const' : 'var';
this.acornOptions = options.acorn || {};
}
build () {

6
src/Module.js

@ -2,7 +2,7 @@ import { parse } from 'acorn/src/index.js';
import MagicString from 'magic-string';
import { walk } from 'estree-walker';
import Statement from './Statement.js';
import { blank, keys } from './utils/object.js';
import { assign, blank, keys } from './utils/object.js';
import { basename, extname } from './utils/path.js';
import getLocation from './utils/getLocation.js';
import makeLegalIdentifier from './utils/makeLegalIdentifier.js';
@ -305,12 +305,12 @@ export default class Module {
// Try to extract a list of top-level statements/declarations. If
// the parse fails, attach file info and abort
try {
this.ast = parse( this.code, {
this.ast = parse( this.code, assign({
ecmaVersion: 6,
sourceType: 'module',
onComment: ( block, text, start, end ) => this.comments.push({ block, text, start, end }),
preserveParens: true
});
}, this.bundle.acornOptions ));
} catch ( err ) {
err.code = 'PARSE_ERROR';
err.file = this.id; // see above - not necessarily true, but true enough

1
src/rollup.js

@ -8,6 +8,7 @@ import Bundle from './Bundle.js';
export const VERSION = '<@VERSION@>';
const ALLOWED_KEYS = [
'acorn',
'banner',
'cache',
'dest',

10
src/utils/object.js

@ -7,3 +7,13 @@ export function blank () {
export function forOwn ( object, func ) {
Object.keys( object ).forEach( key => func( object[ key ], key ) );
}
export function assign ( target, ...sources ) {
sources.forEach( source => {
for ( let key in source ) {
if ( source.hasOwnProperty( key ) ) target[ key ] = source[ key ];
}
});
return target;
}

9
test/function/allow-reserved/_config.js

@ -0,0 +1,9 @@
module.exports = {
solo: true,
description: 'allow reserved identifiers via custom acorn options',
options: {
acorn: {
allowReserved: true
}
}
};

2
test/function/allow-reserved/main.js

@ -0,0 +1,2 @@
var x = function await () {}
assert.equal( x.name, 'await' );
Loading…
Cancel
Save