Skip to content

Commit 422ae8c

Browse files
settings
1 parent cf4ae99 commit 422ae8c

File tree

4 files changed

+53
-31
lines changed

4 files changed

+53
-31
lines changed

docs/rules/my-rule.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Examples would normally go here.
2222
| `cssConfigPath` | Path to the Tailwind CSS configuration file (*.css) | String | | `default-path/app.css` |
2323
| `removeDuplicates` | Remove duplicated classnames | Boolean | | `true` |
2424
| `skipClassAttribute` | If you only want to lint the classnames inside one of the `callees`. | Boolean | | `false` |
25-
| `someBool` | someBool description. | Boolean | | `true` |
25+
| `someBool` | someBool description. | Boolean | | `false` |
2626
| `someEnum` | someEnum description. | String | `always`, `never` | `always` |
2727
| `tags` | List of tags to be detected in template literals | String[] | | [`tw`] |
2828

src/rules/my-rule.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ ruleTester.run(RULE_NAME, myRule, {
3333
{
3434
code: `<button onClick={() => { const name = 'John'; alert(name); }}>JSX</button>`,
3535
},
36+
{
37+
// a code snippet that should pass the linter
38+
code: `var x = 5;`,
39+
options: [
40+
{
41+
someBool: true,
42+
someEnum: "always",
43+
},
44+
],
45+
},
3646
],
3747
invalid: [
3848
{

src/rules/my-rule.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,26 @@ type MessageIds = "issue:var" | "fix:let" | "fix:const";
1818

1919
/**
2020
* The extra options that the rule can accept.
21-
* These options are merged with the shared settings.
21+
* These options get merged with the shared settings.
2222
* The typing is not used by `eslint-doc-generator` which uses the `schema` property in the rule's metadata.
2323
* Yet, it is useful for the IDE to provide autocompletion and type checking.
2424
*/
25-
export type RuleOptions = {
25+
type RuleOptions = {
2626
someBool: boolean;
2727
someEnum: string;
28-
} & PluginSettings;
28+
};
2929

30-
type Options = [RuleOptions];
30+
export type MergedOptions = RuleOptions & PluginSettings;
31+
32+
const RULE_DEFAULT: RuleOptions = {
33+
someBool: false,
34+
someEnum: "always",
35+
};
36+
37+
// TODO which one ?
38+
// - type Options = [RuleOptions];
39+
// - type Options = [MergedOptions];
40+
type Options = [MergedOptions];
3141

3242
// The Rule creator returns a function that is used to create a well-typed ESLint rule
3343
// The parameter passed into RuleCreator is a URL generator function.
@@ -54,13 +64,13 @@ export const myRule = createRule<Options, MessageIds>({
5464
someBool: {
5565
description: "someBool description.",
5666
type: "boolean",
57-
default: true,
67+
default: RULE_DEFAULT.someBool,
5868
},
5969
someEnum: {
6070
description: "someEnum description.",
6171
type: "string",
6272
enum: ["always", "never"],
63-
default: "always",
73+
default: RULE_DEFAULT.someEnum,
6474
},
6575
},
6676
additionalProperties: false,
@@ -83,27 +93,29 @@ export const myRule = createRule<Options, MessageIds>({
8393
},
8494
],
8595
create: (context, options) => {
96+
// Reading inline configuration
97+
console.log("\n", new Date(), "\n", "Options (rule):", "\n", options[0]);
98+
99+
// Shared settings
100+
const sharedSettings = (context.settings?.tailwindcss ||
101+
DEFAULTS) as PluginSharedSettings;
102+
console.log("\n", "sharedSettings (rule):", "\n", sharedSettings);
103+
104+
// Merged settings
105+
// const merged = parsePluginSettings(options[0]) as RuleOptions;
106+
const merged = parsePluginSettings<RuleOptions>({
107+
tailwindcss: options[0],
108+
}) as RuleOptions;
109+
console.log("\n", "merged (rule):", "\n", merged);
110+
86111
return {
87112
VariableDeclaration: (node) => {
113+
console.log("\n", "merged.someBool:", "\n", merged.someBool);
114+
if (merged.someBool === true) {
115+
console.log("someBool is true, processing VariableDeclaration");
116+
return;
117+
}
88118
if (node.kind === "var") {
89-
// Reading inline configuration
90-
console.log(
91-
"\n",
92-
new Date(),
93-
"\n",
94-
"Options (rule):",
95-
"\n",
96-
options[0]
97-
);
98-
// Shared settings
99-
const sharedSettings = (context.settings?.tailwindcss || {
100-
stylesheet: "",
101-
functions: [],
102-
}) as PluginSharedSettings;
103-
console.log("\n", "sharedSettings (rule):", "\n", sharedSettings);
104-
105-
const merged: PluginSettings = parsePluginSettings(context.settings);
106-
console.log("\n", "merged (rule):", "\n", merged);
107119
const rangeStart = node.range[0];
108120
const range: readonly [number, number] = [
109121
rangeStart,

src/utils/parse-plugin-settings.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ export const sharedSettingsSchema: Record<keyof PluginSettings, JSONSchema4> = {
6666
* @param settings The shared settings from the ESLint configuration.
6767
* @returns The parsed plugin settings.
6868
*/
69-
export function parsePluginSettings(
69+
export function parsePluginSettings<RuleOptions>(
7070
settings: SharedConfigurationSettings
71-
): PluginSettings {
72-
const noConfig =
73-
typeof settings.tailwindcss !== "object" || settings.tailwindcss === null;
71+
): PluginSettings & RuleOptions {
7472
const tailwindcssSettings = (
75-
noConfig ? {} : settings.tailwindcss
76-
) as PluginSettings;
73+
typeof settings.tailwindcss !== "object" || settings.tailwindcss === null
74+
? {}
75+
: settings.tailwindcss
76+
) as PluginSettings & RuleOptions;
7777

7878
return {
7979
...DEFAULTS,

0 commit comments

Comments
 (0)