Skip to content

Commit bac5f3f

Browse files
andreww20129romise
andauthored
fix: disallow extra properties in rule options (#883)
Co-authored-by: Vida Xie <[email protected]>
1 parent 2143f34 commit bac5f3f

File tree

8 files changed

+114
-79
lines changed

8 files changed

+114
-79
lines changed

packages/eslint-plugin/rules/jsx-curly-spacing/jsx-curly-spacing.ts

Lines changed: 57 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
* @author Erik Wendel
1010
*/
1111

12-
import type { ASTNode, RuleFixer, Token } from '#types'
13-
import type { BasicConfig, MessageIds, RuleOptions } from './types'
12+
import type { ASTNode, JSONSchema, RuleFixer, Token } from '#types'
13+
import type { MessageIds, RuleOptions } from './types'
1414
import { isTokenOnSameLine } from '#utils/ast'
1515
import { createRule } from '#utils/create-rule'
1616

@@ -29,6 +29,45 @@ const messages = {
2929
spaceNeededBefore: 'A space is required before \'{{token}}\'',
3030
}
3131

32+
const BASIC_CONFIG_SCHEMA = {
33+
type: 'object',
34+
properties: {
35+
when: {
36+
type: 'string',
37+
enum: SPACING_VALUES,
38+
},
39+
allowMultiline: {
40+
type: 'boolean',
41+
},
42+
spacing: {
43+
type: 'object',
44+
properties: {
45+
objectLiterals: {
46+
type: 'string',
47+
enum: SPACING_VALUES,
48+
},
49+
},
50+
additionalProperties: false,
51+
},
52+
},
53+
additionalProperties: false,
54+
} satisfies JSONSchema.JSONSchema4ObjectSchema
55+
56+
const BASIC_CONFIG_OR_BOOLEAN_SCHEMA = {
57+
anyOf: [
58+
BASIC_CONFIG_SCHEMA,
59+
{
60+
type: 'boolean',
61+
},
62+
],
63+
} satisfies JSONSchema.JSONSchema4AnyOfSchema
64+
65+
type BasicConfig = Pick<Extract<RuleOptions[0], { when?: any }>, keyof typeof BASIC_CONFIG_SCHEMA['properties']>
66+
67+
interface NormalizedConfig extends BasicConfig {
68+
objectLiteralSpaces?: 'always' | 'never'
69+
}
70+
3271
export default createRule<RuleOptions, MessageIds>({
3372
name: 'jsx-curly-spacing',
3473
meta: {
@@ -41,56 +80,23 @@ export default createRule<RuleOptions, MessageIds>({
4180
messages,
4281

4382
schema: {
44-
definitions: {
45-
basicConfig: {
46-
type: 'object',
47-
properties: {
48-
when: {
49-
type: 'string',
50-
enum: SPACING_VALUES,
51-
},
52-
allowMultiline: {
53-
type: 'boolean',
54-
},
55-
spacing: {
56-
type: 'object',
57-
properties: {
58-
objectLiterals: {
59-
type: 'string',
60-
enum: SPACING_VALUES,
61-
},
62-
},
63-
},
64-
},
65-
},
66-
basicConfigOrBoolean: {
67-
anyOf: [{
68-
$ref: '#/definitions/basicConfig',
69-
}, {
70-
type: 'boolean',
71-
}],
72-
},
73-
},
7483
type: 'array',
7584
items: [{
76-
anyOf: [{
77-
allOf: [{
78-
$ref: '#/definitions/basicConfig',
79-
}, {
85+
anyOf: [
86+
{
8087
type: 'object',
88+
additionalProperties: false,
8189
properties: {
82-
attributes: {
83-
$ref: '#/definitions/basicConfigOrBoolean',
84-
},
85-
children: {
86-
$ref: '#/definitions/basicConfigOrBoolean',
87-
},
90+
...BASIC_CONFIG_SCHEMA.properties,
91+
attributes: BASIC_CONFIG_OR_BOOLEAN_SCHEMA,
92+
children: BASIC_CONFIG_OR_BOOLEAN_SCHEMA,
8893
},
89-
}],
90-
}, {
91-
type: 'string',
92-
enum: SPACING_VALUES,
93-
}],
94+
},
95+
{
96+
type: 'string',
97+
enum: SPACING_VALUES,
98+
},
99+
],
94100
}, {
95101
type: 'object',
96102
properties: {
@@ -105,6 +111,7 @@ export default createRule<RuleOptions, MessageIds>({
105111
enum: SPACING_VALUES,
106112
},
107113
},
114+
additionalProperties: false,
108115
},
109116
},
110117
additionalProperties: false,
@@ -113,11 +120,11 @@ export default createRule<RuleOptions, MessageIds>({
113120
},
114121

115122
create(context) {
116-
function normalizeConfig(configOrTrue: RuleOptions[0] | true, defaults: BasicConfig, lastPass: boolean = false) {
123+
function normalizeConfig(configOrTrue: RuleOptions[0] | true, defaults: NormalizedConfig, lastPass: boolean = false): NormalizedConfig {
117124
const config = configOrTrue === true ? {} : configOrTrue as NonStringConfig
118-
const when = (config as BasicConfig).when || defaults.when
125+
const when = config.when || defaults.when
119126
const allowMultiline = 'allowMultiline' in config ? config.allowMultiline : defaults.allowMultiline
120-
const spacing = (config as BasicConfig).spacing || {}
127+
const spacing = config.spacing || {}
121128
let objectLiteralSpaces = spacing.objectLiterals || defaults.objectLiteralSpaces
122129
if (lastPass) {
123130
// On the final pass assign the values that should be derived from others if they are still undefined

packages/eslint-plugin/rules/jsx-curly-spacing/types.d.ts

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,73 @@
11
/* GENERATED, DO NOT EDIT DIRECTLY */
22

3-
/* @checksum: 6np7FJPRpiFgbrMsnDL-PC4zMTYMNJoW_bCYp5XgxUg */
3+
/* @checksum: _pSba4lVI8bSVjFb0Xmp0h46hFdDbMk9iLr41NY4nwY */
44

55
export type JsxCurlySpacingSchema0
66
= | []
77
| [
8-
| (BasicConfig & {
9-
attributes?: BasicConfigOrBoolean
10-
children?: BasicConfigOrBoolean
11-
[k: string]: unknown
12-
})
8+
| {
9+
when?: 'always' | 'never'
10+
allowMultiline?: boolean
11+
spacing?: {
12+
objectLiterals?: 'always' | 'never'
13+
}
14+
attributes?:
15+
| {
16+
when?: 'always' | 'never'
17+
allowMultiline?: boolean
18+
spacing?: {
19+
objectLiterals?: 'always' | 'never'
20+
}
21+
}
22+
| boolean
23+
children?:
24+
| {
25+
when?: 'always' | 'never'
26+
allowMultiline?: boolean
27+
spacing?: {
28+
objectLiterals?: 'always' | 'never'
29+
}
30+
}
31+
| boolean
32+
}
1333
| ('always' | 'never'),
1434
]
1535
| [
1636
(
17-
| (BasicConfig & {
18-
attributes?: BasicConfigOrBoolean
19-
children?: BasicConfigOrBoolean
20-
[k: string]: unknown
21-
})
37+
| {
38+
when?: 'always' | 'never'
39+
allowMultiline?: boolean
40+
spacing?: {
41+
objectLiterals?: 'always' | 'never'
42+
}
43+
attributes?:
44+
| {
45+
when?: 'always' | 'never'
46+
allowMultiline?: boolean
47+
spacing?: {
48+
objectLiterals?: 'always' | 'never'
49+
}
50+
}
51+
| boolean
52+
children?:
53+
| {
54+
when?: 'always' | 'never'
55+
allowMultiline?: boolean
56+
spacing?: {
57+
objectLiterals?: 'always' | 'never'
58+
}
59+
}
60+
| boolean
61+
}
2262
| ('always' | 'never')
2363
),
2464
{
2565
allowMultiline?: boolean
2666
spacing?: {
2767
objectLiterals?: 'always' | 'never'
28-
[k: string]: unknown
2968
}
3069
},
3170
]
32-
export type BasicConfigOrBoolean = BasicConfig | boolean
33-
34-
export interface BasicConfig {
35-
when?: 'always' | 'never'
36-
allowMultiline?: boolean
37-
spacing?: {
38-
objectLiterals?: 'always' | 'never'
39-
[k: string]: unknown
40-
}
41-
[k: string]: unknown
42-
}
4371

4472
export type JsxCurlySpacingRuleOptions
4573
= JsxCurlySpacingSchema0

packages/eslint-plugin/rules/jsx-indent-props/jsx-indent-props.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export default createRule<RuleOptions, MessageIds>({
7878
type: 'boolean',
7979
},
8080
},
81+
additionalProperties: false,
8182
},
8283
],
8384
}],

packages/eslint-plugin/rules/jsx-indent-props/types.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
/* GENERATED, DO NOT EDIT DIRECTLY */
22

3-
/* @checksum: yoq47ZFhtyO96ComOIYTmw3_4sqKrrg0Nv2kSf7dBYo */
3+
/* @checksum: 30G_ZrWyAGlBb_DWlLb43wDD7IXoSKVHzxArM3d3ta0 */
44

55
export type JsxIndentPropsSchema0
66
= | ('tab' | 'first')
77
| number
88
| {
99
indentMode?: ('tab' | 'first') | number
1010
ignoreTernaryOperator?: boolean
11-
[k: string]: unknown
1211
}
1312

1413
export type JsxIndentPropsRuleOptions = [

packages/eslint-plugin/rules/jsx-max-props-per-line/jsx-max-props-per-line.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default createRule<RuleOptions, MessageIds>({
4646
minimum: 1,
4747
},
4848
},
49+
additionalProperties: false,
4950
},
5051
},
5152
additionalProperties: false,

packages/eslint-plugin/rules/jsx-max-props-per-line/types.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
/* GENERATED, DO NOT EDIT DIRECTLY */
22

3-
/* @checksum: KOe_wU0r0lsPla5JCuUUOQFmBSnDNOIm5DXIMsRs3g4 */
3+
/* @checksum: Cw90UTKsHhPLRv0dTeRqkUFq1z8xJdtpn3CUONfizj4 */
44

55
export type JsxMaxPropsPerLineSchema0
66
= | {
77
maximum?: {
88
single?: number
99
multi?: number
10-
[k: string]: unknown
1110
}
1211
}
1312
| {

packages/eslint-plugin/rules/multiline-ternary/multiline-ternary.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export default createRule<RuleOptions, MessageIds>({
2929
default: false,
3030
},
3131
},
32+
additionalProperties: false,
3233
},
3334
],
3435

packages/eslint-plugin/rules/multiline-ternary/types.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* GENERATED, DO NOT EDIT DIRECTLY */
22

3-
/* @checksum: F-2EEbab-hKzmU-DEbejw9951edJ-x4z3ngYponoEZI */
3+
/* @checksum: B9Xuzw8xZiK_fneqs8jInWdZlTCYrLuG3DJ0naQrxnY */
44

55
export type MultilineTernarySchema0
66
= | 'always'
@@ -9,7 +9,6 @@ export type MultilineTernarySchema0
99

1010
export interface MultilineTernarySchema1 {
1111
ignoreJSX?: boolean
12-
[k: string]: unknown
1312
}
1413

1514
export type MultilineTernaryRuleOptions = [

0 commit comments

Comments
 (0)