Skip to content
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): [space-infix-ops] support for optional property without type #5155

Merged
merged 4 commits into from Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions packages/eslint-plugin/src/rules/space-infix-ops.ts
Expand Up @@ -69,17 +69,16 @@ export default util.createRule<Options, MessageIds>({

function isSpaceChar(token: TSESTree.Token): boolean {
return (
token.type === AST_TOKEN_TYPES.Punctuator &&
/^[=|?|:]$/.test(token.value)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this regexp was not correct, it should be, /^[=?:]$/ or /^(=|?|:)$/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty common mistake in regexes!
Probably deserves its own lint rule!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend this plugin, if you have a lot of non-trivial regexps to lint

token.type === AST_TOKEN_TYPES.Punctuator && /^[=?:]$/.test(token.value)
);
}

function checkAndReportAssignmentSpace(
node: TSESTree.Node,
leftNode: TSESTree.Token,
leftNode: TSESTree.Token | null,
rightNode?: TSESTree.Token | null,
): void {
if (!rightNode) {
if (!rightNode || !leftNode) {
return;
}

Expand Down Expand Up @@ -109,7 +108,7 @@ export default util.createRule<Options, MessageIds>({
return;
}

const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!;
const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0]);
const rightNode = sourceCode.getTokenByRangeStart(
node.initializer.range[0],
)!;
Expand All @@ -124,9 +123,11 @@ export default util.createRule<Options, MessageIds>({
function checkForPropertyDefinitionAssignmentSpace(
node: TSESTree.PropertyDefinition,
): void {
const leftNode = sourceCode.getLastToken(
node.typeAnnotation ?? node.key,
)!;
const leftNode =
node.optional && !node.typeAnnotation
? sourceCode.getTokenAfter(node.key)
: sourceCode.getLastToken(node.typeAnnotation ?? node.key);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isSpaceChar is allowing ? as token, than we have to skip it,


const rightNode = node.value
? sourceCode.getTokenByRangeStart(node.value.range[0])
: undefined;
Expand Down Expand Up @@ -174,16 +175,16 @@ export default util.createRule<Options, MessageIds>({
function checkForTypeAliasAssignment(
node: TSESTree.TSTypeAliasDeclaration,
): void {
const leftNode = sourceCode.getLastToken(node.typeParameters ?? node.id)!;
const leftNode = sourceCode.getLastToken(node.typeParameters ?? node.id);
const rightNode = sourceCode.getFirstToken(node.typeAnnotation);

checkAndReportAssignmentSpace(node, leftNode, rightNode);
}

function checkForTypeConditional(node: TSESTree.TSConditionalType): void {
const extendsLastToken = sourceCode.getLastToken(node.extendsType)!;
const trueFirstToken = sourceCode.getFirstToken(node.trueType)!;
const trueLastToken = sourceCode.getLastToken(node.trueType)!;
const trueFirstToken = sourceCode.getFirstToken(node.trueType);
const trueLastToken = sourceCode.getLastToken(node.trueType);
const falseFirstToken = sourceCode.getFirstToken(node.falseType)!;

checkAndReportAssignmentSpace(node, extendsLastToken, trueFirstToken);
Expand Down
40 changes: 33 additions & 7 deletions packages/eslint-plugin/tests/rules/space-infix-ops.test.ts
Expand Up @@ -165,6 +165,20 @@ ruleTester.run('space-infix-ops', rule, {
}
`,
},
{
code: `
class Test {
value: string & number;
}
`,
},
{
code: `
class Test {
optional? = false;
}
`,
},
{
code: `
type Test =
Expand Down Expand Up @@ -379,13 +393,6 @@ ruleTester.run('space-infix-ops', rule, {
const x: string & (((() => void)));
`,
},
{
code: `
class Test {
value: string & number;
}
`,
},
{
code: `
function foo<T extends string & number>() {}
Expand Down Expand Up @@ -1861,6 +1868,25 @@ ruleTester.run('space-infix-ops', rule, {
},
],
},
{
code: `
class Test {
optional?= false;
}
`,
output: `
class Test {
optional? = false;
}
`,
errors: [
{
messageId: 'missingSpace',
column: 20,
line: 3,
},
],
},
{
code: `
function foo<T extends string &number>() {}
Expand Down