Skip to content

Commit

Permalink
docs(core): Add eslint & lint
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbrg committed Jan 28, 2024
1 parent 26c143a commit 10e4191
Show file tree
Hide file tree
Showing 14 changed files with 853 additions and 28 deletions.
75 changes: 75 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
sourceType: "module",
tsconfigRootDir: __dirname,
},
plugins: [
"@typescript-eslint/eslint-plugin",
"unused-imports",
"perfectionist",
],
extends: [
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: [".eslintrc.js"],
rules: {
"perfectionist/sort-imports": [
"error",
{
type: "line-length",
order: "asc",
groups: [["builtin", "external"], "internal", "ruleSets"],
"custom-groups": {
value: {
ruleSets: "./rulesets/**",
},
},
"newlines-between": "always",
"max-line-length": 80,
},
],
"perfectionist/sort-named-imports": [
"error",
{
type: "line-length",
order: "asc",
},
],
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-empty-function": "off",
"prefer-rest-params": "off",
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
"warn",
{
vars: "all",
varsIgnorePattern: "^_",
args: "after-used",
argsIgnorePattern: "^_",
},
],
"@typescript-eslint/ban-types": [
"error",
{
types: {
Symbol: false,
Function: false,
},
extendDefaults: true,
},
],
},
};
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint \"{src,test}/**/*.ts\" --fix",
"badges": "jest-badges-readme",
"test": "jest --testPathPattern=test --detectOpenHandles --color --forceExit",
"build": "rm -rf dist && tsc",
"prettier": "prettier --write ."
"prettier": "prettier --write .",
"publish": "npm run build && npm publish --access public"
},
"repository": {
"type": "git",
Expand All @@ -30,9 +32,16 @@
"devDependencies": {
"@jest/globals": "^29.5.0",
"@olavoparno/jest-badges-readme": "^1.5.1",
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
"@types/jest": "^29.5.1",
"@types/node": "^18.16.1",
"axios": "^1.6.0",
"eslint": "^8.55.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-perfectionist": "^2.5.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-unused-imports": "^2.0.0",
"jest": "^29.5.0",
"prettier": "^2.8.8",
"ts-jest": "^29.1.0",
Expand Down
2 changes: 1 addition & 1 deletion prettier.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 120,
"printWidth": 80,
"singleQuote": true,
"trailingComma": "all"
}
2 changes: 1 addition & 1 deletion src/services/evaluator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ObjectDiscovery } from "./object-discovery";
import { Condition, Constraint, Rule } from "../types/rule";
import { Rule, Condition, Constraint } from "../types/rule";

export class Evaluator {
#objectDiscovery: ObjectDiscovery = new ObjectDiscovery();
Expand Down
2 changes: 1 addition & 1 deletion src/services/object-discovery.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Condition, ConditionType, Constraint, Rule } from "../types/rule";
import { Rule, Condition, Constraint, ConditionType } from "../types/rule";

export class ObjectDiscovery {
/**
Expand Down
8 changes: 4 additions & 4 deletions src/services/rule-pilot.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Builder } from "./builder";
import { Mutator } from "./mutator";
import { Evaluator } from "./evaluator";
import { Introspector } from "./introspector";
import { ValidationResult, Validator } from "./validator";

import { IntrospectionResult, Rule } from "../types/rule";
import { RuleError } from "../types/error";
import { Introspector } from "./introspector";
import { Validator, ValidationResult } from "./validator";
import { Rule, IntrospectionResult } from "../types/rule";

export class RulePilot {
static #rulePilot = new RulePilot();
Expand Down Expand Up @@ -70,6 +69,7 @@ export class RulePilot {
* @param trustRule Set true to avoid validating the rule before evaluating it (faster).
* @throws RuleError if the rule is invalid.
*/

async evaluate<T>(
rule: Rule,
criteria: object | object[],
Expand Down
6 changes: 3 additions & 3 deletions src/services/validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ObjectDiscovery } from "./object-discovery";
import { Condition, Constraint, Operator, Rule } from "../types/rule";
import { Rule, Operator, Condition, Constraint } from "../types/rule";

export interface ValidationResult {
isValid: boolean;
Expand All @@ -18,7 +18,7 @@ export class Validator {
*/
validate(rule: Rule): ValidationResult {
// Assume the rule is valid.
let result: ValidationResult = { isValid: true };
const result: ValidationResult = { isValid: true };

// Check the rule is a valid JSON
if (!this.objectDiscovery.isObject(rule)) {
Expand Down Expand Up @@ -71,7 +71,7 @@ export class Validator {
depth: number = 0
): ValidationResult {
// Check to see if the condition is valid.
let result = this.isValidCondition(condition);
const result = this.isValidCondition(condition);
if (!result.isValid) {
return result;
}
Expand Down
5 changes: 2 additions & 3 deletions test/engine.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { RulePilot, Operator, RuleError } from "../src";

import { valid1Json } from "./rulesets/valid1.json";
import { valid2Json } from "./rulesets/valid2.json";
import { valid3Json } from "./rulesets/valid3.json";
import { valid5Json } from "./rulesets/valid5.json";

import { invalid1Json } from "./rulesets/invalid1.json";
import { invalid2Json } from "./rulesets/invalid2.json";

import { Operator, RulePilot, RuleError } from "../src";

describe("RulePilot engine correctly", () => {
it("Evaluates a simple ruleset", async () => {
expect(
Expand Down
4 changes: 2 additions & 2 deletions test/introspector.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { RuleError, RulePilot, RuleTypeError } from "../src";

import { valid2Json } from "./rulesets/valid2.json";
import { valid3Json } from "./rulesets/valid3.json";
import { valid4Json } from "./rulesets/valid4.json";
Expand All @@ -9,6 +7,8 @@ import { valid8Json } from "./rulesets/valid8.json";
import { valid9Json } from "./rulesets/valid9.json";
import { invalid1Json } from "./rulesets/invalid1.json";

import { RuleError, RulePilot, RuleTypeError } from "../src";

describe("RulePilot introspector correctly", () => {
it("Detects invalid rules", async () => {
expect(() => RulePilot.introspect(valid2Json)).toThrow(RuleTypeError);
Expand Down
3 changes: 2 additions & 1 deletion test/mutator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import axios from "axios";
import { beforeEach } from "@jest/globals";

import { RulePilot } from "../src";
import { valid1Json } from "./rulesets/valid1.json";
import { valid3Json } from "./rulesets/valid3.json";

import { RulePilot } from "../src";

const mutation1 = async (value: unknown) => {
const result = await axios.get<{ cca2: any }>(
`https://restcountries.com/v3.1/name/${value}?fullText=true`
Expand Down
2 changes: 1 addition & 1 deletion test/rulesets/invalid2.json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Condition, Rule } from "../../src";
import { Rule, Condition } from "../../src";

export const invalid2Json: Rule = {
conditions: [
Expand Down
4 changes: 2 additions & 2 deletions test/rulesets/valid5.json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export const valid5Json: Rule = {
{
field: "countries",
operator: "contains",
value: "US"
value: "US",
},
{
field: "states",
operator: "contains any",
value: ["KY", "TN"]
value: ["KY", "TN"],
},
],
},
Expand Down
5 changes: 2 additions & 3 deletions test/validator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { RulePilot, Operator, Condition, Constraint } from "../src";

import { valid1Json } from "./rulesets/valid1.json";
import { valid3Json } from "./rulesets/valid3.json";

import { invalid1Json } from "./rulesets/invalid1.json";

import { Operator, RulePilot, Condition, Constraint } from "../src";

describe("RulePilot validator correctly", () => {
it("Identifies a bad operator", () => {
expect(
Expand Down
Loading

0 comments on commit 10e4191

Please sign in to comment.