Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): [consistent-type-assertions] wrap object return value with parentheses#6885
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
JoshuaKGoldberg merged 14 commits intotypescript-eslint:mainfromdora1998:fix-consistent-type-assertion-fixerAug 27, 2023
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
14 commits Select commitHold shift + click to select a range
8b43da5 fix(eslint-plugin): [consistent-type-assertions] wrap object return v…
dora1998c72d3c2 use getWrappingFixer
dora19982662e6e fix
dora1998620311f fix
dora19985862985 fix
dora199802ada97 fix
dora199800771e7 update getOperatorPrecedence
dora19982d10a7f use precedence to wrap node in parens
dora199835c71f7 Merge branch 'main' into fix-consistent-type-assertion-fixer
JoshuaKGoldberg0552295 Fix up post-merge
JoshuaKGoldberg0599935 lint --fix
JoshuaKGoldberg0e2277d remove comment
JoshuaKGoldbergf301006 Merge branch 'main' into fix-consistent-type-assertion-fixer
JoshuaKGoldberg6948bfd Remove post-merge artifact
JoshuaKGoldbergFile 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
49 changes: 39 additions & 10 deletionspackages/eslint-plugin/src/rules/consistent-type-assertions.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
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/util/getOperatorPrecedence.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
9 changes: 9 additions & 0 deletionspackages/eslint-plugin/src/util/getWrappedCode.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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import type { OperatorPrecedence } from './getOperatorPrecedence'; | ||
| export function getWrappedCode( | ||
| text: string, | ||
| nodePrecedence: OperatorPrecedence, | ||
| parentPrecedence: OperatorPrecedence, | ||
| ): string { | ||
| return nodePrecedence > parentPrecedence ? text : `(${text})`; | ||
| } |
32 changes: 27 additions & 5 deletionspackages/eslint-plugin/src/util/getWrappingFixer.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
84 changes: 78 additions & 6 deletionspackages/eslint-plugin/tests/rules/consistent-type-assertions.test.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
91 changes: 91 additions & 0 deletionspackages/eslint-plugin/tests/util/getWrappedCode.test.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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import type { TSESTree } from '@typescript-eslint/utils'; | ||
| import * as ts from 'typescript'; | ||
| import * as util from '../../src/util'; | ||
| import { getWrappedCode } from '../../src/util/getWrappedCode'; | ||
| import { getFixturesRootDir } from '../RuleTester'; | ||
| const rootPath = getFixturesRootDir(); | ||
| const ruleTester = new RuleTester({ | ||
| parser: '@typescript-eslint/parser', | ||
| parserOptions: { | ||
| tsconfigRootDir: rootPath, | ||
| project: './tsconfig.json', | ||
| }, | ||
| }); | ||
| const removeFunctionRule = util.createRule({ | ||
| name: 'remove-function', | ||
| defaultOptions: [], | ||
| meta: { | ||
| type: 'suggestion', | ||
| fixable: 'code', | ||
| docs: { | ||
| description: | ||
| 'Remove function with first arg remaining in random places for test purposes.', | ||
| }, | ||
| messages: { | ||
| removeFunction: 'Please remove this function', | ||
| }, | ||
| schema: [], | ||
| }, | ||
| create(context) { | ||
| const sourceCode = context.getSourceCode(); | ||
| const parserServices = util.getParserServices(context, true); | ||
| const report = (node: TSESTree.CallExpression): void => { | ||
| context.report({ | ||
| node, | ||
| messageId: 'removeFunction', | ||
| fix: fixer => { | ||
| const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node); | ||
| const tsArgumentNode = tsNode.arguments[0]; | ||
| const nodePrecedence = util.getOperatorPrecedence( | ||
| tsArgumentNode.kind, | ||
| ts.isBinaryExpression(tsArgumentNode) | ||
| ? tsArgumentNode.operatorToken.kind | ||
| : ts.SyntaxKind.Unknown, | ||
| ); | ||
| const parentPrecedence = util.getOperatorPrecedence( | ||
| tsNode.parent.kind, | ||
| ts.isBinaryExpression(tsNode.parent) | ||
| ? tsNode.parent.operatorToken.kind | ||
| : ts.SyntaxKind.Unknown, | ||
| ); | ||
| const text = sourceCode.getText(node.arguments[0]); | ||
| return fixer.replaceText( | ||
| node, | ||
| getWrappedCode(text, nodePrecedence, parentPrecedence), | ||
| ); | ||
| }, | ||
| }); | ||
| }; | ||
| return { | ||
| 'CallExpression[callee.name="fn"]': report, | ||
| }; | ||
| }, | ||
| }); | ||
| ruleTester.run('getWrappedCode - removeFunctionRule', removeFunctionRule, { | ||
| valid: [], | ||
| invalid: [ | ||
| // should add parens when the first argument node has lower precedence than the parent node of the CallExpression | ||
| { | ||
| code: '() => fn({ x: "wrapObject" })', | ||
| errors: [{ messageId: 'removeFunction' }], | ||
| output: '() => ({ x: "wrapObject" })', | ||
| }, | ||
| // shouldn't add parens when not necessary | ||
| { | ||
| code: 'const a = fn({ x: "wrapObject" })', | ||
| errors: [{ messageId: 'removeFunction' }], | ||
| output: 'const a = { x: "wrapObject" }', | ||
| }, | ||
| ], | ||
| }); |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.