Skip to content

fix: disallow extra properties in rule options #410

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/rules/dynamic-import-chunkname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default createRule<[Options?], MessageId>({
type: 'string',
},
},
additionalProperties: false,
},
],
messages: {
Expand Down
1 change: 1 addition & 0 deletions src/rules/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const properties = {
type: 'boolean',
},
},
additionalProperties: false,
} satisfies JSONSchema.JSONSchema4

export type Modifier = (typeof modifierValues)[number]
Expand Down
1 change: 1 addition & 0 deletions src/rules/no-namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default createRule<[Options?], MessageId>({
uniqueItems: true,
},
},
additionalProperties: false,
},
],
messages: {
Expand Down
80 changes: 44 additions & 36 deletions src/rules/no-unused-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import path from 'node:path'

import { TSESTree } from '@typescript-eslint/types'
import type { TSESLint } from '@typescript-eslint/utils'
import type { JSONSchema, TSESLint } from '@typescript-eslint/utils'
// eslint-disable-next-line import-x/default -- incorrect types , commonjs actually
import eslintUnsupportedApi from 'eslint/use-at-your-own-risk'

Expand Down Expand Up @@ -444,13 +444,47 @@ const fileIsInPkg = (file: string) => {
export interface Options {
src?: string[]
ignoreExports?: string[]
missingExports?: true
missingExports?: boolean
unusedExports?: boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should change it to be true to align the json schema instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, the first branch of anyOf allows missingExports to have any boolean value, so I thought I'd change the TypeScript type to reflect that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As in, move the enum: [true] of unusedExports to SHARED_OPTIONS_SCHEMA_PROPERTIES? Should the same be done to missingExports? In general, constraining boolean properties to just a single value looks like an uncommon practice. I wonder if this is needed at all?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@43081j @SukkaW Any inputs?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I would probably set the type as boolean. So it should be possible for someone to set it to false even if it is the default

ignoreUnusedTypeExports?: boolean
}

type MessageId = 'notFound' | 'unused'

const SHARED_OPTIONS_SCHEMA_PROPERTIES = {
src: {
description: 'files/paths to be analyzed (only for unused exports)',
type: 'array',
uniqueItems: true,
items: {
type: 'string',
minLength: 1,
},
},
ignoreExports: {
description:
'files/paths for which unused exports will not be reported (e.g module entry points)',
type: 'array',
uniqueItems: true,
items: {
type: 'string',
minLength: 1,
},
},
missingExports: {
description: 'report modules without any exports',
type: 'boolean',
},
unusedExports: {
description: 'report exports without any usage',
type: 'boolean',
},
ignoreUnusedTypeExports: {
description: 'ignore type exports without any usage',
type: 'boolean',
},
} satisfies JSONSchema.JSONSchema4ObjectSchema['properties']

export default createRule<Options[], MessageId>({
name: 'no-unused-modules',
meta: {
Expand All @@ -462,45 +496,14 @@ export default createRule<Options[], MessageId>({
},
schema: [
{
type: 'object',
properties: {
src: {
description: 'files/paths to be analyzed (only for unused exports)',
type: 'array',
uniqueItems: true,
items: {
type: 'string',
minLength: 1,
},
},
ignoreExports: {
description:
'files/paths for which unused exports will not be reported (e.g module entry points)',
type: 'array',
uniqueItems: true,
items: {
type: 'string',
minLength: 1,
},
},
missingExports: {
description: 'report modules without any exports',
type: 'boolean',
},
unusedExports: {
description: 'report exports without any usage',
type: 'boolean',
},
ignoreUnusedTypeExports: {
description: 'ignore type exports without any usage',
type: 'boolean',
},
},
anyOf: [
{
type: 'object',
properties: {
...SHARED_OPTIONS_SCHEMA_PROPERTIES,
unusedExports: {
description:
SHARED_OPTIONS_SCHEMA_PROPERTIES.unusedExports.description,
type: 'boolean',
enum: [true],
},
Expand All @@ -510,16 +513,21 @@ export default createRule<Options[], MessageId>({
},
},
required: ['unusedExports'],
additionalProperties: false,
},
{
type: 'object',
properties: {
...SHARED_OPTIONS_SCHEMA_PROPERTIES,
missingExports: {
description:
SHARED_OPTIONS_SCHEMA_PROPERTIES.missingExports.description,
type: 'boolean',
enum: [true],
},
},
required: ['missingExports'],
additionalProperties: false,
},
],
},
Expand Down
Loading