83 lines
8.7 KiB
JavaScript
83 lines
8.7 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
/**
|
||
|
* @fileoverview Rule to disallow anonymous default exports.
|
||
|
* @author Duncan Beevers
|
||
|
*/
|
||
|
|
||
|
const defs = {
|
||
|
ArrayExpression: {
|
||
|
option: 'allowArray',
|
||
|
description: 'If `false`, will report default export of an array',
|
||
|
message: 'Assign array to a variable before exporting as module default'
|
||
|
},
|
||
|
ArrowFunctionExpression: {
|
||
|
option: 'allowArrowFunction',
|
||
|
description: 'If `false`, will report default export of an arrow function',
|
||
|
message: 'Assign arrow function to a variable before exporting as module default'
|
||
|
},
|
||
|
ClassDeclaration: {
|
||
|
option: 'allowAnonymousClass',
|
||
|
description: 'If `false`, will report default export of an anonymous class',
|
||
|
message: 'Unexpected default export of anonymous class',
|
||
|
forbid: node => !node.declaration.id
|
||
|
},
|
||
|
FunctionDeclaration: {
|
||
|
option: 'allowAnonymousFunction',
|
||
|
description: 'If `false`, will report default export of an anonymous function',
|
||
|
message: 'Unexpected default export of anonymous function',
|
||
|
forbid: node => !node.declaration.id
|
||
|
},
|
||
|
Literal: {
|
||
|
option: 'allowLiteral',
|
||
|
description: 'If `false`, will report default export of a literal',
|
||
|
message: 'Assign literal to a variable before exporting as module default'
|
||
|
},
|
||
|
ObjectExpression: {
|
||
|
option: 'allowObject',
|
||
|
description: 'If `false`, will report default export of an object expression',
|
||
|
message: 'Assign object to a variable before exporting as module default'
|
||
|
},
|
||
|
TemplateLiteral: {
|
||
|
option: 'allowLiteral',
|
||
|
description: 'If `false`, will report default export of a literal',
|
||
|
message: 'Assign literal to a variable before exporting as module default'
|
||
|
}
|
||
|
};
|
||
|
|
||
|
const schemaProperties = Object.keys(defs).map(key => defs[key]).reduce((acc, def) => {
|
||
|
acc[def.option] = {
|
||
|
description: def.description,
|
||
|
type: 'boolean',
|
||
|
default: false
|
||
|
};
|
||
|
|
||
|
return acc;
|
||
|
}, {});
|
||
|
|
||
|
module.exports = {
|
||
|
meta: {
|
||
|
schema: [{
|
||
|
type: 'object',
|
||
|
properties: schemaProperties,
|
||
|
'additionalProperties': false
|
||
|
}]
|
||
|
},
|
||
|
|
||
|
create: function (context) {
|
||
|
const options = Object.assign({}, context.options[0]);
|
||
|
|
||
|
return {
|
||
|
'ExportDefaultDeclaration': node => {
|
||
|
const def = defs[node.declaration.type];
|
||
|
|
||
|
// Recognized node type and allowed by configuration,
|
||
|
// and has no forbid check, or forbid check return value is truthy
|
||
|
if (def && !options[def.option] && (!def.forbid || def.forbid(node))) {
|
||
|
context.report({ node, message: def.message });
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInJ1bGVzL25vLWFub255bW91cy1kZWZhdWx0LWV4cG9ydC5qcyJdLCJuYW1lcyI6WyJkZWZzIiwiQXJyYXlFeHByZXNzaW9uIiwib3B0aW9uIiwiZGVzY3JpcHRpb24iLCJtZXNzYWdlIiwiQXJyb3dGdW5jdGlvbkV4cHJlc3Npb24iLCJDbGFzc0RlY2xhcmF0aW9uIiwiZm9yYmlkIiwibm9kZSIsImRlY2xhcmF0aW9uIiwiaWQiLCJGdW5jdGlvbkRlY2xhcmF0aW9uIiwiTGl0ZXJhbCIsIk9iamVjdEV4cHJlc3Npb24iLCJUZW1wbGF0ZUxpdGVyYWwiLCJzY2hlbWFQcm9wZXJ0aWVzIiwiT2JqZWN0Iiwia2V5cyIsIm1hcCIsImtleSIsInJlZHVjZSIsImFjYyIsImRlZiIsInR5cGUiLCJkZWZhdWx0IiwibW9kdWxlIiwiZXhwb3J0cyIsIm1ldGEiLCJzY2hlbWEiLCJwcm9wZXJ0aWVzIiwiY3JlYXRlIiwiY29udGV4dCIsIm9wdGlvbnMiLCJhc3NpZ24iLCJyZXBvcnQiXSwibWFwcGluZ3MiOiI7O0FBQUE7Ozs7O0FBS0EsTUFBTUEsT0FBTzt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