Skip to content

Commit

Permalink
fix: override settings correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
schoero committed Jan 28, 2025
1 parent 3773e5f commit 6e27500
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 111 deletions.
20 changes: 10 additions & 10 deletions src/options/descriptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { validate } from "json-schema";
import { describe, expect, test } from "vitest";

import { getAttributesSchema, getCalleeSchema, getVariableSchema } from "readable-tailwind:options:descriptions.js";
import { ATTRIBUTE_SCHEMA, CALLEE_SCHEMA, VARIABLE_SCHEMA } from "readable-tailwind:options:descriptions.js";
import { MatcherType } from "readable-tailwind:types:rule.js";

import type { AttributeOption, CalleeOption, VariableOption } from "readable-tailwind:types:rule.js";
Expand All @@ -19,7 +19,7 @@ describe("descriptions", () => {
} satisfies AttributeOption;

expect(
validate(attributes, getAttributesSchema(attributes))
validate(attributes, ATTRIBUTE_SCHEMA)
).toStrictEqual(
{ errors: [], valid: true }
);
Expand All @@ -31,7 +31,7 @@ describe("descriptions", () => {
} satisfies CalleeOption;

expect(
validate(callees, getCalleeSchema(callees))
validate(callees, CALLEE_SCHEMA)
).toStrictEqual(
{ errors: [], valid: true }
);
Expand All @@ -44,7 +44,7 @@ describe("descriptions", () => {
} satisfies VariableOption;

expect(
validate(variable, getVariableSchema(variable))
validate(variable, VARIABLE_SCHEMA)
).toStrictEqual(
{ errors: [], valid: true }
);
Expand All @@ -61,7 +61,7 @@ describe("descriptions", () => {
} satisfies AttributeOption;

expect(
validate(attributes, getAttributesSchema(attributes))
validate(attributes, ATTRIBUTE_SCHEMA)
).toStrictEqual(
{ errors: [], valid: true }
);
Expand All @@ -74,7 +74,7 @@ describe("descriptions", () => {
} satisfies CalleeOption;

expect(
validate(callees, getCalleeSchema(callees))
validate(callees, CALLEE_SCHEMA)
).toStrictEqual(
{ errors: [], valid: true }
);
Expand All @@ -87,7 +87,7 @@ describe("descriptions", () => {
} satisfies VariableOption;

expect(
validate(variable, getVariableSchema(variable))
validate(variable, VARIABLE_SCHEMA)
).toStrictEqual(
{ errors: [], valid: true }
);
Expand Down Expand Up @@ -117,7 +117,7 @@ describe("descriptions", () => {
};

expect(
validate(attributes, getAttributesSchema(attributes))
validate(attributes, ATTRIBUTE_SCHEMA)
).toStrictEqual(
{ errors: [], valid: true }
);
Expand All @@ -143,7 +143,7 @@ describe("descriptions", () => {
};

expect(
validate(callees, getCalleeSchema(callees))
validate(callees, CALLEE_SCHEMA)
).toStrictEqual(
{ errors: [], valid: true }
);
Expand All @@ -169,7 +169,7 @@ describe("descriptions", () => {
};

expect(
validate(variable, getVariableSchema(variable))
validate(variable, VARIABLE_SCHEMA)
).toStrictEqual(
{ errors: [], valid: true }
);
Expand Down
125 changes: 57 additions & 68 deletions src/options/descriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,6 @@ import { MatcherType } from "readable-tailwind:types:rule.js";
import type { Rule } from "eslint";


export function getAttributesSchema(defaultValue: unknown) {
return {
attributes: {
default: defaultValue,
description: "List of attribute names that should get linted.",
items: {
anyOf: [
ATTRIBUTE_NAME_CONFIG,
ATTRIBUTE_REGEX_CONFIG,
ATTRIBUTE_MATCHER_CONFIG
]
},
type: "array"
}
} satisfies Rule.RuleMetaData["schema"];
}

export function getCalleeSchema(defaultValue: unknown) {
return {
callees: {
default: defaultValue,
description: "List of function names which arguments should get linted.",
items: {
anyOf: [
CALLEE_REGEX_CONFIG,
CALLEE_MATCHER_CONFIG,
CALLEE_NAME_CONFIG
]
},
type: "array"
}
} satisfies Rule.RuleMetaData["schema"];
}

export function getVariableSchema(defaultValue: unknown) {
return {
variables: {
default: defaultValue,
description: "List of variable names which values should get linted.",
items: {
anyOf: [
VARIABLE_REGEX_CONFIG,
VARIABLE_MATCHER_CONFIG,
VARIABLE_NAME_CONFIG
]
},
type: "array"
}
} satisfies Rule.RuleMetaData["schema"];
}

export function getTagsSchema(defaultValue: unknown) {
return {
tags: {
default: defaultValue,
description: "List of template literal tags that should get linted.",
items: {
anyOf: [
TAG_REGEX_CONFIG,
TAG_MATCHER_CONFIG,
TAG_NAME_CONFIG
]
},
type: "array"
}
} satisfies Rule.RuleMetaData["schema"];
}

const STRING_MATCHER_SCHEMA = {
properties: {
match: {
Expand Down Expand Up @@ -283,3 +215,60 @@ const TAG_NAME_CONFIG = {
description: "Template literal tag that should get linted.",
type: "string"
};


export const CALLEE_SCHEMA = {
callees: {
description: "List of function names which arguments should get linted.",
items: {
anyOf: [
CALLEE_REGEX_CONFIG,
CALLEE_MATCHER_CONFIG,
CALLEE_NAME_CONFIG
]
},
type: "array"
}
} satisfies Rule.RuleMetaData["schema"];

export const ATTRIBUTE_SCHEMA = {
attributes: {
description: "List of attribute names that should get linted.",
items: {
anyOf: [
ATTRIBUTE_NAME_CONFIG,
ATTRIBUTE_REGEX_CONFIG,
ATTRIBUTE_MATCHER_CONFIG
]
},
type: "array"
}
} satisfies Rule.RuleMetaData["schema"];

export const VARIABLE_SCHEMA = {
variables: {
description: "List of variable names which values should get linted.",
items: {
anyOf: [
VARIABLE_REGEX_CONFIG,
VARIABLE_MATCHER_CONFIG,
VARIABLE_NAME_CONFIG
]
},
type: "array"
}
} satisfies Rule.RuleMetaData["schema"];

export const TAG_SCHEMA = {
tags: {
description: "List of template literal tags that should get linted.",
items: {
anyOf: [
TAG_REGEX_CONFIG,
TAG_MATCHER_CONFIG,
TAG_NAME_CONFIG
]
},
type: "array"
}
} satisfies Rule.RuleMetaData["schema"];
16 changes: 8 additions & 8 deletions src/rules/tailwind-multiline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
DEFAULT_VARIABLE_NAMES
} from "readable-tailwind:options:default-options.js";
import {
getAttributesSchema,
getCalleeSchema,
getTagsSchema,
getVariableSchema
ATTRIBUTE_SCHEMA,
CALLEE_SCHEMA,
TAG_SCHEMA,
VARIABLE_SCHEMA
} from "readable-tailwind:options:descriptions.js";
import {
getLiteralsByESCallExpression,
Expand Down Expand Up @@ -207,10 +207,10 @@ export const tailwindMultiline: ESLintRule<Options> = {
{
additionalProperties: false,
properties: {
...getCalleeSchema(defaultOptions.callees),
...getAttributesSchema(defaultOptions.attributes),
...getVariableSchema(defaultOptions.variables),
...getTagsSchema(defaultOptions.tags),
...CALLEE_SCHEMA,
...ATTRIBUTE_SCHEMA,
...VARIABLE_SCHEMA,
...TAG_SCHEMA,
classesPerLine: {
default: defaultOptions.classesPerLine,
description: "The maximum amount of classes per line. Lines are wrapped appropriately to stay within this limit . The value `0` disables line wrapping by `classesPerLine`.",
Expand Down
16 changes: 8 additions & 8 deletions src/rules/tailwind-no-duplicate-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
DEFAULT_VARIABLE_NAMES
} from "readable-tailwind:options:default-options.js";
import {
getAttributesSchema,
getCalleeSchema,
getTagsSchema,
getVariableSchema
ATTRIBUTE_SCHEMA,
CALLEE_SCHEMA,
TAG_SCHEMA,
VARIABLE_SCHEMA
} from "readable-tailwind:options:descriptions.js";
import {
getLiteralsByESCallExpression,
Expand Down Expand Up @@ -181,10 +181,10 @@ export const tailwindNoDuplicateClasses: ESLintRule<Options> = {
{
additionalProperties: false,
properties: {
...getCalleeSchema(defaultOptions.callees),
...getAttributesSchema(defaultOptions.attributes),
...getVariableSchema(defaultOptions.variables),
...getTagsSchema(defaultOptions.tags)
...CALLEE_SCHEMA,
...ATTRIBUTE_SCHEMA,
...VARIABLE_SCHEMA,
...TAG_SCHEMA
},
type: "object"
}
Expand Down
16 changes: 8 additions & 8 deletions src/rules/tailwind-no-unnecessary-whitespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
DEFAULT_VARIABLE_NAMES
} from "readable-tailwind:options:default-options.js";
import {
getAttributesSchema,
getCalleeSchema,
getTagsSchema,
getVariableSchema
ATTRIBUTE_SCHEMA,
CALLEE_SCHEMA,
TAG_SCHEMA,
VARIABLE_SCHEMA
} from "readable-tailwind:options:descriptions.js";
import {
getLiteralsByESCallExpression,
Expand Down Expand Up @@ -184,10 +184,10 @@ export const tailwindNoUnnecessaryWhitespace: ESLintRule<Options> = {
description: "Allow multi-line class declarations. If this option is disabled, template literal strings will be collapsed into a single line string wherever possible. Must be set to `true` when used in combination with [readable-tailwind/multiline](./multiline.md).",
type: "boolean"
},
...getCalleeSchema(defaultOptions.callees),
...getAttributesSchema(defaultOptions.attributes),
...getVariableSchema(defaultOptions.variables),
...getTagsSchema(defaultOptions.tags)
...CALLEE_SCHEMA,
...ATTRIBUTE_SCHEMA,
...VARIABLE_SCHEMA,
...TAG_SCHEMA
},
type: "object"
}
Expand Down
16 changes: 8 additions & 8 deletions src/rules/tailwind-sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
DEFAULT_VARIABLE_NAMES
} from "readable-tailwind:options:default-options.js";
import {
getAttributesSchema,
getCalleeSchema,
getTagsSchema,
getVariableSchema
ATTRIBUTE_SCHEMA,
CALLEE_SCHEMA,
TAG_SCHEMA,
VARIABLE_SCHEMA
} from "readable-tailwind:options:descriptions.js";
import {
getLiteralsByESCallExpression,
Expand Down Expand Up @@ -248,10 +248,10 @@ export const tailwindSortClasses: ESLintRule<Options> = {
{
additionalProperties: false,
properties: {
...getCalleeSchema(defaultOptions.callees),
...getAttributesSchema(defaultOptions.attributes),
...getVariableSchema(defaultOptions.variables),
...getTagsSchema(defaultOptions.tags),
...CALLEE_SCHEMA,
...ATTRIBUTE_SCHEMA,
...VARIABLE_SCHEMA,
...TAG_SCHEMA,
entryPoint: {
description: "The path to the css entry point of the project. If not specified, the plugin will fall back to the default tailwind classes.",
type: "string"
Expand Down
2 changes: 1 addition & 1 deletion src/types/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export type NameConfig = AttributeName | CalleeName | VariableName;
export type RegexConfig = AttributeRegex | CalleeRegex | VariableRegex;
export type MatchersConfig = AttributeMatchers | CalleeMatchers | VariableMatchers;

export interface ESLintRule<Options extends any[] = [any]> {
export interface ESLintRule<Options extends [any] = [any]> {
name: string;
rule: Rule.RuleModule;
options?: Options;
Expand Down
6 changes: 6 additions & 0 deletions src/utils/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Rule } from "eslint";


export function getOptions<Options extends [Record<string, any>]>(ctx: Rule.RuleContext): Required<Options[0]> {
return ctx.options[0];
}
Loading

0 comments on commit 6e27500

Please sign in to comment.