-
Notifications
You must be signed in to change notification settings - Fork 290
Require JSON support for new commands #8406
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
gonzaloriestra
wants to merge
1
commit into
gonzalo/json-error-handling-v2
from
gonzalo/json-support-by-default
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| const {commandExceptions} = require('./json-output-command-exceptions') | ||
|
|
||
| const exemptCommands = new Set(commandExceptions) | ||
|
|
||
| module.exports = { | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: 'require typed JSON output for new finite commands', | ||
| }, | ||
| schema: [], | ||
| messages: { | ||
| missingJsonFlag: | ||
| 'New finite commands must include ...jsonFlag in their static flags. See docs/cli/json-output.md.', | ||
| missingJsonOutputSchema: | ||
| 'New finite commands must declare a static jsonOutputSchema. See docs/cli/json-output.md.', | ||
| }, | ||
| }, | ||
| create(context) { | ||
| const commandPath = repositoryPath(context.filename) | ||
| if (!isCommandPath(commandPath) || exemptCommands.has(commandPath)) return {} | ||
|
|
||
| return { | ||
| ExportDefaultDeclaration(node) { | ||
| if (node.declaration.type !== 'ClassDeclaration') return | ||
|
|
||
| const classMembers = node.declaration.body.body | ||
|
|
||
| if (!hasJsonOutputSchema(classMembers)) { | ||
| context.report({node: node.declaration, messageId: 'missingJsonOutputSchema'}) | ||
| } | ||
| if (!hasJsonFlag(classMembers)) { | ||
| context.report({node: node.declaration, messageId: 'missingJsonFlag'}) | ||
| } | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| function isCommandPath(commandPath) { | ||
| return /\/src\/(?:cli\/)?commands\//.test(commandPath) | ||
| } | ||
|
|
||
| function repositoryPath(filename) { | ||
| const normalizedFilename = filename.replaceAll('\\', '/') | ||
| const packagesDirectory = normalizedFilename.lastIndexOf('/packages/') | ||
| return packagesDirectory === -1 ? normalizedFilename : normalizedFilename.slice(packagesDirectory + 1) | ||
| } | ||
|
|
||
| function hasJsonOutputSchema(classMembers) { | ||
| return classMembers.some( | ||
| (member) => | ||
| member.type === 'MethodDefinition' && member.kind === 'get' && isStaticMemberNamed(member, 'jsonOutputSchema'), | ||
| ) | ||
| } | ||
|
|
||
| function hasJsonFlag(classMembers) { | ||
| const flags = classMembers.find((member) => isStaticMemberNamed(member, 'flags')) | ||
| return flags?.value?.type === 'ObjectExpression' && flags.value.properties.some(isJsonFlagSpread) | ||
| } | ||
|
|
||
| function isStaticMemberNamed(member, name) { | ||
| return member.static && !member.computed && member.key?.name === name | ||
| } | ||
|
|
||
| function isJsonFlagSpread(property) { | ||
| return ( | ||
| property.type === 'SpreadElement' && | ||
| property.argument.type === 'Identifier' && | ||
| property.argument.name === 'jsonFlag' | ||
| ) | ||
| } |
113 changes: 113 additions & 0 deletions
113
packages/eslint-plugin-cli/rules/command-json-output.test.js
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 |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| const {RuleTester} = require('eslint') | ||
| const typescriptParser = require('@typescript-eslint/parser') | ||
|
|
||
| const rule = require('./command-json-output') | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| languageOptions: { | ||
| ecmaVersion: 2022, | ||
| sourceType: 'module', | ||
| parser: typescriptParser, | ||
| }, | ||
| }) | ||
|
|
||
| ruleTester.run('command-json-output', rule, { | ||
| valid: [ | ||
| { | ||
| name: 'finite query command', | ||
| filename: '/repo/packages/app/src/cli/commands/app/widgets/list.ts', | ||
| code: ` | ||
| export default class WidgetList extends Command { | ||
| static flags = {...jsonFlag} | ||
| static get jsonOutputSchema() { | ||
| return widgetListJsonOutputSchema | ||
| } | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| name: 'finite operation command', | ||
| filename: '/repo/packages/app/src/cli/commands/app/widgets/delete.ts', | ||
| code: ` | ||
| export default class WidgetDelete extends Command { | ||
| static flags = {...globalFlags, ...jsonFlag} | ||
| static get jsonOutputSchema() { | ||
| return widgetDeleteJsonOutputSchema | ||
| } | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| name: 'allow-listed streaming command', | ||
| filename: '/repo/packages/app/src/cli/commands/app/dev.ts', | ||
| code: 'export default class Dev extends Command {}', | ||
| }, | ||
| { | ||
| name: 'legacy command baseline', | ||
| filename: '/repo/packages/app/src/cli/commands/app/build.ts', | ||
| code: 'export default class Build extends Command {}', | ||
| }, | ||
| { | ||
| name: 'non-command module', | ||
| filename: '/repo/packages/app/src/cli/services/widgets.ts', | ||
| code: 'export default class WidgetService {}', | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| name: 'streaming marker without an allow-list entry', | ||
| filename: '/repo/packages/app/src/cli/commands/app/widgets/watch.ts', | ||
| code: ` | ||
| export default class WidgetWatch extends Command { | ||
| static jsonOutputSupport = 'streaming' as const | ||
| } | ||
| `, | ||
| errors: [{messageId: 'missingJsonOutputSchema'}, {messageId: 'missingJsonFlag'}], | ||
| }, | ||
| { | ||
| name: 'new subcommand of an allow-listed streaming command', | ||
| filename: '/repo/packages/app/src/cli/commands/app/dev/status.ts', | ||
| code: 'export default class DevStatus extends Command {}', | ||
| errors: [{messageId: 'missingJsonOutputSchema'}, {messageId: 'missingJsonFlag'}], | ||
| }, | ||
| { | ||
| name: 'new command without JSON support', | ||
| filename: '/repo/packages/app/src/cli/commands/app/widgets/create.ts', | ||
| code: 'export default class WidgetCreate extends Command {}', | ||
| errors: [ | ||
| { | ||
| message: 'New finite commands must declare a static jsonOutputSchema. See docs/cli/json-output.md.', | ||
| }, | ||
| { | ||
| message: 'New finite commands must include ...jsonFlag in their static flags. See docs/cli/json-output.md.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'command missing its schema', | ||
| filename: '/repo/packages/app/src/cli/commands/app/widgets/search.ts', | ||
| code: 'export default class WidgetSearch extends Command { static flags = {...jsonFlag} }', | ||
| errors: [ | ||
| { | ||
| message: 'New finite commands must declare a static jsonOutputSchema. See docs/cli/json-output.md.', | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| name: 'command missing its JSON flag', | ||
| filename: '/repo/packages/app/src/cli/commands/app/widgets/update.ts', | ||
| code: ` | ||
| export default class WidgetUpdate extends Command { | ||
| static get jsonOutputSchema() { | ||
| return widgetUpdateJsonOutputSchema | ||
| } | ||
| } | ||
| `, | ||
| errors: [ | ||
| { | ||
| message: 'New finite commands must include ...jsonFlag in their static flags. See docs/cli/json-output.md.', | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.