eslint-plugin-early-return
Advanced tools
Comparing version 0.0.4 to 0.0.5
import { ESLintUtils, AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
var version = "0.0.4"; | ||
var version = "0.0.5"; | ||
@@ -8,2 +8,5 @@ const createRule = ESLintUtils.RuleCreator((name)=>`https://github.com/hyoban/eslint-plugin-early-return/blob/main/src/rules/${name}.`); | ||
// eslint-disable-next-line unused-imports/no-unused-imports | ||
function isConditionRevertNeedBracket(node) { | ||
return !(node.type === AST_NODE_TYPES.Identifier); | ||
} | ||
function getIndentation(node) { | ||
@@ -41,2 +44,3 @@ return ' '.repeat(node.loc.start.column); | ||
const condition = context.sourceCode.getText(node.test); | ||
const revertCondition = isConditionRevertNeedBracket(node.test) ? `!(${condition})` : `!${condition}`; | ||
let ifText = context.sourceCode.getText(node.consequent); | ||
@@ -46,3 +50,3 @@ ifText = ifText.startsWith('{') && ifText.endsWith('}') ? ifText.replace(/^{/, '').replace(/}$/, '').replaceAll('\n ', '\n').slice(1, -1) : `${getIndentation(node)}${ifText}`; | ||
return [ | ||
fixer.replaceText(node, `if (!(${condition})) ${elseText}\n${ifText}`) | ||
fixer.replaceText(node, `if (${revertCondition}) ${elseText}\n${ifText}`) | ||
]; | ||
@@ -49,0 +53,0 @@ } |
{ | ||
"name": "eslint-plugin-early-return", | ||
"type": "module", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"packageManager": "pnpm@8.15.3", | ||
@@ -6,0 +6,0 @@ "description": "Prefer early return pattern to clean if else statement", |
Sorry, the diff of this file is not supported yet
10225
140