Skip to content

Commit 7667d63

Browse files
cjc343ajafff
authored andcommitted
Add 'trailing' mode to parameter-properties (#28)
1 parent ea86b6b commit 7667d63

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

docs/parameter-properties.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Usage:
1212
true,
1313
"all-or-none", // forces all or none of a constructors parameters to be parameter properties
1414
"leading", // forces parameter properties to precede regular parameters
15+
"trailing", // forces regular parameters to precede parameter properties
1516
"member-access", // forces an access modifier for every parameter property
1617
"readonly" // forces all parameter properties to be readonly
1718
]

rules/parameterPropertiesRule.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { AbstractConfigDependentRule } from '../src/rules';
66

77
const ALL_OR_NONE_OPTION = 'all-or-none';
88
const LEADING_OPTION = 'leading';
9+
const TRAILING_OPTION = 'trailing';
910
const READONLY_OPTION = 'readonly';
1011
const MEMBER_ACCESS_OPTION = 'member-access';
1112

1213
const ALL_OR_NONE_FAIL = 'don\'t mix parameter properties with regular parameters';
1314
const LEADING_FAIL = 'parameter properties must precede regular parameters';
15+
const TRAILING_FAIL = 'regular parameters must precede parameter properties';
1416
const READONLY_FAIL = 'parameter property must be readonly';
1517
const MEMBER_ACCESS_FAIL = 'parameter property must have access modifier';
1618

@@ -26,6 +28,7 @@ const MEMBER_ACCESS_FAIL = 'parameter property must have access modifier';
2628
interface IOptions {
2729
allOrNone: boolean;
2830
leading: boolean;
31+
trailing: boolean;
2932
readOnly: boolean;
3033
memberAccess: boolean;
3134
}
@@ -35,6 +38,7 @@ export class Rule extends AbstractConfigDependentRule {
3538
return this.applyWithWalker(new ParameterPropertyWalker(sourceFile, this.ruleName, {
3639
allOrNone: this.ruleArguments.indexOf(ALL_OR_NONE_OPTION) !== -1,
3740
leading: this.ruleArguments.indexOf(LEADING_OPTION) !== -1,
41+
trailing: this.ruleArguments.indexOf(TRAILING_OPTION) !== -1,
3842
readOnly: this.ruleArguments.indexOf(READONLY_OPTION) !== -1,
3943
memberAccess: this.ruleArguments.indexOf(MEMBER_ACCESS_OPTION) !== -1,
4044
}));
@@ -90,6 +94,10 @@ class ParameterPropertyWalker extends Lint.AbstractWalker<IOptions> {
9094
regular = true;
9195
}
9296
}
97+
} else if (this.options.trailing) {
98+
for (let i = index; i < length; ++i)
99+
if (!utils.isParameterProperty(parameters[i]))
100+
this.addFailureAtNode(parameters[i], TRAILING_FAIL);
93101
}
94102

95103
if (this.options.memberAccess) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Foo {
2+
constructor(foo: string, public bar: number, readonly baz?) {}
3+
}
4+
5+
class Bar {
6+
constructor() {}
7+
}
8+
9+
class Baz {
10+
constructor(bas) {}
11+
}
12+
13+
class Bas {
14+
constructor(readonly baz) {}
15+
}
16+
17+
class Quux {
18+
constructor(protected foo, bar) {}
19+
~~~ [fail]
20+
}
21+
22+
class Ajaff {
23+
constructor(private foo, bar, public baz) {}
24+
~~~ [fail]
25+
}
26+
27+
[fail]: regular parameters must precede parameter properties
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"rulesDirectory": "../../../../rules",
3+
"rules": {
4+
"parameter-properties": [
5+
true,
6+
"trailing"
7+
]
8+
}
9+
}

0 commit comments

Comments
 (0)