Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
semicolon: add "strict-bound-class-methods" option (#3294)
Browse files Browse the repository at this point in the history
[new-rule-option] `semicolon` adds `"strict-bound-class-methods"`
Fixes: #3216
  • Loading branch information
ajafff authored and adidahiya committed Oct 20, 2017
1 parent 9dfb8a9 commit 94d1cfd
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/rules/semicolonRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ import * as Lint from "../index";
const OPTION_ALWAYS = "always";
const OPTION_NEVER = "never";
const OPTION_IGNORE_BOUND_CLASS_METHODS = "ignore-bound-class-methods";
const OPTION_STRICT_BOUND_CLASS_METHODS = "strict-bound-class-methods";
const OPTION_IGNORE_INTERFACES = "ignore-interfaces";

const enum BoundClassMethodOption {
Default,
Ignore,
Strict,
}

interface Options {
boundClassMethods: boolean;
boundClassMethods: BoundClassMethodOption;
interfaces: boolean;
}

Expand All @@ -45,7 +52,10 @@ export class Rule extends Lint.Rules.AbstractRule {
The following arguments may be optionally provided:
* \`"${OPTION_IGNORE_INTERFACES}"\` skips checking semicolons at the end of interface members.
* \`"${OPTION_IGNORE_BOUND_CLASS_METHODS}"\` skips checking semicolons at the end of bound class methods.`,
* \`"${OPTION_IGNORE_BOUND_CLASS_METHODS}"\` skips checking semicolons at the end of bound class methods.
* \`"${OPTION_STRICT_BOUND_CLASS_METHODS}"\` disables any special handling of bound class methods and treats them as any
other assignment. This option overrides \`"${OPTION_IGNORE_BOUND_CLASS_METHODS}"\`.
`,
options: {
type: "array",
items: [
Expand Down Expand Up @@ -77,7 +87,11 @@ export class Rule extends Lint.Rules.AbstractRule {

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const options: Options = {
boundClassMethods: this.ruleArguments.indexOf(OPTION_IGNORE_BOUND_CLASS_METHODS) === -1,
boundClassMethods: this.ruleArguments.indexOf(OPTION_STRICT_BOUND_CLASS_METHODS) !== -1
? BoundClassMethodOption.Strict
: this.ruleArguments.indexOf(OPTION_IGNORE_BOUND_CLASS_METHODS) !== -1
? BoundClassMethodOption.Ignore
: BoundClassMethodOption.Default,
interfaces: this.ruleArguments.indexOf(OPTION_IGNORE_INTERFACES) === -1,
};
const Walker = this.ruleArguments.indexOf(OPTION_NEVER) === -1 ? SemicolonAlwaysWalker : SemicolonNeverWalker;
Expand Down Expand Up @@ -175,10 +189,11 @@ abstract class SemicolonWalker extends Lint.AbstractWalker<Options> {

private visitPropertyDeclaration(node: ts.PropertyDeclaration) {
// check if this is a multi-line arrow function
if (node.initializer !== undefined &&
if (this.options.boundClassMethods !== BoundClassMethodOption.Strict &&
node.initializer !== undefined &&
node.initializer.kind === ts.SyntaxKind.ArrowFunction &&
!utils.isSameLine(this.sourceFile, node.getStart(this.sourceFile), node.end)) {
if (this.options.boundClassMethods) {
if (this.options.boundClassMethods === BoundClassMethodOption.Default) {
this.checkUnnecessary(node);
}
} else {
Expand Down
22 changes: 22 additions & 0 deletions test/rules/semicolon/strict-bound-class-methods/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class MyClass {
public name : string;
private index : number;
private email : string;

public initializedProperty = 6;
public initializedMethodProperty = () => {
return "hi";
};

public initializedMethodPropertyWithoutSemicolon = () => {
return "hi again";
};

public initializedMethodProperty1Line = () => { return "hi"; };

public initializedMethodPropertyWithoutSemicolon1Line = () => { return "hi again"; };
public realMethod() {}
public multilineRealMethod() {
return "foo";
}
}
27 changes: 27 additions & 0 deletions test/rules/semicolon/strict-bound-class-methods/test.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class MyClass {
public name : string
~nil [Missing semicolon]
private index : number
~nil [Missing semicolon]
private email : string;

public initializedProperty = 6
~nil [Missing semicolon]
public initializedMethodProperty = () => {
return "hi";
};

public initializedMethodPropertyWithoutSemicolon = () => {
return "hi again";
}
~nil [Missing semicolon]

public initializedMethodProperty1Line = () => { return "hi"; };

public initializedMethodPropertyWithoutSemicolon1Line = () => { return "hi again"; }
~nil [Missing semicolon]
public realMethod() {}
public multilineRealMethod() {
return "foo";
}
}
5 changes: 5 additions & 0 deletions test/rules/semicolon/strict-bound-class-methods/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"semicolon": [true, "always", "strict-bound-class-methods"]
}
}

0 comments on commit 94d1cfd

Please sign in to comment.