eslint-plugin-canonical
Version:
Canonical linting rules for ESLint.
86 lines (85 loc) • 3.07 kB
JavaScript
;
/**
* @file Rule to ensure that filenames match a convention (default: camelCase)
* @author Stefan Lau
* @see https://github.com/selaux/eslint-plugin-filenames/blob/32fc70dd7572211d1e5b97e06ec7a005c77fe8d4/lib/rules/match-regex.js
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const node_path_1 = __importDefault(require("node:path"));
const utilities_1 = require("../utilities");
// eslint-disable-next-line unicorn/no-unsafe-regex
const defaultRegexp = /^[\da-z]+(?:[A-Z][\da-z]+)*$/gu;
exports.default = (0, utilities_1.createRule)({
create: (context, [options]) => {
var _a;
const conventionRegexp = options.regex
? new RegExp(options.regex, 'u')
: defaultRegexp;
const ignoreExporting = options.ignoreExporting;
const filename = (_a = context.filename) !== null && _a !== void 0 ? _a : context.getFilename();
return {
Program(node) {
conventionRegexp.lastIndex = 0;
const absoluteFilename = node_path_1.default.resolve(filename);
const parsed = (0, utilities_1.parseFilename)(absoluteFilename);
const shouldIgnore = (0, utilities_1.isIgnoredFilename)(filename);
const matchesRegex = conventionRegexp.test(parsed.name);
if (shouldIgnore) {
return;
}
const isExporting = Boolean((0, utilities_1.getExportedName)(node, false));
if (ignoreExporting && isExporting) {
return;
}
if (!matchesRegex) {
context.report({
data: {
name: parsed.base,
},
messageId: 'notMatch',
node,
});
}
},
};
},
defaultOptions: [
{
ignoreExporting: false,
regex: null,
},
],
meta: {
docs: {
description: 'Enforce a certain file naming convention using a regular expression.',
},
messages: {
notMatch: "Filename '{{name}}' does not match the naming convention.",
},
schema: [
{
properties: {
ignoreExporting: {
type: 'boolean',
},
regex: {
oneOf: [
{
type: 'string',
},
{
type: 'null',
},
],
},
},
type: 'object',
},
],
type: 'suggestion',
},
name: 'filename-match-regex',
});