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): [space-infix-ops] support for optional property without type#5155
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
3bbe0c1
d1cc86a
b09c698
47e7a2b
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -36,13 +36,9 @@ export default util.createRule<Options, MessageIds>({ | ||
const rules = baseRule.create(context); | ||
const sourceCode = context.getSourceCode(); | ||
function report(operator: TSESTree.Token): void { | ||
context.report({ | ||
node: operator, | ||
messageId: 'missingSpace', | ||
data: { | ||
operator: operator.value, | ||
@@ -65,38 +61,36 @@ export default util.createRule<Options, MessageIds>({ | ||
return fixer.replaceText(operator, fixString); | ||
}, | ||
}); | ||
} | ||
function isSpaceChar(token: TSESTree.Token): boolean { | ||
return ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. this regexp was not correct, it should be, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This is a pretty common mistake in regexes! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I recommendthis plugin, if you have a lot of non-trivial regexps to lint | ||
token.type === AST_TOKEN_TYPES.Punctuator && /^[=?:]$/.test(token.value) | ||
); | ||
} | ||
function checkAndReportAssignmentSpace( | ||
leftNode: TSESTree.Token | TSESTree.Node | null, | ||
rightNode?: TSESTree.Token | TSESTree.Node | null, | ||
): void { | ||
if (!rightNode || !leftNode) { | ||
return; | ||
} | ||
const operator = sourceCode.getFirstTokenBetween( | ||
leftNode, | ||
rightNode, | ||
isSpaceChar, | ||
)!; | ||
const prev = sourceCode.getTokenBefore(operator)!; | ||
const next = sourceCode.getTokenAfter(operator)!; | ||
if ( | ||
!sourceCode.isSpaceBetween!(prev, operator) || | ||
!sourceCode.isSpaceBetween!(operator, next) | ||
) { | ||
report(operator); | ||
} | ||
} | ||
@@ -105,16 +99,7 @@ export default util.createRule<Options, MessageIds>({ | ||
* @param node The node to report | ||
*/ | ||
function checkForEnumAssignmentSpace(node: TSESTree.TSEnumMember): void { | ||
checkAndReportAssignmentSpace(node.id, node.initializer); | ||
} | ||
/** | ||
@@ -124,14 +109,12 @@ export default util.createRule<Options, MessageIds>({ | ||
function checkForPropertyDefinitionAssignmentSpace( | ||
node: TSESTree.PropertyDefinition, | ||
): void { | ||
const leftNode = | ||
node.optional && !node.typeAnnotation | ||
? sourceCode.getTokenAfter(node.key) | ||
: node.typeAnnotation ?? node.key; | ||
checkAndReportAssignmentSpace(leftNode,node.value); | ||
} | ||
/** | ||
@@ -161,7 +144,7 @@ export default util.createRule<Options, MessageIds>({ | ||
!sourceCode.isSpaceBetween!(prev!, operator) || | ||
!sourceCode.isSpaceBetween!(operator, next!) | ||
) { | ||
report(operator); | ||
} | ||
} | ||
}); | ||
@@ -174,20 +157,15 @@ export default util.createRule<Options, MessageIds>({ | ||
function checkForTypeAliasAssignment( | ||
node: TSESTree.TSTypeAliasDeclaration, | ||
): void { | ||
checkAndReportAssignmentSpace( | ||
node.typeParameters ??node.id, | ||
node.typeAnnotation, | ||
); | ||
} | ||
function checkForTypeConditional(node: TSESTree.TSConditionalType): void { | ||
checkAndReportAssignmentSpace(node.extendsType, node.trueType); | ||
checkAndReportAssignmentSpace(node.trueType, node.falseType); | ||
} | ||
return { | ||