@shayanthenerd/eslint-config
Version:
A modern, flexible ESLint configuration for enforcing best practices and maintaining a consistent coding style
142 lines (141 loc) • 3.48 kB
JavaScript
import { isEnabled } from "../utils/isEnabled.mjs";
import { defaultOptions } from "../helpers/options/defaultOptions.mjs";
//#region src/rules/perfectionist.ts
const literalCustomGroups = [
{
groupName: "template-literal",
selector: "literal",
elementNamePattern: "^`.*`$"
},
{
groupName: "bigint-literal",
selector: "literal",
elementNamePattern: "^-?\\d[\\d_]*n$"
},
{
groupName: "string-literal",
selector: "literal",
elementNamePattern: "^(?:'.*'|\".*\")$"
},
{
groupName: "boolean-literal",
selector: "literal",
elementNamePattern: "^(?:true|false)$"
},
{
groupName: "number-literal",
selector: "literal",
elementNamePattern: "^-?(?:\\d[\\d_]*(?:\\.\\d[\\d_]*)?|\\.\\d[\\d_]*)(?:[eE][+-]?\\d[\\d_]*)?$"
}
];
const booleanKeywordCustomGroup = {
groupName: "boolean-keyword",
selector: "keyword",
elementNamePattern: "^boolean$"
};
const sharedTypeLikeGroups = [
"boolean-literal",
"number-literal",
"bigint-literal",
"string-literal",
"template-literal",
"literal",
"keyword",
"named",
"operator",
"tuple",
"object",
"union",
"intersection",
"conditional",
"function",
"import",
"unknown",
"nullish"
];
const typeConstituentSortOptions = {
customGroups: [booleanKeywordCustomGroup, ...literalCustomGroups],
groups: ["boolean-keyword", ...sharedTypeLikeGroups]
};
const sortArrayIncludesOptions = {
customGroups: literalCustomGroups,
groups: [
"boolean-literal",
"number-literal",
"bigint-literal",
"string-literal",
"template-literal",
"literal",
"unknown"
]
};
const sortImportsCustomGroups = [{
groupName: "component",
elementNamePattern: ["\\.(vue|[jt]sx)$"],
modifiers: ["value"]
}, {
groupName: "image",
elementNamePattern: ["\\.(ico|svg|gif|png|jpe?g|webp|avif|heic)$"]
}];
const sortImportsGroups = [
["style", "side-effect-style"],
{ newlinesBetween: 0 },
"side-effect",
"image",
"component",
"type-external",
{ newlinesBetween: 0 },
"type-builtin",
{ newlinesBetween: 0 },
["type-tsconfig-path", "type-subpath"],
{ newlinesBetween: 0 },
[
"type-internal",
"type-index",
"type-parent",
"type-sibling"
],
"external",
{ newlinesBetween: 0 },
"builtin",
["tsconfig-path", "subpath"],
{ newlinesBetween: 0 },
[
"internal",
"index",
"parent",
"sibling"
],
["import", "unknown"]
];
function getPerfectionistRules(options) {
const { env, tsConfig, configs: { stylistic, perfectionist } } = options;
const { maxLineLength } = isEnabled(stylistic) ? stylistic : defaultOptions.configs.stylistic;
const { sortType } = isEnabled(perfectionist) ? perfectionist : defaultOptions.configs.perfectionist;
return {
"perfectionist/sort-maps": "warn",
"perfectionist/sort-exports": "warn",
"perfectionist/sort-union-types": ["warn", typeConstituentSortOptions],
"perfectionist/sort-array-includes": ["warn", sortArrayIncludesOptions],
"perfectionist/sort-intersection-types": ["warn", typeConstituentSortOptions],
"perfectionist/sort-named-imports": "warn",
"perfectionist/sort-named-exports": "warn",
"perfectionist/sort-imports": ["warn", {
environment: env === "bun" ? env : "node",
tsconfig: tsConfig || { rootDir: "" },
sortSideEffects: true,
fallbackSort: {
order: "asc",
type: "natural"
},
partitionByComment: true,
specialCharacters: "trim",
type: sortType,
maxLineLength,
customGroups: sortImportsCustomGroups,
groups: sortImportsGroups
}]
};
}
//#endregion
export { getPerfectionistRules };