Skip to content

Commit 3089754

Browse files
nzakasfasttime
andauthored
feat: Add extendable config types (#210)
Co-authored-by: Francesco Trotta <github@fasttime.org>
1 parent 031f2d4 commit 3089754

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default defineConfig([
5959
// TypeScript
6060
...tseslint.config({
6161
files: ["**/*.ts"],
62+
ignores: ["**/tests/**/*.ts"],
6263
extends: [...tseslint.configs.strict, ...tseslint.configs.stylistic],
6364
rules: {
6465
"no-use-before-define": "off",

packages/core/src/types.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -666,22 +666,36 @@ export interface LinterOptionsConfig {
666666
* Indicates what to do when an unused disable directive is found.
667667
*/
668668
reportUnusedDisableDirectives?: boolean | Severity;
669+
670+
/**
671+
* A severity value indicating if and how unused inline configs should be
672+
* tracked and reported.
673+
*/
674+
reportUnusedInlineConfigs?: Severity;
669675
}
670676

671677
/**
672-
* Shared settings that are accessible from within plugins.
678+
* The configuration for a rule.
673679
*/
674-
export type SettingsConfig = Record<string, unknown>;
680+
export type RuleConfig<RuleOptions extends unknown[] = unknown[]> =
681+
| Severity
682+
| [Severity, ...RuleOptions];
675683

684+
/* eslint-disable @typescript-eslint/consistent-indexed-object-style -- needed to allow extension */
676685
/**
677-
* The configuration for a rule.
686+
* A collection of rules and their configurations.
678687
*/
679-
export type RuleConfig = Severity | [Severity, ...unknown[]];
688+
export interface RulesConfig {
689+
[key: string]: RuleConfig;
690+
}
680691

681692
/**
682-
* A collection of rules and their configurations.
693+
* A collection of settings.
683694
*/
684-
export type RulesConfig = Record<string, RuleConfig>;
695+
export interface SettingsConfig {
696+
[key: string]: unknown;
697+
}
698+
/* eslint-enable @typescript-eslint/consistent-indexed-object-style -- needed to allow extension */
685699

686700
//------------------------------------------------------------------------------
687701
// Languages

packages/core/tests/types/types.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@ import type {
1515
Language,
1616
LanguageContext,
1717
LanguageOptions,
18+
LinterOptionsConfig,
1819
OkParseResult,
1920
ParseResult,
21+
RuleConfig,
2022
RuleContext,
2123
RuleDefinition,
2224
RulesConfig,
2325
RulesMeta,
2426
RuleTextEdit,
2527
RuleTextEditor,
2628
RuleVisitor,
29+
SettingsConfig,
2730
SourceLocation,
2831
SourceRange,
2932
TextSourceCode,
@@ -46,6 +49,41 @@ interface TestRootNode {
4649
length: number;
4750
}
4851

52+
//-----------------------------------------------------------------------------
53+
// Tests for config types
54+
//-----------------------------------------------------------------------------
55+
56+
const emptyRules: RulesConfig = {};
57+
58+
const rules: RulesConfig = {
59+
"no-console": "error",
60+
"no-unused-vars": 0,
61+
"json/no-duplicate-keys": ["warn"],
62+
"css/use-baseline": [1, { available: "widely" }],
63+
};
64+
65+
const emptySettings: SettingsConfig = {};
66+
67+
const settings: SettingsConfig = {
68+
foo: true,
69+
bar: "baz",
70+
};
71+
72+
const ruleConfig1: RuleConfig = "error";
73+
const ruleConfig2: RuleConfig = 1;
74+
const ruleConfig3: RuleConfig = ["error", { foo: "bar" }];
75+
const ruleConfig4: RuleConfig<string[]> = ["error", "foo", "bar"];
76+
const ruleConfig5: RuleConfig<[{ available: "widely" | "newly" }]> = [
77+
"error",
78+
{ available: "widely" },
79+
];
80+
81+
const linterConfig: LinterOptionsConfig = {
82+
noInlineConfig: true,
83+
reportUnusedDisableDirectives: "error",
84+
reportUnusedInlineConfigs: "warn",
85+
};
86+
4987
//-----------------------------------------------------------------------------
5088
// Tests for shared types
5189
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)