|
/** |
|
* @fileoverview dynamicImport requires a leading comment with the webpackChunkName |
|
* @author Kimberly Strauch |
|
* @copyright 2018 Kimberly Strauch. All rights reserved. |
|
* See LICENSE file in root directory for full license. |
|
*/ |
|
|
|
'use strict'; |
|
|
|
const rule = require('../../../lib/rules/dynamic-import-requires-webpack-chunkname'); |
|
const RuleTester = require('eslint').RuleTester; |
|
|
|
const ruleTester = new RuleTester(); |
|
|
|
const importReplacement = 'dynamicImport'; |
|
const options = [{ importReplacement }]; |
|
|
|
const noLeadingCommentError = `${importReplacement} requires a leading block comment in the form: /* webpackChunkName: "moduleChunkName" */`; |
|
const commentFormattingError = `${importReplacement} requires the leading comment to be in the form: /* webpackChunkName: "moduleChunkName" */`; |
|
const nonBlockCommentError = `${importReplacement} must use a /* foo */ style comment, not a // foo comment`; |
|
const numberOfArgumentsError = `${importReplacement} requires exactly 1 argument`; |
|
const nonStringArgumentError = `${importReplacement} requires a string literal argument`; |
|
|
|
ruleTester.run('dynamic-import-requires-webpack-chunkname', rule, { |
|
|
|
valid: [ |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName: "someModule" */ |
|
'test' |
|
)`, |
|
options, |
|
}, |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName: "Some_Other_Module" */ |
|
'test' |
|
)`, |
|
options, |
|
}, |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName: "Some_Other_Module_123" */ |
|
'test' |
|
)`, |
|
options, |
|
}, |
|
], |
|
|
|
invalid: [ |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName: "someModule" */ |
|
test |
|
)`, |
|
options, |
|
errors: [{ |
|
message: nonStringArgumentError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: 'dynamicImport(someModule)', |
|
options, |
|
errors: [{ |
|
message: nonStringArgumentError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: `dynamicImport( |
|
// webpackChunkName: "someModule" |
|
'someModule' |
|
)`, |
|
options, |
|
errors: [{ |
|
message: nonBlockCommentError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName: "someModule" */ |
|
test |
|
)`, |
|
options, |
|
errors: [{ |
|
message: nonStringArgumentError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: 'dynamicImport()', |
|
options, |
|
errors: [{ |
|
message: numberOfArgumentsError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName: "someModule" */ |
|
)`, |
|
options, |
|
errors: [{ |
|
message: numberOfArgumentsError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName: "someModule" */ |
|
'someModule', |
|
'someOtherModule' |
|
)`, |
|
options, |
|
errors: [{ |
|
message: numberOfArgumentsError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: 'dynamicImport(\'test\')', |
|
options, |
|
errors: [{ |
|
message: noLeadingCommentError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName: someModule */ |
|
'someModule' |
|
)`, |
|
options, |
|
errors: [{ |
|
message: commentFormattingError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName: 'someModule' */ |
|
'someModule' |
|
)`, |
|
options, |
|
errors: [{ |
|
message: commentFormattingError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName "someModule" */ |
|
'someModule' |
|
)`, |
|
options, |
|
errors: [{ |
|
message: commentFormattingError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
{ |
|
code: `dynamicImport( |
|
/* webpackChunkName:"someModule" */ |
|
'someModule' |
|
)`, |
|
options, |
|
errors: [{ |
|
message: commentFormattingError, |
|
type: 'CallExpression', |
|
}], |
|
}, |
|
], |
|
}); |