Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(eslint-plugin): fix schemas across several rules and add schema t…
…ests (#6947) * fix(eslint-plugin): fix schemas across several rules and add schema tests * Fix typecheck * Fix lint * Address review feedback * Fix CI * Revert accidental change * Fix tests --------- Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
- Loading branch information
1 parent
6ae1fa7
commit dd31bed
Showing
18 changed files
with
321 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as util from '../src/util'; | ||
import { areOptionsValid } from './areOptionsValid'; | ||
|
||
const exampleRule = util.createRule<['value-a' | 'value-b'], never>({ | ||
name: 'my-example-rule', | ||
meta: { | ||
type: 'layout', | ||
docs: { | ||
description: 'Detects something or other', | ||
}, | ||
schema: [{ type: 'string', enum: ['value-a', 'value-b'] }], | ||
messages: {}, | ||
}, | ||
defaultOptions: ['value-a'], | ||
create() { | ||
return {}; | ||
}, | ||
}); | ||
|
||
test('returns true for valid options', () => { | ||
expect(areOptionsValid(exampleRule, ['value-a'])).toBe(true); | ||
}); | ||
|
||
describe('returns false for invalid options', () => { | ||
test('bad enum value', () => { | ||
expect(areOptionsValid(exampleRule, ['value-c'])).toBe(false); | ||
}); | ||
|
||
test('bad type', () => { | ||
expect(areOptionsValid(exampleRule, [true])).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { TSUtils } from '@typescript-eslint/utils'; | ||
import type { RuleModule } from '@typescript-eslint/utils/ts-eslint'; | ||
import Ajv from 'ajv'; | ||
import type { JSONSchema4 } from 'json-schema'; | ||
|
||
const ajv = new Ajv({ async: false }); | ||
|
||
export function areOptionsValid( | ||
rule: RuleModule<string, readonly unknown[]>, | ||
options: unknown, | ||
): boolean { | ||
const normalizedSchema = normalizeSchema(rule.meta.schema); | ||
|
||
const valid = ajv.validate(normalizedSchema, options); | ||
if (typeof valid !== 'boolean') { | ||
// Schema could not validate options synchronously. This is not allowed for ESLint rules. | ||
return false; | ||
} | ||
|
||
return valid; | ||
} | ||
|
||
function normalizeSchema( | ||
schema: JSONSchema4 | readonly JSONSchema4[], | ||
): JSONSchema4 { | ||
if (!TSUtils.isArray(schema)) { | ||
return schema; | ||
} | ||
|
||
if (schema.length === 0) { | ||
return { | ||
type: 'array', | ||
minItems: 0, | ||
maxItems: 0, | ||
}; | ||
} | ||
|
||
return { | ||
type: 'array', | ||
items: schema as JSONSchema4[], | ||
minItems: 0, | ||
maxItems: schema.length, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
packages/eslint-plugin/tests/schema-snapshots/parameter-properties.shot
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.