Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
bradzacher merged 4 commits intomainfromfix/space-infix-ops-optional-property
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 26 additions & 48 deletionspackages/eslint-plugin/src/rules/space-infix-ops.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,13 +36,9 @@ export default util.createRule<Options, MessageIds>({
const rules = baseRule.create(context);
const sourceCode = context.getSourceCode();

const report = (
node: TSESTree.Node | TSESTree.Token,
operator: TSESTree.Token,
): void => {
function report(operator: TSESTree.Token): void {
context.report({
node: node,
loc: operator.loc,
node: operator,
messageId: 'missingSpace',
data: {
operator: operator.value,
Expand All@@ -65,38 +61,36 @@ export default util.createRule<Options, MessageIds>({
return fixer.replaceText(operator, fixString);
},
});
};
}

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

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 recommendthis plugin, if you have a lot of non-trivial regexps to lint

bradzacher reacted with thumbs up emoji
token.type === AST_TOKEN_TYPES.Punctuator && /^[=?:]$/.test(token.value)
);
}

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

const operator = sourceCode.getFirstTokenBetween(
leftNode,
rightNode,
isSpaceChar,
);
)!;

const prev = sourceCode.getTokenBefore(operator!);
const next = sourceCode.getTokenAfter(operator!);
const prev = sourceCode.getTokenBefore(operator)!;
const next = sourceCode.getTokenAfter(operator)!;

if (
!sourceCode.isSpaceBetween!(prev!, operator!) ||
!sourceCode.isSpaceBetween!(operator!, next!)
!sourceCode.isSpaceBetween!(prev, operator) ||
!sourceCode.isSpaceBetween!(operator, next)
) {
report(node,operator!);
report(operator);
}
}

Expand All@@ -105,16 +99,7 @@ export default util.createRule<Options, MessageIds>({
* @param node The node to report
*/
function checkForEnumAssignmentSpace(node: TSESTree.TSEnumMember): void {
if (!node.initializer) {
return;
}

const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!;
const rightNode = sourceCode.getTokenByRangeStart(
node.initializer.range[0],
)!;

checkAndReportAssignmentSpace(node, leftNode, rightNode);
checkAndReportAssignmentSpace(node.id, node.initializer);
}

/**
Expand All@@ -124,14 +109,12 @@ export default util.createRule<Options, MessageIds>({
function checkForPropertyDefinitionAssignmentSpace(
node: TSESTree.PropertyDefinition,
): void {
const leftNode = sourceCode.getLastToken(
node.typeAnnotation ?? node.key,
)!;
const rightNode = node.value
? sourceCode.getTokenByRangeStart(node.value.range[0])
: undefined;
const leftNode =
node.optional && !node.typeAnnotation
? sourceCode.getTokenAfter(node.key)
: node.typeAnnotation ?? node.key;

checkAndReportAssignmentSpace(node,leftNode,rightNode);
checkAndReportAssignmentSpace(leftNode,node.value);
}

/**
Expand DownExpand Up@@ -161,7 +144,7 @@ export default util.createRule<Options, MessageIds>({
!sourceCode.isSpaceBetween!(prev!, operator) ||
!sourceCode.isSpaceBetween!(operator, next!)
) {
report(typeAnnotation,operator);
report(operator);
}
}
});
Expand All@@ -174,20 +157,15 @@ export default util.createRule<Options, MessageIds>({
function checkForTypeAliasAssignment(
node: TSESTree.TSTypeAliasDeclaration,
): void {
const leftNode = sourceCode.getLastToken(node.typeParameters ?? node.id)!;
const rightNode = sourceCode.getFirstToken(node.typeAnnotation);

checkAndReportAssignmentSpace(node, leftNode, rightNode);
checkAndReportAssignmentSpace(
node.typeParameters ??node.id,
node.typeAnnotation,
);
}

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 falseFirstToken = sourceCode.getFirstToken(node.falseType)!;

checkAndReportAssignmentSpace(node, extendsLastToken, trueFirstToken);
checkAndReportAssignmentSpace(node, trueLastToken, falseFirstToken);
checkAndReportAssignmentSpace(node.extendsType, node.trueType);
checkAndReportAssignmentSpace(node.trueType, node.falseType);
}

return {
Expand Down
125 changes: 118 additions & 7 deletionspackages/eslint-plugin/tests/rules/space-infix-ops.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
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 DownExpand 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 DownExpand Up@@ -984,6 +991,91 @@ ruleTester.run('space-infix-ops', rule, {
},
],
},
{
code: `
type Test = |string|(((() => void)))|string;
`,
output: `
type Test = | string | (((() => void))) | string;
`,
errors: [
{
messageId: 'missingSpace',
column: 21,
line: 2,
},
{
messageId: 'missingSpace',
column: 28,
line: 2,
},
{
messageId: 'missingSpace',
column: 45,
line: 2,
},
],
},
{
code: `
type Test=|string|(((() => void)))|string;
`,
output: `
type Test = |string | (((() => void))) | string;
`,
errors: [
{
messageId: 'missingSpace',
column: 18,
line: 2,
},
{
messageId: 'missingSpace',
column: 19,
line: 2,
},
{
messageId: 'missingSpace',
column: 26,
line: 2,
},
{
messageId: 'missingSpace',
column: 43,
line: 2,
},
],
},
{
code: `
type Test=(string&number)|string|(((() => void)));
`,
output: `
type Test = (string & number) | string | (((() => void)));
`,
errors: [
{
messageId: 'missingSpace',
column: 18,
line: 2,
},
{
messageId: 'missingSpace',
column: 26,
line: 2,
},
{
messageId: 'missingSpace',
column: 34,
line: 2,
},
{
messageId: 'missingSpace',
column: 41,
line: 2,
},
],
},
{
code: `
type Test =
Expand DownExpand Up@@ -1861,6 +1953,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

[8]ページ先頭

©2009-2025 Movatter.jp