Skip to content

Commit

Permalink
feat(tslint): change angular member ordering (#21)
Browse files Browse the repository at this point in the history
* change angular-member-ordering rule due to new codestyle

* ordering static method

* add setters and getters ordering and space instead tab in config
  • Loading branch information
vladimirpotekhin authored and MarsiBarsi committed Nov 14, 2019
1 parent 74b0fec commit b0d7112
Show file tree
Hide file tree
Showing 9 changed files with 3,056 additions and 596 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ root = true

[*]
charset = utf-8
indent_style = tab
indent_style = space
indent_size = 4
end_of_line = lf
insert_final_newline = true
Expand Down
117 changes: 84 additions & 33 deletions src/tinkoffAngularMemberOrderingRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,49 @@ export class Rule extends Rules.AbstractRule {
optionsDescription: '',
options: {
order: [
'public-static',
'private-instance-field',
'private-static-field',
'protected-static-field',
'protected-instance-field',
'public-instance-field',
'public-static-field',
'@Input',
'@Output',
'public-instance',
'protected-static',
'protected-instance',
'private-static',
'private-instance',
'public-getter',
'public-setter',
'protected-getter',
'protected-setter',
'private-getter',
'private-setter',
'public-instance-method',
'public-static-method',
'protected-instance-method',
'protected-static-method',
'private-instance-method',
'private-static-method',
],
},
optionExamples: [
'public-static',
'protected-static',
'private-static',
'private-instance-field',
'private-static-field',
'protected-static-field',
'protected-instance-field',
'public-instance-field',
'public-static-field',
'@Input',
'@Output',
'public-instance',
'protected-instance',
'private-instance',
'public-getter',
'public-setter',
'protected-getter',
'protected-setter',
'private-getter',
'private-setter',
'public-instance-method',
'public-static-method',
'protected-instance-method',
'protected-static-method',
'private-instance-method',
'private-static-method',
],
type: 'style',
typescriptOnly: false,
Expand Down Expand Up @@ -62,7 +86,8 @@ export class Rule extends Rules.AbstractRule {
type NodeDeclaration =
| ts.PropertyDeclaration
| ts.SetAccessorDeclaration
| ts.GetAccessorDeclaration;
| ts.GetAccessorDeclaration
| ts.MethodDeclaration;

class TinkoffAngularMemberOrderingWalker extends AngularMemberOrderingWalker {
private bindingWasAppeared = false;
Expand All @@ -87,8 +112,11 @@ class TinkoffAngularMemberOrderingWalker extends AngularMemberOrderingWalker {
protected hasMatch(node: ts.Node): boolean {
return (
node.kind === ts.SyntaxKind.PropertyDeclaration ||
node.kind === ts.SyntaxKind.MethodDeclaration ||
this.isInputAccessor(node) ||
this.isOutputAccessor(node)
this.isOutputAccessor(node) ||
this.isGetAccessor(node) ||
this.isSetAccessor(node)
);
}

Expand Down Expand Up @@ -126,6 +154,21 @@ class TinkoffAngularMemberOrderingWalker extends AngularMemberOrderingWalker {
);
}

protected nodeWidth(node: NodeDeclaration): number {
const input = '@Input';
const output = '@Output';

if (this.isInput(node)) {
return input.length;
}

if (this.isOutput(node)) {
return output.length;
}

return node.getChildAt(0).getWidth();
}

private isInputAfterAccessor(
node: NodeDeclaration,
prevNode: NodeDeclaration,
Expand All @@ -148,21 +191,6 @@ class TinkoffAngularMemberOrderingWalker extends AngularMemberOrderingWalker {
return isNodeOutput && !isNodeAccessor && isPrevNodeNodeOutputAccessor;
}

protected nodeWidth(node: NodeDeclaration): number {
const input = '@Input';
const output = '@Output';

if (this.isInput(node)) {
return input.length;
}

if (this.isOutput(node)) {
return output.length;
}

return node.getChildAt(0).getWidth();
}

private getNodeType(node: NodeDeclaration): string {
return this.getNodeInfo<string>(node, 'text');
}
Expand Down Expand Up @@ -190,12 +218,30 @@ class TinkoffAngularMemberOrderingWalker extends AngularMemberOrderingWalker {
nodeName = 'public';
}

if (this.isGetAccessor(node)) {
nodeName += '-getter';

return Rule.memberData[nodeName][field];
}

if (this.isSetAccessor(node)) {
nodeName += '-setter';

return Rule.memberData[nodeName][field];
}

if (this.isIncludedModifier(node, ts.SyntaxKind.StaticKeyword)) {
nodeName += '-static';
} else {
nodeName += '-instance';
}

if (node.kind === ts.SyntaxKind.MethodDeclaration) {
nodeName += '-method';
} else {
nodeName += '-field';
}

return Rule.memberData[nodeName][field];
}

Expand Down Expand Up @@ -240,9 +286,14 @@ class TinkoffAngularMemberOrderingWalker extends AngularMemberOrderingWalker {
}

private isAccessor(node: ts.Node | NodeDeclaration): boolean {
return (
node.kind === ts.SyntaxKind.SetAccessor ||
node.kind === ts.SyntaxKind.GetAccessor
);
return this.isGetAccessor(node) || this.isSetAccessor(node);
}

private isGetAccessor(node: ts.Node): boolean {
return node.kind === ts.SyntaxKind.GetAccessor;
}

private isSetAccessor(node: ts.Node): boolean {
return node.kind === ts.SyntaxKind.SetAccessor;
}
}
14 changes: 7 additions & 7 deletions src/tinkoffConditionBreaksRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ class ConditionalBreaksWalker extends Lint.RuleWalker {
return node.getChildren().some(ConditionalBreaksWalker.checkChildren);
}

private static checkChildren(node: ts.Node): boolean {
return (
node.kind === ts.SyntaxKind.ConditionalExpression ||
node.getChildren().some(ConditionalBreaksWalker.checkChildren)
);
}

private getLineNumber(position: number): number {
return ts.getLineAndCharacterOfPosition(this.getSourceFile(), position).line;
}
Expand All @@ -107,6 +100,13 @@ class ConditionalBreaksWalker extends Lint.RuleWalker {
);
}

private static checkChildren(node: ts.Node): boolean {
return (
node.kind === ts.SyntaxKind.ConditionalExpression ||
node.getChildren().some(ConditionalBreaksWalker.checkChildren)
);
}

private static getFixes(node: ts.ConditionalExpression): Replacement[] {
const lineAndChar = ts.getLineAndCharacterOfPosition(
node.getSourceFile(),
Expand Down
70 changes: 59 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,38 +164,86 @@ export abstract class NewLineRuleWalker extends AbstractWalker<void> {
// Angular member ordering rule

export const defaultMemberData = {
'public-static': {
'public-static-field': {
rank: 0,
text: 'PUBLIC STATIC property',
},
'protected-static': {
'protected-static-field': {
rank: 1,
text: 'PROTECTED STATIC property',
},
'private-static': {
'private-static-field': {
rank: 2,
text: 'PRIVATE STATIC property',
},
'@Input': {
'public-static-method': {
rank: 3,
text: 'PUBLIC STATIC METHOD',
},
'protected-static-method': {
rank: 4,
text: 'PROTECTED STATIC METHOD',
},
'private-static-method': {
rank: 5,
text: 'PRIVATE STATIC METHOD',
},
'@Input': {
rank: 6,
text: '@Input',
},
'@Output': {
rank: 4,
rank: 7,
text: '@Output',
},
'public-instance': {
rank: 5,
'public-getter': {
rank: 8,
text: 'PUBLIC GETTER',
},
'public-setter': {
rank: 9,
text: 'PUBLIC SETTER',
},
'protected-getter': {
rank: 10,
text: 'PROTECTED GETTER',
},
'protected-setter': {
rank: 11,
text: 'PROTECTED SETTER',
},
'private-getter': {
rank: 12,
text: 'PRIVATE GETTER',
},
'private-setter': {
rank: 13,
text: 'PRIVATE SETTER',
},
'public-instance-field': {
rank: 14,
text: 'PUBLIC INSTANCE property',
},
'protected-instance': {
rank: 6,
'protected-instance-field': {
rank: 15,
text: 'PROTECTED INSTANCE property',
},
'private-instance': {
rank: 7,
'private-instance-field': {
rank: 16,
text: 'PRIVATE INSTANCE property',
},
'public-instance-method': {
rank: 17,
text: 'PUBLIC INSTANCE METHOD',
},
'protected-instance-method': {
rank: 18,
text: 'PROTECTED INSTANCE METHOD',
},
'private-instance-method': {
rank: 19,
text: 'PRIVATE INSTANCE METHOD',
},
};

type NodeDeclaration =
Expand Down
Loading

0 comments on commit b0d7112

Please sign in to comment.