-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
fix(eslint-plugin): [consistent-type-assertions] wrap object return value with parentheses #6885
Merged
JoshuaKGoldberg
merged 14 commits into
typescript-eslint:main
from
dora1998:fix-consistent-type-assertion-fixer
Aug 27, 2023
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8b43da5
fix(eslint-plugin): [consistent-type-assertions] wrap object return v…
dora1998 c72d3c2
use getWrappingFixer
dora1998 2662e6e
fix
dora1998 620311f
fix
dora1998 5862985
fix
dora1998 02ada97
fix
dora1998 00771e7
update getOperatorPrecedence
dora1998 2d10a7f
use precedence to wrap node in parens
dora1998 35c71f7
Merge branch 'main' into fix-consistent-type-assertion-fixer
JoshuaKGoldberg 0552295
Fix up post-merge
JoshuaKGoldberg 0599935
lint --fix
JoshuaKGoldberg 0e2277d
remove comment
JoshuaKGoldberg f301006
Merge branch 'main' into fix-consistent-type-assertion-fixer
JoshuaKGoldberg 6948bfd
Remove post-merge artifact
JoshuaKGoldberg File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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})`; | ||
} |
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,92 @@ | ||
import type { TSESTree } from '@typescript-eslint/utils'; | ||
import { isBinaryExpression } from 'tsutils'; | ||
import * as ts from 'typescript'; | ||
|
||
import * as util from '../../src/util'; | ||
import { getWrappedCode } from '../../src/util/getWrappedCode'; | ||
import { getFixturesRootDir, RuleTester } 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.', | ||
recommended: false, | ||
}, | ||
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, | ||
isBinaryExpression(tsArgumentNode) | ||
? tsArgumentNode.operatorToken.kind | ||
: ts.SyntaxKind.Unknown, | ||
); | ||
const parentPrecedence = util.getOperatorPrecedence( | ||
tsNode.parent.kind, | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun merge conflict artifact: we switched to
ts-api-utils
in v6.