Skip to content

Commit

Permalink
feat(preset): add configuration presets
Browse files Browse the repository at this point in the history
  • Loading branch information
Mendes Hugo committed Jul 2, 2023
1 parent a795f19 commit 6aa5cc3
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
}
},
{
"files": ["jest.config.ts", "src/index.ts"],
"files": ["jest.config.ts", "src/index.ts", "src/configs/*.ts"],
"rules": {
"import/no-default-export": "off"
}
Expand Down
6 changes: 5 additions & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import * as path from "path";
import { JestConfigWithTsJest } from "ts-jest";

export default {
collectCoverageFrom: ["<rootDir>/src/**/*.ts", "!<rootDir>/src/**/index.ts"],
collectCoverageFrom: [
"<rootDir>/src/**/*.ts",
"!<rootDir>/src/**/index.ts",
"!<rootDir>/src/configs/*.ts"
],
coverageThreshold: { global: { branches: 75, functions: 75, lines: 75, statements: 75 } },
moduleFileExtensions: ["js", "ts"],
setupFilesAfterEnv: [path.resolve(__dirname, "./tools/jest/jest-extended.ts")],
Expand Down
31 changes: 31 additions & 0 deletions src/configs/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { TSESLint } from "@typescript-eslint/utils";
import { Linter } from "eslint";

import { SortRuleOptions } from "../lib/sort-rule";
import {
SORT_ON_ACCESSORS_NAME,
SORT_ON_CLASSES_NAME,
SORT_ON_METHODS_NAME,
SORT_ON_PARAMETERS_NAME,
SORT_ON_PROPERTIES_NAME
} from "../rules";

export const PLUGIN_NAME = "sort-decorators";

/**
* @param ruleEntry the rule entry to set to all rules
* @returns A configuration with all its rules set with the given entry
*/
export function createConfiguration(ruleEntry: Linter.RuleEntry<[SortRuleOptions]>) {
return {
parser: "@typescript-eslint/parser",
plugins: [PLUGIN_NAME],
rules: {
[`${PLUGIN_NAME}/${SORT_ON_ACCESSORS_NAME}`]: ruleEntry,
[`${PLUGIN_NAME}/${SORT_ON_CLASSES_NAME}`]: ruleEntry,
[`${PLUGIN_NAME}/${SORT_ON_METHODS_NAME}`]: ruleEntry,
[`${PLUGIN_NAME}/${SORT_ON_PARAMETERS_NAME}`]: ruleEntry,
[`${PLUGIN_NAME}/${SORT_ON_PROPERTIES_NAME}`]: ruleEntry
}
} as const satisfies TSESLint.Linter.Config;
}
2 changes: 2 additions & 0 deletions src/configs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { recommended } from "./recommended";
export { strict } from "./strict";
3 changes: 3 additions & 0 deletions src/configs/recommended.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createConfiguration } from "./common";

export const recommended = createConfiguration("warn");
3 changes: 3 additions & 0 deletions src/configs/strict.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { createConfiguration } from "./common";

export const strict = createConfiguration(["error", { autoFix: true }]);
5 changes: 5 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TSESLint } from "@typescript-eslint/utils";

import * as configs from "./configs";
import {
SORT_ON_ACCESSORS_NAME,
SORT_ON_CLASSES_NAME,
Expand All @@ -14,6 +15,10 @@ import {
} from "./rules";

export default {
configs: {
recommended: configs.recommended,
strict: configs.strict
},
rules: {
[SORT_ON_ACCESSORS_NAME]: sortOnAccessors,
[SORT_ON_CLASSES_NAME]: sortOnClasses,
Expand Down

0 comments on commit 6aa5cc3

Please sign in to comment.