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

feat(eslint-plugin): [space-infix-ops] support for class properties and type aliases #3231

Merged
Show file tree
Hide file tree
Changes from all commits
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
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,
},
],
},
],
});