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): [consistent-type-assertions] wrap object return v…
…alue with parentheses (#6885) * fix(eslint-plugin): [consistent-type-assertions] wrap object return value with parentheses * use getWrappingFixer * fix * fix * fix * fix * update getOperatorPrecedence * use precedence to wrap node in parens * Fix up post-merge * lint --fix * remove comment * Remove post-merge artifact --------- Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
- Loading branch information
1 parent
85f34da
commit 23ac499
Showing
7 changed files
with
318 additions
and
37 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
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,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.