Skip to content

Commit 5414bf2

Browse files
authored
feat(eslint-plugin): [space-infix-ops] support for class properties and type aliases (typescript-eslint#3231)
1 parent 40bdb0b commit 5414bf2

File tree

2 files changed

+151
-15
lines changed

2 files changed

+151
-15
lines changed

packages/eslint-plugin/src/rules/space-infix-ops.ts

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,11 @@ export default util.createRule<Options, MessageIds>({
3434
const rules = baseRule.create(context);
3535
const sourceCode = context.getSourceCode();
3636

37-
/**
38-
* Check if it has an assignment char and report if it's faulty
39-
* @param node The node to report
40-
*/
41-
function checkForAssignmentSpace(node: TSESTree.TSEnumMember): void {
42-
if (!node.initializer) {
43-
return;
44-
}
45-
46-
const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!;
47-
const rightNode = sourceCode.getTokenByRangeStart(
48-
node.initializer.range[0],
49-
)!;
50-
37+
function checkAndReportAssignmentSpace(
38+
node: TSESTree.Node,
39+
leftNode: TSESTree.Token,
40+
rightNode?: TSESTree.Token | null,
41+
): void {
5142
if (!rightNode) {
5243
return;
5344
}
@@ -94,9 +85,60 @@ export default util.createRule<Options, MessageIds>({
9485
}
9586
}
9687

88+
/**
89+
* Check if it has an assignment char and report if it's faulty
90+
* @param node The node to report
91+
*/
92+
function checkForEnumAssignmentSpace(node: TSESTree.TSEnumMember): void {
93+
if (!node.initializer) {
94+
return;
95+
}
96+
97+
const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!;
98+
const rightNode = sourceCode.getTokenByRangeStart(
99+
node.initializer.range[0],
100+
)!;
101+
102+
checkAndReportAssignmentSpace(node, leftNode, rightNode);
103+
}
104+
105+
/**
106+
* Check if it has an assignment char and report if it's faulty
107+
* @param node The node to report
108+
*/
109+
function checkForClassPropertyAssignmentSpace(
110+
node: TSESTree.ClassProperty,
111+
): void {
112+
const leftNode = sourceCode.getTokenByRangeStart(
113+
node.typeAnnotation?.range[0] ?? node.range[0],
114+
)!;
115+
const rightNode = node.value
116+
? sourceCode.getTokenByRangeStart(node.value.range[0])
117+
: undefined;
118+
119+
checkAndReportAssignmentSpace(node, leftNode, rightNode);
120+
}
121+
122+
/**
123+
* Check if it has an assignment char and report if it's faulty
124+
* @param node The node to report
125+
*/
126+
function checkForTypeAliasAssignmentSpace(
127+
node: TSESTree.TSTypeAliasDeclaration,
128+
): void {
129+
const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!;
130+
const rightNode = sourceCode.getTokenByRangeStart(
131+
node.typeAnnotation.range[0],
132+
);
133+
134+
checkAndReportAssignmentSpace(node, leftNode, rightNode);
135+
}
136+
97137
return {
98138
...rules,
99-
TSEnumMember: checkForAssignmentSpace,
139+
TSEnumMember: checkForEnumAssignmentSpace,
140+
ClassProperty: checkForClassPropertyAssignmentSpace,
141+
TSTypeAliasDeclaration: checkForTypeAliasAssignmentSpace,
100142
};
101143
},
102144
});

packages/eslint-plugin/tests/rules/space-infix-ops.test.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,32 @@ ruleTester.run('space-infix-ops', rule, {
3333
}
3434
`,
3535
},
36+
{
37+
code: `
38+
class Test {
39+
public readonly value?: number;
40+
}
41+
`,
42+
},
43+
{
44+
code: `
45+
class Test {
46+
public readonly value = 1;
47+
}
48+
`,
49+
},
50+
{
51+
code: `
52+
class Test {
53+
private value:number = 1;
54+
}
55+
`,
56+
},
57+
{
58+
code: `
59+
type Test = string | boolean;
60+
`,
61+
},
3662
],
3763
invalid: [
3864
{
@@ -98,5 +124,73 @@ ruleTester.run('space-infix-ops', rule, {
98124
},
99125
],
100126
},
127+
{
128+
code: `
129+
class Test {
130+
public readonly value= 2;
131+
}
132+
`,
133+
output: `
134+
class Test {
135+
public readonly value = 2;
136+
}
137+
`,
138+
errors: [
139+
{
140+
messageId: 'missingSpace',
141+
column: 32,
142+
line: 3,
143+
},
144+
],
145+
},
146+
{
147+
code: `
148+
class Test {
149+
public readonly value =2;
150+
}
151+
`,
152+
output: `
153+
class Test {
154+
public readonly value = 2;
155+
}
156+
`,
157+
errors: [
158+
{
159+
messageId: 'missingSpace',
160+
column: 33,
161+
line: 3,
162+
},
163+
],
164+
},
165+
{
166+
code: `
167+
type Test= string | number;
168+
`,
169+
output: `
170+
type Test = string | number;
171+
`,
172+
errors: [
173+
{
174+
messageId: 'missingSpace',
175+
column: 18,
176+
line: 2,
177+
},
178+
],
179+
},
180+
{
181+
code: `
182+
type Test =string | number;
183+
`,
184+
output: `
185+
type Test = string | number;
186+
`,
187+
errors: [
188+
{
189+
messageId: 'missingSpace',
190+
column: 19,
191+
line: 2,
192+
},
193+
],
194+
},
101195
],
102196
});

0 commit comments

Comments
 (0)