Skip to content

Commit

Permalink
refactor!: rename classAttributes to attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
schoero committed Jan 19, 2025
1 parent fb07b48 commit eae9ece
Show file tree
Hide file tree
Showing 26 changed files with 203 additions and 215 deletions.
12 changes: 6 additions & 6 deletions docs/api/defaults.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# Defaults

Every [rule](../../README.md#rules) can target multiple class attributes, callee names, variable names or template literal tags.
Every [rule](../../README.md#rules) can target multiple attributes, callee names, variable names or template literal tags.

Read the [concepts documentation](../concepts/concepts.md) first to learn why this is important and what different options there are to define where to look for tailwind classes.

Expand All @@ -28,8 +28,8 @@ If an utility is not supported or you have built your own, you can change the ma

```ts
import {
getDefaultAttributes,
getDefaultCallees,
getDefaultClassAttributes,
getDefaultTags,
getDefaultVariables
} from "eslint-plugin-readable-tailwind/api/defaults";
Expand All @@ -43,8 +43,8 @@ import {
```ts
import eslintPluginReadableTailwind from "eslint-plugin-readable-tailwind";
import {
getDefaultAttributes,
getDefaultCallees,
getDefaultClassAttributes,
getDefaultVariables
} from "eslint-plugin-readable-tailwind/api/defaults";
import { MatcherType } from "eslint-plugin-readable-tailwind/api/types";
Expand All @@ -69,8 +69,8 @@ export default [
]
}],
"readable-tailwind/no-duplicate-classes": ["warn", {
classAttributes: [
...getDefaultClassAttributes(),
attributes: [
...getDefaultAttributes(),
[
"myAttribute", [
{
Expand All @@ -93,7 +93,7 @@ export default [
]
}],
"readable-tailwind/sort-classes": ["warn", {
classAttributes: [
attributes: [
...getDefaultTags(),
[
"myTag", [
Expand Down
10 changes: 5 additions & 5 deletions docs/concepts/concepts.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The [rules](../../README.md#rules) of this plugin can't apply to every string li

There are different ways to define where to look for tailwind classes.

- [Name](#name): Matches a single string literal by the name of the callee, variable or class attribute.
- [Name](#name): Matches a single string literal by the name of the callee, variable or attribute.
- [Regular expressions](#regular-expressions): Matches multiple string literals by regular expressions.
- [Matchers](#matchers): Matches multiple string literals by the abstract syntax tree.

Expand All @@ -18,7 +18,7 @@ It is possible that you never have to change this configuration, but if you do n

## Name

The simplest form to define string literals to lint is by their name. Callees, variables or class attributes with that name will be linted. The name can be a string or a regular expression.
The simplest form to define string literals to lint is by their name. Callees, variables or attributes with that name will be linted. The name can be a string or a regular expression.

<br/>

Expand All @@ -34,7 +34,7 @@ type Name = string;

```jsonc
{
"classAttributes": ["myAttribute"]
"attributes": ["myAttribute"]
}
```

Expand Down Expand Up @@ -101,7 +101,7 @@ type Regex = [

```jsonc
{
"classAttributes": [
"attributes": [
[
// matches the attribute name and the attribute value
"myAttribute={([\\S\\s]*)}",
Expand Down Expand Up @@ -254,7 +254,7 @@ For example, the object path for the `value` key in the following object would b

```jsonc
{
"classAttributes": [
"attributes": [
[
// matches attributes with the name `myAttribute`
"myAttribute",
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/multiline.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Enforce tailwind classes to be broken up into multiple lines. It is possible to

<br/>

- `classAttributes`
- `attributes`

The name of the attribute that contains the tailwind classes. This can also be set globally via the [`settings` object](../settings/settings.md).

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-duplicate-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Disallow duplicate classes in tailwindcss class strings.

## Options

- `classAttributes`
- `attributes`

The name of the attribute that contains the tailwind classes. This can also be set globally via the [`settings` object](../settings/settings.md).

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-unnecessary-whitespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Disallow unnecessary whitespace in between and around tailwind classes.

<br/>

- `classAttributes`
- `attributes`

The name of the attribute that contains the tailwind classes. This can also be set globally via the [`settings` object](../settings/settings.md).

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/sort-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Enforce the order of tailwind classes. It is possible to sort classes alphabetic

<br/>

- `classAttributes`
- `attributes`

The name of the attribute that contains the tailwind classes. This can also be set globally via the [`settings` object](../settings/settings.md).

Expand Down
6 changes: 3 additions & 3 deletions docs/settings/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Table of Contents

- [classAttributes](#classattributes)
- [attributes](#attributes)
- [callees](#callees)
- [variables](#variables)

Expand All @@ -21,7 +21,7 @@ To set the settings object, add a `settings` key to the eslint config.
"rules": { /* ... */ },
"settings": {
"readable-tailwind": {
"classAttributes": [/* ... */],
"attributes": [/* ... */],
"callees": [/* ... */],
"variables": [/* ... */],
"tags": [/* ... */]
Expand All @@ -33,7 +33,7 @@ To set the settings object, add a `settings` key to the eslint config.
<br />
<br />

### `classAttributes`
### `attributes`

The name of the attribute that contains the tailwind classes.

Expand Down
2 changes: 1 addition & 1 deletion src/api/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function getDefaultCallees() {
return DEFAULT_CALLEE_NAMES;
}

export function getDefaultClassAttributes() {
export function getDefaultAttributes() {
return DEFAULT_ATTRIBUTE_NAMES;
}

Expand Down
4 changes: 2 additions & 2 deletions src/options/default-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { TW_JOIN } from "readable-tailwind:options:callees/twJoin.js";
import { TW_MERGE } from "readable-tailwind:options:callees/twMerge.js";
import { MatcherType } from "readable-tailwind:types:rule.js";

import type { Callees, ClassAttributes, Tags, Variables } from "readable-tailwind:types:rule.js";
import type { Attributes, Callees, Tags, Variables } from "readable-tailwind:types:rule.js";


export const DEFAULT_CALLEE_NAMES = [
Expand Down Expand Up @@ -58,7 +58,7 @@ export const DEFAULT_ATTRIBUTE_NAMES = [
}
]
]
] satisfies ClassAttributes;
] satisfies Attributes;

export const DEFAULT_VARIABLE_NAMES = [
[
Expand Down
30 changes: 13 additions & 17 deletions src/options/descriptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import { validate } from "json-schema";
import { describe, expect, test } from "vitest";

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

import type { CalleeOption, ClassAttributeOption, VariableOption } from "readable-tailwind:types:rule.js";
import type { AttributeOption, CalleeOption, VariableOption } from "readable-tailwind:types:rule.js";


describe("descriptions", () => {

test("name config", () => {

const classAttributes = {
classAttributes: [
const attributes = {
attributes: [
"class",
"className"
]
} satisfies ClassAttributeOption;
} satisfies AttributeOption;

expect(
validate(classAttributes, getClassAttributeSchema(classAttributes))
validate(attributes, getAttributesSchema(attributes))
).toStrictEqual(
{ errors: [], valid: true }
);
Expand Down Expand Up @@ -57,15 +53,15 @@ describe("descriptions", () => {

test("regex config", () => {

const classAttributes = {
classAttributes: [
const attributes = {
attributes: [
"(class|className)",
"(.*)"
]
} satisfies ClassAttributeOption;
} satisfies AttributeOption;

expect(
validate(classAttributes, getClassAttributeSchema(classAttributes))
validate(attributes, getAttributesSchema(attributes))
).toStrictEqual(
{ errors: [], valid: true }
);
Expand Down Expand Up @@ -100,8 +96,8 @@ describe("descriptions", () => {

test("matcher config", () => {

const classAttributes: ClassAttributeOption = {
classAttributes: [
const attributes: AttributeOption = {
attributes: [
[
"class",
[
Expand All @@ -121,7 +117,7 @@ describe("descriptions", () => {
};

expect(
validate(classAttributes, getClassAttributeSchema(classAttributes))
validate(attributes, getAttributesSchema(attributes))
).toStrictEqual(
{ errors: [], valid: true }
);
Expand Down
16 changes: 8 additions & 8 deletions src/options/descriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { MatcherType } from "readable-tailwind:types:rule.js";
import type { Rule } from "eslint";


export function getClassAttributeSchema(defaultValue: unknown) {
export function getAttributesSchema(defaultValue: unknown) {
return {
classAttributes: {
attributes: {
default: defaultValue,
description: "List of attribute names that should get linted.",
items: {
anyOf: [
CLASS_ATTRIBUTE_NAME_CONFIG,
CLASS_ATTRIBUTE_REGEX_CONFIG,
CLASS_ATTRIBUTE_MATCHER_CONFIG
ATTRIBUTE_NAME_CONFIG,
ATTRIBUTE_REGEX_CONFIG,
ATTRIBUTE_MATCHER_CONFIG
]
},
type: "array"
Expand Down Expand Up @@ -112,7 +112,7 @@ const OBJECT_VALUE_MATCHER_SCHEMA = {
type: "object"
} satisfies Rule.RuleMetaData["schema"];

const CLASS_ATTRIBUTE_REGEX_CONFIG = {
const ATTRIBUTE_REGEX_CONFIG = {
description: "List of regular expressions that matches string literals which should get linted.",
items: [
{
Expand All @@ -127,7 +127,7 @@ const CLASS_ATTRIBUTE_REGEX_CONFIG = {
type: "array"
};

const CLASS_ATTRIBUTE_MATCHER_CONFIG = {
const ATTRIBUTE_MATCHER_CONFIG = {
description: "List of matchers that will automatically be matched.",
items: [
{
Expand All @@ -150,7 +150,7 @@ const CLASS_ATTRIBUTE_MATCHER_CONFIG = {
type: "array"
};

const CLASS_ATTRIBUTE_NAME_CONFIG = {
const ATTRIBUTE_NAME_CONFIG = {
description: "Attribute name that for which children get linted.",
type: "string"
};
Expand Down
4 changes: 2 additions & 2 deletions src/parsers/es.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ describe("es", () => {
});
});

it("should match classAttributes via regex", () => {
it("should match attributes via regex", () => {
lint(tailwindNoUnnecessaryWhitespace, TEST_SYNTAXES, {
invalid: [
{
errors: 1,
jsx: `<img testStyles=" lint " />`,
jsxOutput: `<img testStyles="lint" />`,
options: [{
classAttributes: ["^.*Styles$"]
attributes: ["^.*Styles$"]
}],
svelte: `<img testStyles=" lint " />`,
svelteOutput: `<img testStyles="lint" />`,
Expand Down
20 changes: 8 additions & 12 deletions src/parsers/html.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import {
isClassAttributeMatchers,
isClassAttributeName,
isClassAttributeRegex
} from "readable-tailwind:utils:matchers.js";
import { isAttributesMatchers, isAttributesName, isAttributesRegex } from "readable-tailwind:utils:matchers.js";
import { deduplicateLiterals } from "readable-tailwind:utils:utils.js";

import type { AttributeNode, TagNode } from "es-html-parser";
import type { Rule } from "eslint";

import type { Literal, QuoteMeta } from "readable-tailwind:types:ast.js";
import type { ClassAttributes } from "readable-tailwind:types:rule.js";
import type { Attributes } from "readable-tailwind:types:rule.js";


export function getLiteralsByHTMLClassAttribute(ctx: Rule.RuleContext, attribute: AttributeNode, classAttributes: ClassAttributes): Literal[] {
const literals = classAttributes.reduce<Literal[]>((literals, classAttribute) => {
if(isClassAttributeName(classAttribute)){
if(classAttribute.toLowerCase() !== attribute.key.value.toLowerCase()){ return literals; }
export function getLiteralsByHTMLAttributes(ctx: Rule.RuleContext, attribute: AttributeNode, attributes: Attributes): Literal[] {
const literals = attributes.reduce<Literal[]>((literals, Attributes) => {

Check warning on line 12 in src/parsers/html.ts

View workflow job for this annotation

GitHub Actions / lint

Parameter name `Attributes` must match one of the following formats: camelCase
if(isAttributesName(Attributes)){
if(Attributes.toLowerCase() !== attribute.key.value.toLowerCase()){ return literals; }
literals.push(...getLiteralsByHTMLAttributeNode(ctx, attribute));
} else if(isClassAttributeRegex(classAttribute)){
} else if(isAttributesRegex(Attributes)){
console.warn("Regex not supported in HTML");
} else if(isClassAttributeMatchers(classAttribute)){
} else if(isAttributesMatchers(Attributes)){
console.warn("Matchers not supported in HTML");
}

Expand Down
Loading

0 comments on commit eae9ece

Please sign in to comment.