Browse Source

more aggressive linting

semi-dynamic-namespace-imports
Rich-Harris 8 years ago
parent
commit
93ec667228
  1. 2
      .eslintignore
  2. 11
      .eslintrc
  3. 4
      src/ast/attachScopes.js
  4. 2
      src/rollup.js
  5. 20
      test/.babelrc
  6. 241
      test/test.js

2
.eslintignore

@ -0,0 +1,2 @@
test/*
!test/test.js

11
.eslintrc

@ -7,7 +7,16 @@
"space-before-blocks": [ 2, "always" ], "space-before-blocks": [ 2, "always" ],
"space-before-function-paren": [ 2, "always" ], "space-before-function-paren": [ 2, "always" ],
"no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ], "no-mixed-spaces-and-tabs": [ 2, "smart-tabs" ],
"no-cond-assign": [ 0 ] "no-cond-assign": [ 0 ],
"object-shorthand": [2, "always" ],
"no-const-assign": 2,
"no-class-assign": 2,
"no-this-before-super": 2,
"no-var": 2,
"quote-props": [ 2, "as-needed" ],
"one-var": [ 2, "never" ],
"prefer-arrow-callback": 2,
"arrow-spacing": 2
}, },
"env": { "env": {
"es6": true, "es6": true,

4
src/ast/attachScopes.js

@ -2,8 +2,8 @@ import { walk } from 'estree-walker';
import Scope from './Scope.js'; import Scope from './Scope.js';
const blockDeclarations = { const blockDeclarations = {
'const': true, const: true,
'let': true let: true
}; };
export default function attachScopes ( statement ) { export default function attachScopes ( statement ) {

2
src/rollup.js

@ -68,7 +68,7 @@ export function rollup ( options ) {
return rendered; return rendered;
} }
var result = { const result = {
imports: bundle.externalModules.map( module => module.id ), imports: bundle.externalModules.map( module => module.id ),
exports: keys( bundle.entryModule.exports ), exports: keys( bundle.entryModule.exports ),
modules: bundle.orderedModules.map( module => module.toJSON() ), modules: bundle.orderedModules.map( module => module.toJSON() ),

20
test/.babelrc

@ -1,20 +0,0 @@
{
"whitelist": [
"es6.arrowFunctions",
"es6.blockScoping",
"es6.classes",
"es6.constants",
"es6.destructuring",
"es6.modules",
"es6.parameters",
"es6.properties.shorthand",
"es6.spread",
"es6.templateLiterals"
],
"loose": [
"es6.classes",
"es6.destructuring"
],
"compact": false,
"sourceMap": true
}

241
test/test.js

@ -1,19 +1,19 @@
require( 'source-map-support' ).install(); require( 'source-map-support' ).install();
require( 'console-group' ).install(); require( 'console-group' ).install();
var path = require( 'path' ); const path = require( 'path' );
var sander = require( 'sander' ); const sander = require( 'sander' );
var assert = require( 'assert' ); const assert = require( 'assert' );
var exec = require( 'child_process' ).exec; const { exec } = require( 'child_process' );
var buble = require( 'buble' ); const buble = require( 'buble' );
var rollup = require( '../dist/rollup' ); const rollup = require( '../dist/rollup' );
var FUNCTION = path.resolve( __dirname, 'function' ); const FUNCTION = path.resolve( __dirname, 'function' );
var FORM = path.resolve( __dirname, 'form' ); const FORM = path.resolve( __dirname, 'form' );
var SOURCEMAPS = path.resolve( __dirname, 'sourcemaps' ); const SOURCEMAPS = path.resolve( __dirname, 'sourcemaps' );
var CLI = path.resolve( __dirname, 'cli' ); const CLI = path.resolve( __dirname, 'cli' );
var PROFILES = [ const PROFILES = [
{ format: 'amd' }, { format: 'amd' },
{ format: 'cjs' }, { format: 'cjs' },
{ format: 'es' }, { format: 'es' },
@ -22,8 +22,8 @@ var PROFILES = [
]; ];
function extend ( target ) { function extend ( target ) {
[].slice.call( arguments, 1 ).forEach( function ( source ) { [].slice.call( arguments, 1 ).forEach( source => {
source && Object.keys( source ).forEach( function ( key ) { source && Object.keys( source ).forEach( key => {
target[ key ] = source[ key ]; target[ key ] = source[ key ];
}); });
}); });
@ -41,7 +41,7 @@ function loadConfig ( path ) {
} catch ( err ) { } catch ( err ) {
console.error( err.message ); console.error( err.message );
console.error( err.stack ); console.error( err.stack );
throw new Error( 'Failed to load ' + path + '. An old test perhaps? You should probably delete the directory' ); throw new Error( `Failed to load ${path}. An old test perhaps? You should probably delete the directory` );
} }
} }
@ -57,81 +57,81 @@ function loader ( modules ) {
}; };
} }
describe( 'rollup', function () { describe( 'rollup', () => {
this.timeout( 10000 ); this.timeout( 10000 );
describe( 'sanity checks', function () { describe( 'sanity checks', () => {
it( 'exists', function () { it( 'exists', () => {
assert.ok( !!rollup ); assert.ok( !!rollup );
}); });
it( 'has a rollup method', function () { it( 'has a rollup method', () => {
assert.equal( typeof rollup.rollup, 'function' ); assert.equal( typeof rollup.rollup, 'function' );
}); });
it( 'fails without options', function () { it( 'fails without options', () => {
return rollup.rollup().then( function () { return rollup.rollup().then( () => {
throw new Error( 'Missing expected error' ); throw new Error( 'Missing expected error' );
}, function (err) { }, err => {
assert.equal( 'You must supply options.entry to rollup', err.message ); assert.equal( 'You must supply options.entry to rollup', err.message );
}); });
}); });
it( 'fails without options.entry', function () { it( 'fails without options.entry', () => {
return rollup.rollup({}).then( function () { return rollup.rollup({}).then( () => {
throw new Error( 'Missing expected error' ); throw new Error( 'Missing expected error' );
}, function (err) { }, err => {
assert.equal( 'You must supply options.entry to rollup', err.message ); assert.equal( 'You must supply options.entry to rollup', err.message );
}); });
}); });
it( 'fails with invalid keys', function () { it( 'fails with invalid keys', () => {
return rollup.rollup({ entry: 'x', plUgins: [] }).then( function () { return rollup.rollup({ entry: 'x', plUgins: [] }).then( () => {
throw new Error( 'Missing expected error' ); throw new Error( 'Missing expected error' );
}, function ( err ) { }, err => {
assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: acorn, banner, cache, dest, entry, exports, external, footer, format, globals, indent, intro, moduleId, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, sourceMap, sourceMapFile, targets, treeshake, useStrict' ); assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: acorn, banner, cache, dest, entry, exports, external, footer, format, globals, indent, intro, moduleId, moduleName, noConflict, onwarn, outro, paths, plugins, preferConst, sourceMap, sourceMapFile, targets, treeshake, useStrict' );
}); });
}); });
}); });
describe( 'bundle.write()', function () { describe( 'bundle.write()', () => {
it( 'fails without options or options.dest', function () { it( 'fails without options or options.dest', () => {
return rollup.rollup({ return rollup.rollup({
entry: 'x', entry: 'x',
plugins: [{ plugins: [{
resolveId: function () { return 'test'; }, resolveId: () => { return 'test'; },
load: function () { load: () => {
return '// empty'; return '// empty';
} }
}] }]
}).then( function ( bundle ) { }).then( bundle => {
assert.throws( function () { assert.throws( () => {
bundle.write(); bundle.write();
}, /must supply options\.dest/ ); }, /must supply options\.dest/ );
assert.throws( function () { assert.throws( () => {
bundle.write({}); bundle.write({});
}, /must supply options\.dest/ ); }, /must supply options\.dest/ );
}); });
}); });
it( 'expects options.moduleName for IIFE and UMD bundles', function () { it( 'expects options.moduleName for IIFE and UMD bundles', () => {
return rollup.rollup({ return rollup.rollup({
entry: 'x', entry: 'x',
plugins: [{ plugins: [{
resolveId: function () { return 'test'; }, resolveId: () => { return 'test'; },
load: function () { load: () => {
return 'export var foo = 42;'; return 'export var foo = 42;';
} }
}] }]
}).then( function ( bundle ) { }).then( bundle => {
assert.throws( function () { assert.throws( () => {
bundle.generate({ bundle.generate({
format: 'umd' format: 'umd'
}); });
}, /You must supply options\.moduleName for UMD bundles/ ); }, /You must supply options\.moduleName for UMD bundles/ );
assert.throws( function () { assert.throws( () => {
bundle.generate({ bundle.generate({
format: 'iife' format: 'iife'
}); });
@ -139,37 +139,37 @@ describe( 'rollup', function () {
}); });
}); });
it( 'warns on es6 format', function () { it( 'warns on es6 format', () => {
var warned; let warned;
return rollup.rollup({ return rollup.rollup({
entry: 'x', entry: 'x',
plugins: [{ plugins: [{
resolveId: function () { return 'test'; }, resolveId: () => { return 'test'; },
load: function () { load: () => {
return '// empty'; return '// empty';
} }
}], }],
onwarn: function ( msg ) { onwarn: msg => {
if ( /The es6 format is deprecated/.test( msg ) ) warned = true; if ( /The es6 format is deprecated/.test( msg ) ) warned = true;
} }
}).then( function ( bundle ) { }).then( bundle => {
bundle.generate({ format: 'es6' }); bundle.generate({ format: 'es6' });
assert.ok( warned ); assert.ok( warned );
}); });
}); });
}); });
describe( 'function', function () { describe( 'function', () => {
sander.readdirSync( FUNCTION ).sort().forEach( function ( dir ) { sander.readdirSync( FUNCTION ).sort().forEach( dir => {
if ( dir[0] === '.' ) return; // .DS_Store... if ( dir[0] === '.' ) return; // .DS_Store...
var config = loadConfig( FUNCTION + '/' + dir + '/_config.js' ); const config = loadConfig( FUNCTION + '/' + dir + '/_config.js' );
( config.skip ? it.skip : config.solo ? it.only : it )( dir, function () { ( config.skip ? it.skip : config.solo ? it.only : it )( dir, () => {
var warnings = []; let warnings = [];
var captureWarning = msg => warnings.push( msg ); const captureWarning = msg => warnings.push( msg );
var options = extend( { const options = extend( {
entry: FUNCTION + '/' + dir + '/main.js', entry: FUNCTION + '/' + dir + '/main.js',
onwarn: captureWarning onwarn: captureWarning
}, config.options ); }, config.options );
@ -177,16 +177,18 @@ describe( 'rollup', function () {
if ( config.solo ) console.group( dir ); if ( config.solo ) console.group( dir );
return rollup.rollup( options ) return rollup.rollup( options )
.then( function ( bundle ) { .then( bundle => {
var unintendedError; let unintendedError;
if ( config.error ) { if ( config.error ) {
throw new Error( 'Expected an error while rolling up' ); throw new Error( 'Expected an error while rolling up' );
} }
let result;
// try to generate output // try to generate output
try { try {
var result = bundle.generate( extend( {}, config.bundleOptions, { result = bundle.generate( extend( {}, config.bundleOptions, {
format: 'cjs' format: 'cjs'
})); }));
@ -204,7 +206,7 @@ describe( 'rollup', function () {
if ( unintendedError ) throw unintendedError; if ( unintendedError ) throw unintendedError;
if ( config.error || config.generateError ) return; if ( config.error || config.generateError ) return;
var code = result.code; let code = result.code;
if ( config.buble ) { if ( config.buble ) {
code = buble.transform( code, { code = buble.transform( code, {
@ -214,24 +216,17 @@ describe( 'rollup', function () {
if ( config.code ) config.code( code ); if ( config.code ) config.code( code );
var module = { let module = {
exports: {} exports: {}
}; };
var context = extend({ const context = extend({ require, module, assert, exports: module.exports }, config.context || {} );
require: require,
module: module,
exports: module.exports,
assert: assert
}, config.context || {} );
var contextKeys = Object.keys( context ); const contextKeys = Object.keys( context );
var contextValues = contextKeys.map( function ( key ) { const contextValues = contextKeys.map( key => context[ key ] );
return context[ key ];
});
try { try {
var fn = new Function( contextKeys, code ); const fn = new Function( contextKeys, code );
fn.apply( {}, contextValues ); fn.apply( {}, contextValues );
if ( config.runtimeError ) { if ( config.runtimeError ) {
@ -261,7 +256,7 @@ describe( 'rollup', function () {
if ( config.solo ) console.groupEnd(); if ( config.solo ) console.groupEnd();
if ( unintendedError ) throw unintendedError; if ( unintendedError ) throw unintendedError;
}, function ( err ) { }, err => {
if ( config.error ) { if ( config.error ) {
config.error( err ); config.error( err );
} else { } else {
@ -272,15 +267,15 @@ describe( 'rollup', function () {
}); });
}); });
describe( 'form', function () { describe( 'form', () => {
sander.readdirSync( FORM ).sort().forEach( function ( dir ) { sander.readdirSync( FORM ).sort().forEach( dir => {
if ( dir[0] === '.' ) return; // .DS_Store... if ( dir[0] === '.' ) return; // .DS_Store...
var config = loadConfig( FORM + '/' + dir + '/_config.js' ); const config = loadConfig( FORM + '/' + dir + '/_config.js' );
if ( config.skipIfWindows && process.platform === 'win32' ) return; if ( config.skipIfWindows && process.platform === 'win32' ) return;
var options = extend( {}, { const options = extend( {}, {
entry: FORM + '/' + dir + '/main.js', entry: FORM + '/' + dir + '/main.js',
onwarn: msg => { onwarn: msg => {
if ( /No name was provided for/.test( msg ) ) return; if ( /No name was provided for/.test( msg ) ) return;
@ -289,20 +284,20 @@ describe( 'rollup', function () {
} }
}, config.options ); }, config.options );
( config.skip ? describe.skip : config.solo ? describe.only : describe)( dir, function () { ( config.skip ? describe.skip : config.solo ? describe.only : describe)( dir, () => {
PROFILES.forEach( function ( profile ) { PROFILES.forEach( profile => {
it( 'generates ' + profile.format, function () { it( 'generates ' + profile.format, () => {
return rollup.rollup( options ).then( function ( bundle ) { return rollup.rollup( options ).then( bundle => {
var options = extend( {}, config.options, { const options = extend( {}, config.options, {
dest: FORM + '/' + dir + '/_actual/' + profile.format + '.js', dest: FORM + '/' + dir + '/_actual/' + profile.format + '.js',
format: profile.format format: profile.format
}); });
return bundle.write( options ).then( function () { return bundle.write( options ).then( () => {
var actualCode = normaliseOutput( sander.readFileSync( FORM, dir, '_actual', profile.format + '.js' ) ); const actualCode = normaliseOutput( sander.readFileSync( FORM, dir, '_actual', profile.format + '.js' ) );
var expectedCode; let expectedCode;
var actualMap; let actualMap;
var expectedMap; let expectedMap;
try { try {
expectedCode = normaliseOutput( sander.readFileSync( FORM, dir, '_expected', profile.format + '.js' ) ); expectedCode = normaliseOutput( sander.readFileSync( FORM, dir, '_expected', profile.format + '.js' ) );
@ -338,26 +333,24 @@ describe( 'rollup', function () {
}); });
}); });
describe( 'sourcemaps', function () { describe( 'sourcemaps', () => {
sander.readdirSync( SOURCEMAPS ).sort().forEach( function ( dir ) { sander.readdirSync( SOURCEMAPS ).sort().forEach( dir => {
if ( dir[0] === '.' ) return; // .DS_Store... if ( dir[0] === '.' ) return; // .DS_Store...
describe( dir, function () { describe( dir, () => {
process.chdir( SOURCEMAPS + '/' + dir ); process.chdir( SOURCEMAPS + '/' + dir );
var config = loadConfig( SOURCEMAPS + '/' + dir + '/_config.js' ); const config = loadConfig( SOURCEMAPS + '/' + dir + '/_config.js' );
var entry = path.resolve( SOURCEMAPS, dir, 'main.js' ); const entry = path.resolve( SOURCEMAPS, dir, 'main.js' );
var dest = path.resolve( SOURCEMAPS, dir, '_actual/bundle' ); const dest = path.resolve( SOURCEMAPS, dir, '_actual/bundle' );
var options = extend( {}, config.options, { const options = extend( {}, config.options, { entry });
entry: entry
});
PROFILES.forEach( function ( profile ) { PROFILES.forEach( profile => {
( config.skip ? it.skip : config.solo ? it.only : it )( 'generates ' + profile.format, function () { ( config.skip ? it.skip : config.solo ? it.only : it )( 'generates ' + profile.format, () => {
process.chdir( SOURCEMAPS + '/' + dir ); process.chdir( SOURCEMAPS + '/' + dir );
return rollup.rollup( options ).then( function ( bundle ) { return rollup.rollup( options ).then( bundle => {
var options = extend( {}, { const options = extend( {}, {
format: profile.format, format: profile.format,
sourceMap: true, sourceMap: true,
dest: `${dest}.${profile.format}.js` dest: `${dest}.${profile.format}.js`
@ -366,7 +359,7 @@ describe( 'rollup', function () {
bundle.write( options ); bundle.write( options );
if ( config.before ) config.before(); if ( config.before ) config.before();
var result = bundle.generate( options ); const result = bundle.generate( options );
config.test( result.code, result.map ); config.test( result.code, result.map );
}); });
}); });
@ -375,19 +368,19 @@ describe( 'rollup', function () {
}); });
}); });
describe( 'cli', function () { describe( 'cli', () => {
sander.readdirSync( CLI ).sort().forEach( function ( dir ) { sander.readdirSync( CLI ).sort().forEach( dir => {
if ( dir[0] === '.' ) return; // .DS_Store... if ( dir[0] === '.' ) return; // .DS_Store...
describe( dir, function () { describe( dir, () => {
var config = loadConfig( CLI + '/' + dir + '/_config.js' ); const config = loadConfig( CLI + '/' + dir + '/_config.js' );
( config.skip ? it.skip : config.solo ? it.only : it )( dir, function ( done ) { ( config.skip ? it.skip : config.solo ? it.only : it )( dir, done => {
process.chdir( config.cwd || path.resolve( CLI, dir ) ); process.chdir( config.cwd || path.resolve( CLI, dir ) );
const command = 'node ' + path.resolve( __dirname, '../bin' ) + path.sep + config.command; const command = 'node ' + path.resolve( __dirname, '../bin' ) + path.sep + config.command;
exec( command, {}, function ( err, code, stderr ) { exec( command, {}, ( err, code, stderr ) => {
if ( err ) { if ( err ) {
if ( config.error ) { if ( config.error ) {
config.error( err ); config.error( err );
@ -399,7 +392,7 @@ describe( 'rollup', function () {
if ( stderr ) console.error( stderr ); if ( stderr ) console.error( stderr );
var unintendedError; let unintendedError;
if ( config.execute ) { if ( config.execute ) {
try { try {
@ -409,8 +402,8 @@ describe( 'rollup', function () {
}).code; }).code;
} }
var fn = new Function( 'require', 'module', 'exports', 'assert', code ); const fn = new Function( 'require', 'module', 'exports', 'assert', code );
var module = { const module = {
exports: {} exports: {}
}; };
fn( require, module, module.exports, assert ); fn( require, module, module.exports, assert );
@ -449,10 +442,10 @@ describe( 'rollup', function () {
} }
else if ( sander.existsSync( '_expected' ) && sander.statSync( '_expected' ).isDirectory() ) { else if ( sander.existsSync( '_expected' ) && sander.statSync( '_expected' ).isDirectory() ) {
var error = null; let error = null;
sander.readdirSync( '_expected' ).forEach( child => { sander.readdirSync( '_expected' ).forEach( child => {
var expected = sander.readFileSync( path.join( '_expected', child ) ).toString(); const expected = sander.readFileSync( path.join( '_expected', child ) ).toString();
var actual = sander.readFileSync( path.join( '_actual', child ) ).toString(); const actual = sander.readFileSync( path.join( '_actual', child ) ).toString();
try { try {
assert.equal( normaliseOutput( actual ), normaliseOutput( expected ) ); assert.equal( normaliseOutput( actual ), normaliseOutput( expected ) );
} catch ( err ) { } catch ( err ) {
@ -463,7 +456,7 @@ describe( 'rollup', function () {
} }
else { else {
var expected = sander.readFileSync( '_expected.js' ).toString(); const expected = sander.readFileSync( '_expected.js' ).toString();
try { try {
assert.equal( normaliseOutput( code ), normaliseOutput( expected ) ); assert.equal( normaliseOutput( code ), normaliseOutput( expected ) );
done(); done();
@ -477,7 +470,7 @@ describe( 'rollup', function () {
}); });
}); });
describe('incremental', function () { describe('incremental', () => {
function executeBundle ( bundle ) { function executeBundle ( bundle ) {
const cjs = bundle.generate({ format: 'cjs' }); const cjs = bundle.generate({ format: 'cjs' });
const m = new Function( 'module', 'exports', cjs.code ); const m = new Function( 'module', 'exports', cjs.code );
@ -488,17 +481,17 @@ describe( 'rollup', function () {
return module.exports; return module.exports;
} }
var calls; let calls;
var modules; let modules;
var plugin = { const plugin = {
resolveId: id => id, resolveId: id => id,
load: id => { load: id => {
return modules[ id ]; return modules[ id ];
}, },
transform: function ( code ) { transform: code => {
calls += 1; calls += 1;
return code; return code;
} }
@ -513,7 +506,7 @@ describe( 'rollup', function () {
}; };
}); });
it('does not transforms in the second time', function () { it('does not transforms in the second time', () => {
return rollup.rollup({ return rollup.rollup({
entry: 'entry', entry: 'entry',
plugins: [ plugin ] plugins: [ plugin ]
@ -530,7 +523,7 @@ describe( 'rollup', function () {
}); });
}); });
it('transforms modified sources', function () { it('transforms modified sources', () => {
let cache; let cache;
return rollup.rollup({ return rollup.rollup({
@ -557,7 +550,7 @@ describe( 'rollup', function () {
describe( 'hooks', () => { describe( 'hooks', () => {
it( 'passes bundle & output object to ongenerate & onwrite hooks', () => { it( 'passes bundle & output object to ongenerate & onwrite hooks', () => {
var dest = path.join( __dirname, 'tmp/bundle.js' ); const dest = path.join( __dirname, 'tmp/bundle.js' );
return rollup.rollup({ return rollup.rollup({
entry: 'entry', entry: 'entry',
@ -583,7 +576,7 @@ describe( 'rollup', function () {
}); });
it( 'calls ongenerate hooks in sequence', () => { it( 'calls ongenerate hooks in sequence', () => {
var result = []; let result = [];
return rollup.rollup({ return rollup.rollup({
entry: 'entry', entry: 'entry',
@ -611,8 +604,8 @@ describe( 'rollup', function () {
}); });
it( 'calls onwrite hooks in sequence', () => { it( 'calls onwrite hooks in sequence', () => {
var result = []; let result = [];
var dest = path.join( __dirname, 'tmp/bundle.js' ); const dest = path.join( __dirname, 'tmp/bundle.js' );
return rollup.rollup({ return rollup.rollup({
entry: 'entry', entry: 'entry',

Loading…
Cancel
Save