Skip to content

Commit

Permalink
feat(eslint-plugin): [space-infix-ops] support for class properties a…
Browse files Browse the repository at this point in the history
…nd type aliases (#3231)
  • Loading branch information
FDIM committed Mar 28, 2021
1 parent 40bdb0b commit 5414bf2
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 15 deletions.
72 changes: 57 additions & 15 deletions packages/eslint-plugin/src/rules/space-infix-ops.ts
Expand Up @@ -34,20 +34,11 @@ export default util.createRule<Options, MessageIds>({
const rules = baseRule.create(context);
const sourceCode = context.getSourceCode();

/**
* Check if it has an assignment char and report if it's faulty
* @param node The node to report
*/
function checkForAssignmentSpace(node: TSESTree.TSEnumMember): void {
if (!node.initializer) {
return;
}

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

function checkAndReportAssignmentSpace(
node: TSESTree.Node,
leftNode: TSESTree.Token,
rightNode?: TSESTree.Token | null,
): void {
if (!rightNode) {
return;
}
Expand Down Expand Up @@ -94,9 +85,60 @@ export default util.createRule<Options, MessageIds>({
}
}

/**
* Check if it has an assignment char and report if it's faulty
* @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);
}

/**
* Check if it has an assignment char and report if it's faulty
* @param node The node to report
*/
function checkForClassPropertyAssignmentSpace(
node: TSESTree.ClassProperty,
): void {
const leftNode = sourceCode.getTokenByRangeStart(
node.typeAnnotation?.range[0] ?? node.range[0],
)!;
const rightNode = node.value
? sourceCode.getTokenByRangeStart(node.value.range[0])
: undefined;

checkAndReportAssignmentSpace(node, leftNode, rightNode);
}

/**
* Check if it has an assignment char and report if it's faulty
* @param node The node to report
*/
function checkForTypeAliasAssignmentSpace(
node: TSESTree.TSTypeAliasDeclaration,
): void {
const leftNode = sourceCode.getTokenByRangeStart(node.id.range[0])!;
const rightNode = sourceCode.getTokenByRangeStart(
node.typeAnnotation.range[0],
);

checkAndReportAssignmentSpace(node, leftNode, rightNode);
}

return {
...rules,
TSEnumMember: checkForAssignmentSpace,
TSEnumMember: checkForEnumAssignmentSpace,
ClassProperty: checkForClassPropertyAssignmentSpace,
TSTypeAliasDeclaration: checkForTypeAliasAssignmentSpace,
};
},
});
94 changes: 94 additions & 0 deletions packages/eslint-plugin/tests/rules/space-infix-ops.test.ts
Expand Up @@ -33,6 +33,32 @@ ruleTester.run('space-infix-ops', rule, {
}
`,
},
{
code: `
class Test {
public readonly value?: number;
}
`,
},
{
code: `
class Test {
public readonly value = 1;
}
`,
},
{
code: `
class Test {
private value:number = 1;
}
`,
},
{
code: `
type Test = string | boolean;
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -98,5 +124,73 @@ ruleTester.run('space-infix-ops', rule, {
},
],
},
{
code: `
class Test {
public readonly value= 2;
}
`,
output: `
class Test {
public readonly value = 2;
}
`,
errors: [
{
messageId: 'missingSpace',
column: 32,
line: 3,
},
],
},
{
code: `
class Test {
public readonly value =2;
}
`,
output: `
class Test {
public readonly value = 2;
}
`,
errors: [
{
messageId: 'missingSpace',
column: 33,
line: 3,
},
],
},
{
code: `
type Test= string | number;
`,
output: `
type Test = string | number;
`,
errors: [
{
messageId: 'missingSpace',
column: 18,
line: 2,
},
],
},
{
code: `
type Test =string | number;
`,
output: `
type Test = string | number;
`,
errors: [
{
messageId: 'missingSpace',
column: 19,
line: 2,
},
],
},
],
});

0 comments on commit 5414bf2

Please sign in to comment.