Skip to content

Commit

Permalink
fix: abstract rule imports & use getters
Browse files Browse the repository at this point in the history
\### Rationale
In order to facilitate better testing of the correct imported rules,
the rules imports need to be abstracted rather than using a global var
which is not abstractable and controllable for invalid case checking.

Also converted to getter functions to ensure imports are low
computation during import but when accessed the computation occurs.
  • Loading branch information
codejedi365 committed Oct 23, 2021
1 parent c27c6c4 commit e3266da
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 32 deletions.
47 changes: 15 additions & 32 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,11 @@
// Plugin Definition
//------------------------------------------------------------------------------

import type {
RuleListener,
RuleMetaDataDocs,
RuleModule
} from "@typescript-eslint/experimental-utils/dist/ts-eslint";
import noDebug from "./rules/no-debug";
import noOnly from "./rules/no-only";
import noSkip from "./rules/no-skip";
import noIdenticalTitle from "./rules/no-identical-title";
import expectExpect from "./rules/expect-expect";
import type { Linter } from "eslint";
import rulebook from "./rules";

export const rules: {
[key: string]: RuleModule<string, unknown[], RuleListener>;
} = {
noDebug,
noSkip,
noOnly,
noIdenticalTitle,
expectExpect
};

export const generateRecommendedConfig = (): Record<
string,
RuleMetaDataDocs["recommended"]
> => {
return Object.entries(rules).reduce((memo, [name, rule]) => {
export const generateRecommendedConfig = (): Partial<Linter.RulesRecord> => {
return Object.entries(rulebook.rules).reduce((memo, [name, rule]) => {
return !rule.meta.docs
? memo
: {
Expand All @@ -37,13 +16,17 @@ export const generateRecommendedConfig = (): Record<
}, {});
};

export const { rules } = rulebook;

export const configs = {
recommended: {
globals: {
fixture: false,
test: false
},
plugins: ["testcafe-community"],
rules: generateRecommendedConfig()
get recommended(): Linter.Config {
return {
globals: {
fixture: false,
test: false
},
plugins: ["testcafe-community"],
rules: generateRecommendedConfig()
};
}
};
30 changes: 30 additions & 0 deletions lib/rules/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @fileoverview easy import file to grab all rules in folder
* @author codejedi365
*/
import type {
RuleListener,
RuleModule
} from "@typescript-eslint/experimental-utils/dist/ts-eslint";

// Add Import for any new rules to this list with camelCase
import noDebug from "./no-debug";
import noOnly from "./no-only";
import noSkip from "./no-skip";
import noIdenticalTitle from "./no-identical-title";
import expectExpect from "./expect-expect";

// Add export entry to this object for rule definition to be recognized
export default {
get rules(): {
[key: string]: RuleModule<string, unknown[], RuleListener>;
} {
return {
noDebug,
noSkip,
noOnly,
noIdenticalTitle,
expectExpect
};
}
};

0 comments on commit e3266da

Please sign in to comment.