Skip to content

Commit

Permalink
fix: detect rules with TypeScript export = rule syntax (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Jul 11, 2022
1 parent 8b642c9 commit 8e66bf7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ function isTypeScriptRuleHelper(node) {
function getRuleExportsESM(ast, scopeManager) {
return (
ast.body
.filter((statement) => statement.type === 'ExportDefaultDeclaration')
.map((statement) => statement.declaration)
.filter((statement) =>
[
'ExportDefaultDeclaration', // export default rule;
'TSExportAssignment', // export = rule;
].includes(statement.type)
)
.map((statement) => statement.declaration || statement.expression)
// eslint-disable-next-line unicorn/prefer-object-from-entries
.reduce((currentExports, node) => {
if (node.type === 'ObjectExpression') {
Expand Down
62 changes: 62 additions & 0 deletions tests/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ describe('utils', () => {
'const notRule = foo(); export default notRule;',
'const notRule = function(){}; export default notRule;',
'const notRule = {}; export default notRule;',
'const notRule = {}; export = notRule;',
'const notRule: Rule.RuleModule = {}; export = notRule;',
'export = {};',
].forEach((noRuleCase) => {
it(`returns null for ${noRuleCase}`, () => {
const ast = typescriptEslintParser.parse(noRuleCase, {
Expand Down Expand Up @@ -188,6 +191,65 @@ describe('utils', () => {
isNewStyle: true,
},

// No helper.
'export default { create() {}, meta: {} };': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
// No helper, `export =` syntax.
'export = { create() {}, meta: {} };': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
// No helper, variable.
'const rule = { create() {}, meta: {} }; export default rule;': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
// no helper, variable with type.
'const rule: Rule.RuleModule = { create() {}, meta: {} }; export default rule;':
{
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
// No helper, variable, `export =` syntax.
'const rule = { create() {}, meta: {} }; export = rule;': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
// No helper, variable with type, `export =` syntax.
'const rule: Rule.RuleModule = { create() {}, meta: {} }; export = rule;':
{
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
// Helper, variable, `export =` syntax.
'const rule = createESLintRule({ create() {}, meta: {} }); export = rule;':
{
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
// Helper, variable with type, `export =` syntax.
'const rule: Rule.RuleModule = createESLintRule({ create() {}, meta: {} }); export = rule;':
{
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},
// Helper, `export =` syntax.
'export = createESLintRule({ create() {}, meta: {} });': {
create: { type: 'FunctionExpression' },
meta: { type: 'ObjectExpression' },
isNewStyle: true,
},

// Util function with "{} as const".
'export default createESLintRule({ create() {}, meta: {} as const });':
{
Expand Down

0 comments on commit 8e66bf7

Please sign in to comment.