Skip to content

Commit

Permalink
Fix filename-match-export
Browse files Browse the repository at this point in the history
Some export statements were (wrongly) ignored.
Fixes #31
  • Loading branch information
Xavier Dupessey committed Jul 16, 2024
1 parent 133ad9d commit b3e2554
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions lib/rules/filename-match-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

const path = require('path');

const clean = (value) => (value ?? '').toLowerCase().replace(/[^a-z0-9]+/g, '');

const moreInfo = `More info: https://github.com/criteo/eslint-plugin-criteo#filename-match-export`;

module.exports = {
Expand Down Expand Up @@ -33,15 +35,32 @@ module.exports = {
const options = context.options[0] || {};
const removeFromFilename = options.removeFromFilename || [];

const lowercaseExported = new Set();
const exportedClean = new Set();
const exported = [];

return {
ExportNamedDeclaration: function (node) {
if (node.declaration.id && node.declaration.id.name) {
exported.push(node.declaration.id.name);
lowercaseExported.add(node.declaration.id.name.toLowerCase());
const exportedNames = [];

// ClassDeclaration
if (node.declaration?.id?.name) {
exportedNames.push(node.declaration.id.name);
}

// VariableDeclarator
else if (node.declaration?.declarations?.length) {
exportedNames.push(...node.declaration.declarations.map((declaration) => declaration.id?.name));
}

// ExportNamedDeclaration
else if (node.specifiers?.length) {
exportedNames.push(...node.specifiers.map((specifier) => specifier.exported?.name));
}

exportedNames.forEach((exportedName) => {
exported.push(exportedName);
exportedClean.add(clean(exportedName));
});
},
'Program:exit': function (node) {
// Nothing is exported, allow any name
Expand All @@ -56,9 +75,9 @@ module.exports = {
filenameClean = filenameClean.replace(remove, '');
}

filenameClean = filenameClean.toLowerCase().replace(/[^a-z0-9]+/g, '');
filenameClean = clean(filenameClean);

if (!lowercaseExported.has(filenameClean)) {
if (!exportedClean.has(filenameClean)) {
return context.report({
data: { filename: filename.base, exported: exported.join('" | "') },
messageId: 'error',
Expand Down

0 comments on commit b3e2554

Please sign in to comment.