Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: template literal tags #65

Merged
merged 10 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 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 or variable names.
Every [rule](../../README.md#rules) can target multiple class 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 Down Expand Up @@ -30,6 +30,7 @@ If an utility is not supported or you have built your own, you can change the ma
import {
getDefaultCallees,
getDefaultClassAttributes,
getDefaultTags,
getDefaultVariables
} from "eslint-plugin-readable-tailwind/api/defaults";
```
Expand Down Expand Up @@ -67,6 +68,18 @@ export default [
]
]
}],
"readable-tailwind/no-duplicate-classes": ["warn", {
classAttributes: [
...getDefaultClassAttributes(),
[
"myAttribute", [
{
match: MatcherType.String
}
]
]
]
}],
"readable-tailwind/no-unnecessary-whitespace": ["warn", {
variables: [
...getDefaultVariables(),
Expand All @@ -81,9 +94,9 @@ export default [
}],
"readable-tailwind/sort-classes": ["warn", {
classAttributes: [
...getDefaultClassAttributes(),
...getDefaultTags(),
[
"myAttribute", [
"myTag", [
{
match: MatcherType.String
}
Expand Down
17 changes: 14 additions & 3 deletions docs/rules/multiline.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Enforce tailwind classes to be broken up into multiple lines. It is possible to

- `classAttributes`

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

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [Name](../concepts/concepts.md#name) for `"class"` and [strings Matcher](../concepts/concepts.md#types-of-matchers) for `"class", "className"`
Expand All @@ -72,7 +72,7 @@ Enforce tailwind classes to be broken up into multiple lines. It is possible to

- `callees`

List of function names which arguments should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md.
List of function names which arguments should get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [Matchers](../concepts/concepts.md#types-of-matchers) for `"cc", "clb", "clsx", "cn", "cnb", "ctl", "cva", "cx", "dcnb", "objstr", "tv", "twJoin", "twMerge"`
Expand All @@ -81,13 +81,24 @@ Enforce tailwind classes to be broken up into multiple lines. It is possible to

- `variables`

List of variable names which initializer should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md.
List of variable names whose initializer should get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [strings Matcher](../concepts/concepts.md#types-of-matchers) for `"className", "classNames", "classes", "style", "styles"`

<br/>

- `tags`

List of template literal tag names whose content should get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: None

Note: When using the `tags` option, it is recommended to use the [strings Matcher](../concepts/concepts.md#types-of-matchers) for your tag names. This will ensure that nested expressions get linted correctly.

<br/>

## Examples

With the default options, a class name will be broken up into multiple lines and grouped by their modifiers. Groups are separated by an empty line.
Expand Down
17 changes: 14 additions & 3 deletions docs/rules/no-duplicate-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Disallow duplicate classes in tailwindcss class strings.

- `classAttributes`

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

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [Name](../concepts/concepts.md#name) for `"class"` and [strings Matcher](../concepts/concepts.md#types-of-matchers) for `"class", "className"`
Expand All @@ -17,7 +17,7 @@ Disallow duplicate classes in tailwindcss class strings.

- `callees`

List of function names which arguments should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md.
List of function names which arguments should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [Matchers](../concepts/concepts.md#types-of-matchers) for `"cc", "clb", "clsx", "cn", "cnb", "ctl", "cva", "cx", "dcnb", "objstr", "tv", "twJoin", "twMerge"`
Expand All @@ -26,13 +26,24 @@ Disallow duplicate classes in tailwindcss class strings.

- `variables`

List of variable names which initializer should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md.
List of variable names whose initializer should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [strings Matcher](../concepts/concepts.md#types-of-matchers) for `"className", "classNames", "classes", "style", "styles"`

<br/>

- `tags`

List of template literal tag names whose content should get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: None

Note: When using the `tags` option, it is recommended to use the [strings Matcher](../concepts/concepts.md#types-of-matchers) for your tag names. This will ensure that nested expressions get linted correctly.

<br/>

## Examples

```tsx
Expand Down
19 changes: 15 additions & 4 deletions docs/rules/no-unnecessary-whitespace.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Disallow unnecessary whitespace in between and around tailwind classes.
- `allowMultiline`

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).
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`
**Default**: `true`
Expand All @@ -18,7 +18,7 @@ Disallow unnecessary whitespace in between and around tailwind classes.

- `classAttributes`

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

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [Name](../concepts/concepts.md#name) for `"class"` and [strings Matcher](../concepts/concepts.md#types-of-matchers) for `"class", "className"`
Expand All @@ -27,7 +27,7 @@ Disallow unnecessary whitespace in between and around tailwind classes.

- `callees`

List of function names which arguments should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md.
List of function names which arguments should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [Matchers](../concepts/concepts.md#types-of-matchers) for `"cc", "clb", "clsx", "cn", "cnb", "ctl", "cva", "cx", "dcnb", "objstr", "tv", "twJoin", "twMerge"`
Expand All @@ -36,13 +36,24 @@ Disallow unnecessary whitespace in between and around tailwind classes.

- `variables`

List of variable names which initializer should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md.
List of variable names whose initializer should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [strings Matcher](../concepts/concepts.md#types-of-matchers) for `"className", "classNames", "classes", "style", "styles"`

<br/>

- `tags`

List of template literal tag names whose content should get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: None

Note: When using the `tags` option, it is recommended to use the [strings Matcher](../concepts/concepts.md#types-of-matchers) for your tag names. This will ensure that nested expressions get linted correctly.

<br/>

## Examples

```tsx
Expand Down
17 changes: 14 additions & 3 deletions docs/rules/sort-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Enforce the order of tailwind classes. It is possible to sort classes alphabetic

- `classAttributes`

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

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [Name](../concepts/concepts.md#name) for `"class"` and [strings Matcher](../concepts/concepts.md#types-of-matchers) for `"class", "className"`
Expand All @@ -42,7 +42,7 @@ Enforce the order of tailwind classes. It is possible to sort classes alphabetic

- `callees`

List of function names which arguments should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md.
List of function names which arguments should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [Matchers](../concepts/concepts.md#types-of-matchers) for `"cc", "clb", "clsx", "cn", "cnb", "ctl", "cva", "cx", "dcnb", "objstr", "tv", "twJoin", "twMerge"`
Expand All @@ -51,13 +51,24 @@ Enforce the order of tailwind classes. It is possible to sort classes alphabetic

- `variables`

List of variable names which initializer should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md.
List of variable names whose initializer should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [strings Matcher](../concepts/concepts.md#types-of-matchers) for `"className", "classNames", "classes", "style", "styles"`

<br/>

- `tags`

List of template literal tag names whose content should get linted. This can also be set globally via the [`settings` object](../settings/settings.md).

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: None

Note: When using the `tags` option, it is recommended to use the [strings Matcher](../concepts/concepts.md#types-of-matchers) for your tag names. This will ensure that nested expressions get linted correctly.

<br/>

## Examples

```tsx
Expand Down
26 changes: 19 additions & 7 deletions docs/settings/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<br />
<br />

The settings object can be used to globally configure shared options across all rules. Global will always be overridden by rule-specific options.
The settings object can be used to globally configure shared options across all rules. Global options will always be overridden by rule-specific options.
To set the settings object, add a `settings` key to the eslint config.

<br />
Expand All @@ -21,9 +21,10 @@ To set the settings object, add a `settings` key to the eslint config.
"rules": { /* ... */ },
"settings": {
"readable-tailwind": {
"classAttributes": ["class", "className"],
"callees": ["cc", "clb", "clsx", "cn", "cnb", "ctl", "cva", "cx", "dcnb", "objstr", "tv", "twJoin", "twMerge"],
"variables": ["className", "classNames", "classes", "style", "styles"]
"classAttributes": [/* ... */],
"callees": [/* ... */],
"variables": [/* ... */],
"tags": [/* ... */]
}
}
}
Expand All @@ -34,7 +35,7 @@ To set the settings object, add a `settings` key to the eslint config.

### `classAttributes`

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

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [Name](../concepts/concepts.md#name) for `"class"` and [strings Matcher](../concepts/concepts.md#types-of-matchers) for `"class", "className"`
Expand All @@ -43,7 +44,7 @@ To set the settings object, add a `settings` key to the eslint config.

### `callees`

List of function names which arguments should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md.
List of function names which arguments should also get linted.

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [Matchers](../concepts/concepts.md#types-of-matchers) for `"cc", "clb", "clsx", "cn", "cnb", "ctl", "cva", "cx", "dcnb", "objstr", "tv", "twJoin", "twMerge"`
Expand All @@ -52,7 +53,18 @@ To set the settings object, add a `settings` key to the eslint config.

### `variables`

List of variable names which initializer should also get linted. This can also be set globally via the [`settings` object](../settings/settings.md.
List of variable names whose initializer should also get linted.

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: [strings Matcher](../concepts/concepts.md#types-of-matchers) for `"className", "classNames", "classes", "style", "styles"`

<br/>

- `tags`

List of template literal tag names whose content should get linted.

**Type**: Array of [Name](../concepts/concepts.md#name), [Regex](../concepts/concepts.md#regular-expressions) or [Matchers](../concepts/concepts.md#matchers)
**Default**: None

Note: When using the `tags` option, it is recommended to use the [strings Matcher](../concepts/concepts.md#types-of-matchers) for your tag names. This will ensure that nested expressions get linted correctly.
5 changes: 5 additions & 0 deletions src/api/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
DEFAULT_ATTRIBUTE_NAMES,
DEFAULT_CALLEE_NAMES,
DEFAULT_TAG_NAMES,
DEFAULT_VARIABLE_NAMES
} from "readable-tailwind:options:default-options.js";

Expand All @@ -16,3 +17,7 @@ export function getDefaultClassAttributes() {
export function getDefaultVariables() {
return DEFAULT_VARIABLE_NAMES;
}

export function getDefaultTags() {
return DEFAULT_TAG_NAMES;
}
4 changes: 3 additions & 1 deletion 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, Variables } from "readable-tailwind:types:rule.js";
import type { Callees, ClassAttributes, Tags, Variables } from "readable-tailwind:types:rule.js";


export const DEFAULT_CALLEE_NAMES = [
Expand Down Expand Up @@ -97,3 +97,5 @@ export const DEFAULT_VARIABLE_NAMES = [
]
]
] satisfies Variables;

export const DEFAULT_TAG_NAMES = [] satisfies Tags;
Loading
Loading