Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
chore(website): validate rule options in editor#6907
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
7 commits Select commitHold shift + click to select a range
fda2c70
chore(website): validate rule options in editor
armano201bd2a8
chore(website): add support for oneOf and anyOf
armano2035a478
Merge remote-tracking branch 'origin/v6' into chore/playground-valida…
armano2b830f93
chore: add logging for unsupported schemas / invalid, and add fallback
armano27c699d3
chore: do not show messages for empty objects
armano2664cc78
fix: remove invalid condition
armano23a5b13a
fix: filter out invalid error codes
armano2File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
10 changes: 9 additions & 1 deletionpackages/website/src/components/editor/LoadedEditor.tsx
This file contains hidden or 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 |
---|---|---|
@@ -8,6 +8,7 @@ import { createCompilerOptions } from '../lib/createCompilerOptions'; | ||
import { debounce } from '../lib/debounce'; | ||
import { | ||
getEslintJsonSchema, | ||
getRuleJsonSchemaWithErrorLevel, | ||
getTypescriptJsonSchema, | ||
} from '../lib/jsonSchema'; | ||
import { parseTSConfig, tryParseEslintModule } from '../lib/parseConfig'; | ||
@@ -147,16 +148,23 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({ | ||
}, [webLinter, onEsASTChange, onScopeChange, onTsASTChange]); | ||
useEffect(() => { | ||
const createRuleUri = (name: string): string => | ||
monaco.Uri.parse(`/rules/${name.replace('@', '')}.json`).toString(); | ||
// configure the JSON language support with schemas and schema associations | ||
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ | ||
validate: true, | ||
enableSchemaRequest: false, | ||
allowComments: true, | ||
schemas: [ | ||
...Array.from(webLinter.rules.values()).map(rule => ({ | ||
uri: createRuleUri(rule.name), | ||
schema: getRuleJsonSchemaWithErrorLevel(rule.name, rule.schema), | ||
})), | ||
armano2 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
{ | ||
uri: monaco.Uri.file('eslint-schema.json').toString(), // id of the first schema | ||
fileMatch: ['/.eslintrc'], // associate with our model | ||
schema: getEslintJsonSchema(webLinter, createRuleUri), | ||
}, | ||
{ | ||
uri: monaco.Uri.file('ts-schema.json').toString(), // id of the first schema | ||
1 change: 1 addition & 0 deletionspackages/website/src/components/editor/useSandboxServices.ts
This file contains hidden or 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
105 changes: 89 additions & 16 deletionspackages/website/src/components/lib/jsonSchema.ts
This file contains hidden or 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 |
---|---|---|
@@ -3,33 +3,106 @@ import type * as ts from 'typescript'; | ||
import type { CreateLinter } from '../linter/createLinter'; | ||
const defaultRuleSchema: JSONSchema4 = { | ||
type: ['string', 'number'], | ||
enum: ['off', 'warn', 'error', 0, 1, 2], | ||
}; | ||
/** | ||
* Add the error level to the rule schema items | ||
* | ||
* if you encounter issues with rule schema validation you can check the schema by using the following code in the console: | ||
* monaco.languages.json.jsonDefaults.diagnosticsOptions.schemas.find(item => item.uri.includes('typescript-eslint/consistent-type-imports')) | ||
* monaco.languages.json.jsonDefaults.diagnosticsOptions.schemas.find(item => item.uri.includes('no-unused-labels')) | ||
* monaco.languages.json.jsonDefaults.diagnosticsOptions.schemas.filter(item => item.schema.type === 'array') | ||
*/ | ||
export function getRuleJsonSchemaWithErrorLevel( | ||
name: string, | ||
ruleSchema: JSONSchema4 | JSONSchema4[], | ||
): JSONSchema4 { | ||
if (Array.isArray(ruleSchema)) { | ||
return { | ||
type: 'array', | ||
items: [defaultRuleSchema, ...ruleSchema], | ||
minItems: 1, | ||
additionalItems: false, | ||
}; | ||
} | ||
// TODO: delete this once we update schemas | ||
// example: ban-ts-comment | ||
if (Array.isArray(ruleSchema.prefixItems)) { | ||
const { prefixItems, ...rest } = ruleSchema; | ||
return { | ||
...rest, | ||
items: [defaultRuleSchema, ...(prefixItems as JSONSchema4[])], | ||
maxItems: ruleSchema.maxItems ? ruleSchema.maxItems + 1 : undefined, | ||
minItems: ruleSchema.minItems ? ruleSchema.minItems + 1 : 1, | ||
additionalItems: false, | ||
}; | ||
} | ||
// example: explicit-member-accessibility | ||
if (Array.isArray(ruleSchema.items)) { | ||
return { | ||
...ruleSchema, | ||
items: [defaultRuleSchema, ...ruleSchema.items], | ||
maxItems: ruleSchema.maxItems ? ruleSchema.maxItems + 1 : undefined, | ||
minItems: ruleSchema.minItems ? ruleSchema.minItems + 1 : 1, | ||
additionalItems: false, | ||
}; | ||
} | ||
if (typeof ruleSchema.items === 'object' && ruleSchema.items) { | ||
// example: naming-convention rule | ||
return { | ||
...ruleSchema, | ||
items: [defaultRuleSchema], | ||
additionalItems: ruleSchema.items, | ||
}; | ||
} | ||
// example eqeqeq | ||
if (Array.isArray(ruleSchema.anyOf)) { | ||
return { | ||
...ruleSchema, | ||
anyOf: ruleSchema.anyOf.map(item => | ||
getRuleJsonSchemaWithErrorLevel(name, item), | ||
), | ||
}; | ||
} | ||
// example logical-assignment-operators | ||
if (Array.isArray(ruleSchema.oneOf)) { | ||
return { | ||
...ruleSchema, | ||
oneOf: ruleSchema.oneOf.map(item => | ||
getRuleJsonSchemaWithErrorLevel(name, item), | ||
), | ||
}; | ||
} | ||
if (typeof ruleSchema !== 'object' || Object.keys(ruleSchema).length) { | ||
console.error('unsupported rule schema', name, ruleSchema); | ||
} | ||
armano2 marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
return { | ||
type: 'array', | ||
items: [defaultRuleSchema], | ||
minItems: 1, | ||
additionalItems: false, | ||
}; | ||
} | ||
/** | ||
* Get the JSON schema for the eslint config | ||
* Currently we only support the rules and extends | ||
*/ | ||
export function getEslintJsonSchema( | ||
linter: CreateLinter, | ||
createRef: (name: string) => string, | ||
): JSONSchema4 { | ||
const properties: Record<string, JSONSchema4> = {}; | ||
for (const [, item] of linter.rules) { | ||
properties[item.name] = { | ||
description: `${item.description}\n ${item.url}`, | ||
title: item.name.startsWith('@typescript') ? 'Rules' : 'Core rules', | ||
default: 'off', | ||
oneOf: [defaultRuleSchema, { $ref: createRef(item.name) }], | ||
}; | ||
} | ||
13 changes: 11 additions & 2 deletionspackages/website/src/components/linter/createLinter.ts
This file contains hidden or 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
5 changes: 3 additions & 2 deletionspackages/website/src/components/linter/utils.ts
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.