Skip to content

Commit c4775c0

Browse files
committed
feat(linter): add schema for typescript/consistent-type-assertions (#23349)
`assertionStyle` can only be `never` without any other options. This is currently not reflected within the schema. Upstream docs: https://typescript-eslint.io/rules/consistent-type-assertions/
1 parent 6e516f7 commit c4775c0

4 files changed

Lines changed: 292 additions & 5 deletions

File tree

‎apps/oxlint/src-js/package/config.generated.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ export type ImportKind = "none" | "all" | "multiple" | "single";
190190
export type SortOrder = "desc" | "asc";
191191
export type ClassLiteralPropertyStyleOption = "fields" | "getters";
192192
export type ConsistentIndexedObjectStyleConfig = "record" | "index-signature";
193+
export type ArrayLiteralTypeAssertions = "allow" | "allow-as-parameter" | "never";
194+
export type AssertionStyle = "as" | "angle-bracket" | "never";
195+
export type ObjectLiteralTypeAssertions = "allow" | "allow-as-parameter" | "never";
193196
export type ConsistentTypeDefinitionsConfig = "interface" | "type";
194197
export type FixStyle = "separate-type-imports" | "inline-type-imports";
195198
export type Prefer = "type-imports" | "no-type-imports";
@@ -1289,7 +1292,10 @@ export interface DummyRuleMap {
12891292
| [AllowWarnDeny]
12901293
| [AllowWarnDeny, ConsistentIndexedObjectStyleConfig];
12911294
"typescript/consistent-return"?: AllowWarnDeny | [AllowWarnDeny] | [AllowWarnDeny, ConsistentReturnConfig];
1292-
"typescript/consistent-type-assertions"?: DummyRule;
1295+
"typescript/consistent-type-assertions"?:
1296+
| AllowWarnDeny
1297+
| [AllowWarnDeny]
1298+
| [AllowWarnDeny, ConsistentTypeAssertionsConfig];
12931299
"typescript/consistent-type-definitions"?:
12941300
| AllowWarnDeny
12951301
| [AllowWarnDeny]
@@ -4545,6 +4551,20 @@ export interface ConsistentReturnConfig {
45454551
*/
45464552
treatUndefinedAsUnspecified?: boolean;
45474553
}
4554+
export interface ConsistentTypeAssertionsConfig {
4555+
/**
4556+
* Whether array literal type assertions are allowed, allowed only as parameters, or disallowed.
4557+
*/
4558+
arrayLiteralTypeAssertions?: ArrayLiteralTypeAssertions;
4559+
/**
4560+
* Which assertion syntax is enforced.
4561+
*/
4562+
assertionStyle?: AssertionStyle;
4563+
/**
4564+
* Whether object literal type assertions are allowed, allowed only as parameters, or disallowed.
4565+
*/
4566+
objectLiteralTypeAssertions?: ObjectLiteralTypeAssertions;
4567+
}
45484568
export interface ConsistentTypeExportsConfig {
45494569
/**
45504570
* Enables an autofix strategy that rewrites mixed exports using inline `type` specifiers.

‎crates/oxc_linter/src/utils/schemars.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::rules::RuleEnum;
1313
/// These should return the DummyRule struct/schema instead of the actual rule struct/schema,
1414
/// until we verify that the rule's schema is valid and can be generated by schemars.
1515
#[cfg(feature = "ruledocs")]
16-
const NO_VERIFIED_VALID_SCHEMA: [&str; 36] = [
16+
const NO_VERIFIED_VALID_SCHEMA: [&str; 35] = [
1717
"eslint/func-name-matching",
1818
"eslint/no-restricted-globals",
1919
"eslint/no-restricted-imports",
@@ -42,7 +42,6 @@ const NO_VERIFIED_VALID_SCHEMA: [&str; 36] = [
4242
"typescript/array-type",
4343
"typescript/ban-ts-comment",
4444
"typescript/consistent-generic-constructors",
45-
"typescript/consistent-type-assertions",
4645
"typescript/no-restricted-types",
4746
"unicorn/filename-case",
4847
"unicorn/import-style",

‎npm/oxlint/configuration_schema.json‎

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,34 @@
679679
},
680680
"additionalProperties": false
681681
},
682+
"ArrayLiteralTypeAssertions": {
683+
"oneOf": [
684+
{
685+
"description": "Allow type assertions on array literals.\n\nExamples of **correct** code with this option:\n```ts\nconst x = [1, 2] as number[];\nconst x = ['a'] as Array<string>;\n```",
686+
"type": "string",
687+
"enum": [
688+
"allow"
689+
],
690+
"markdownDescription": "Allow type assertions on array literals.\n\nExamples of **correct** code with this option:\n```ts\nconst x = [1, 2] as number[];\nconst x = ['a'] as Array<string>;\n```"
691+
},
692+
{
693+
"description": "Allow type assertions on array literals only when used as a function parameter,\n`throw` target, or default value.\n\nExamples of **incorrect** code with this option:\n```ts\nconst x = [1, 2] as Foo;\nconst foo = () => [5] as Foo;\n```\n\nExamples of **correct** code with this option:\n```ts\nprint([5] as Foo);\nthrow [1, 2] as Bar;\nfunction f(x = [5] as Foo.Bar) {}\n```",
694+
"type": "string",
695+
"enum": [
696+
"allow-as-parameter"
697+
],
698+
"markdownDescription": "Allow type assertions on array literals only when used as a function parameter,\n`throw` target, or default value.\n\nExamples of **incorrect** code with this option:\n```ts\nconst x = [1, 2] as Foo;\nconst foo = () => [5] as Foo;\n```\n\nExamples of **correct** code with this option:\n```ts\nprint([5] as Foo);\nthrow [1, 2] as Bar;\nfunction f(x = [5] as Foo.Bar) {}\n```"
699+
},
700+
{
701+
"description": "Disallow type assertions on array literals entirely.\n\nExamples of **incorrect** code with this option:\n```ts\nconst x = [1, 2] as Foo;\nprint([5] as Foo);\n```\n\nExamples of **correct** code with this option:\n```ts\nconst x: Foo = [1, 2];\nconst x = [1, 2] satisfies Foo;\n```",
702+
"type": "string",
703+
"enum": [
704+
"never"
705+
],
706+
"markdownDescription": "Disallow type assertions on array literals entirely.\n\nExamples of **incorrect** code with this option:\n```ts\nconst x = [1, 2] as Foo;\nprint([5] as Foo);\n```\n\nExamples of **correct** code with this option:\n```ts\nconst x: Foo = [1, 2];\nconst x = [1, 2] satisfies Foo;\n```"
707+
}
708+
]
709+
},
682710
"ArrowBodyStyle": {
683711
"type": "array",
684712
"items": [
@@ -702,6 +730,34 @@
702730
},
703731
"additionalProperties": false
704732
},
733+
"AssertionStyle": {
734+
"oneOf": [
735+
{
736+
"description": "Enforce `as` syntax for type assertions.\n\nExamples of **incorrect** code with this option:\n```ts\nconst value = <Foo>bar;\n```\n\nExamples of **correct** code with this option:\n```ts\nconst value = bar as Foo;\n```",
737+
"type": "string",
738+
"enum": [
739+
"as"
740+
],
741+
"markdownDescription": "Enforce `as` syntax for type assertions.\n\nExamples of **incorrect** code with this option:\n```ts\nconst value = <Foo>bar;\n```\n\nExamples of **correct** code with this option:\n```ts\nconst value = bar as Foo;\n```"
742+
},
743+
{
744+
"description": "Enforce angle-bracket syntax for type assertions.\n\nExamples of **incorrect** code with this option:\n```ts\nconst value = bar as Foo;\n```\n\nExamples of **correct** code with this option:\n```ts\nconst value = <Foo>bar;\n```",
745+
"type": "string",
746+
"enum": [
747+
"angle-bracket"
748+
],
749+
"markdownDescription": "Enforce angle-bracket syntax for type assertions.\n\nExamples of **incorrect** code with this option:\n```ts\nconst value = bar as Foo;\n```\n\nExamples of **correct** code with this option:\n```ts\nconst value = <Foo>bar;\n```"
750+
},
751+
{
752+
"description": "Disallow type assertions entirely.\n\nExamples of **incorrect** code with this option:\n```ts\nconst value = bar as Foo;\nconst value = <Foo>bar;\n```\n\nExamples of **correct** code with this option:\n```ts\nconst value: Foo = bar;\nconst value = bar satisfies Foo;\n```",
753+
"type": "string",
754+
"enum": [
755+
"never"
756+
],
757+
"markdownDescription": "Disallow type assertions entirely.\n\nExamples of **incorrect** code with this option:\n```ts\nconst value = bar as Foo;\nconst value = <Foo>bar;\n```\n\nExamples of **correct** code with this option:\n```ts\nconst value: Foo = bar;\nconst value = bar satisfies Foo;\n```"
758+
}
759+
]
760+
},
705761
"AutocompleteValidConfig": {
706762
"type": "object",
707763
"properties": {
@@ -1267,6 +1323,39 @@
12671323
},
12681324
"additionalProperties": false
12691325
},
1326+
"ConsistentTypeAssertionsConfig": {
1327+
"type": "object",
1328+
"properties": {
1329+
"arrayLiteralTypeAssertions": {
1330+
"description": "Whether array literal type assertions are allowed, allowed only as parameters, or disallowed.",
1331+
"allOf": [
1332+
{
1333+
"$ref": "#/definitions/ArrayLiteralTypeAssertions"
1334+
}
1335+
],
1336+
"markdownDescription": "Whether array literal type assertions are allowed, allowed only as parameters, or disallowed."
1337+
},
1338+
"assertionStyle": {
1339+
"description": "Which assertion syntax is enforced.",
1340+
"allOf": [
1341+
{
1342+
"$ref": "#/definitions/AssertionStyle"
1343+
}
1344+
],
1345+
"markdownDescription": "Which assertion syntax is enforced."
1346+
},
1347+
"objectLiteralTypeAssertions": {
1348+
"description": "Whether object literal type assertions are allowed, allowed only as parameters, or disallowed.",
1349+
"allOf": [
1350+
{
1351+
"$ref": "#/definitions/ObjectLiteralTypeAssertions"
1352+
}
1353+
],
1354+
"markdownDescription": "Whether object literal type assertions are allowed, allowed only as parameters, or disallowed."
1355+
}
1356+
},
1357+
"additionalProperties": false
1358+
},
12701359
"ConsistentTypeDefinitionsConfig": {
12711360
"oneOf": [
12721361
{
@@ -6318,7 +6407,24 @@
63186407
]
63196408
},
63206409
"typescript/consistent-type-assertions": {
6321-
"$ref": "#/definitions/DummyRule"
6410+
"anyOf": [
6411+
{
6412+
"$ref": "#/definitions/AllowWarnDeny"
6413+
},
6414+
{
6415+
"type": "array",
6416+
"items": [
6417+
{
6418+
"$ref": "#/definitions/AllowWarnDeny"
6419+
},
6420+
{
6421+
"$ref": "#/definitions/ConsistentTypeAssertionsConfig"
6422+
}
6423+
],
6424+
"maxItems": 2,
6425+
"minItems": 1
6426+
}
6427+
]
63226428
},
63236429
"typescript/consistent-type-definitions": {
63246430
"anyOf": [
@@ -13973,6 +14079,34 @@
1397314079
}
1397414080
]
1397514081
},
14082+
"ObjectLiteralTypeAssertions": {
14083+
"oneOf": [
14084+
{
14085+
"description": "Allow type assertions on object literals.\n\nExamples of **correct** code with this option:\n```ts\nconst x = { a: 1 } as Foo;\nconst x = {} as Foo<int>;\n```",
14086+
"type": "string",
14087+
"enum": [
14088+
"allow"
14089+
],
14090+
"markdownDescription": "Allow type assertions on object literals.\n\nExamples of **correct** code with this option:\n```ts\nconst x = { a: 1 } as Foo;\nconst x = {} as Foo<int>;\n```"
14091+
},
14092+
{
14093+
"description": "Allow type assertions on object literals only when used as a function parameter,\n`throw` target, or default value.\n\nExamples of **incorrect** code with this option:\n```ts\nconst x = { a: 1 } as Foo;\nconst x = {} as Foo<int>;\n```\n\nExamples of **correct** code with this option:\n```ts\nprint({ a: 1 } as Foo);\nthrow { bar: 5 } as Foo;\nfunction f(x = {} as Foo) {}\n```",
14094+
"type": "string",
14095+
"enum": [
14096+
"allow-as-parameter"
14097+
],
14098+
"markdownDescription": "Allow type assertions on object literals only when used as a function parameter,\n`throw` target, or default value.\n\nExamples of **incorrect** code with this option:\n```ts\nconst x = { a: 1 } as Foo;\nconst x = {} as Foo<int>;\n```\n\nExamples of **correct** code with this option:\n```ts\nprint({ a: 1 } as Foo);\nthrow { bar: 5 } as Foo;\nfunction f(x = {} as Foo) {}\n```"
14099+
},
14100+
{
14101+
"description": "Disallow type assertions on object literals entirely.\n\nExamples of **incorrect** code with this option:\n```ts\nconst x = { a: 1 } as Foo;\nprint({ a: 1 } as Foo);\n```\n\nExamples of **correct** code with this option:\n```ts\nconst x: Foo = { a: 1 };\nconst x = { a: 1 } satisfies Foo;\n```",
14102+
"type": "string",
14103+
"enum": [
14104+
"never"
14105+
],
14106+
"markdownDescription": "Disallow type assertions on object literals entirely.\n\nExamples of **incorrect** code with this option:\n```ts\nconst x = { a: 1 } as Foo;\nprint({ a: 1 } as Foo);\n```\n\nExamples of **correct** code with this option:\n```ts\nconst x: Foo = { a: 1 };\nconst x = { a: 1 } satisfies Foo;\n```"
14107+
}
14108+
]
14109+
},
1397614110
"ObjectShorthandOptions": {
1397714111
"type": "object",
1397814112
"properties": {

0 commit comments

Comments
 (0)