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

member-access: add fixer #2969

Merged
merged 5 commits into from
Aug 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
member-access: add fixer
[new-fixer] `member-access`
Fixes: #2967
  • Loading branch information
ajafff committed Jun 29, 2017
commit 924e6abee76c63c0da9ac4fc488f040ad8a512a0
31 changes: 25 additions & 6 deletions src/rules/memberAccessRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true, [true, OPTION_NO_PUBLIC], [true, OPTION_CHECK_ACCESSOR]],
type: "typescript",
typescriptOnly: true,
hasFix: true,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down Expand Up @@ -118,18 +119,36 @@ function walk(ctx: Lint.WalkContext<Options>) {
return;
}

const isPublic = Lint.hasModifier(node.modifiers, ts.SyntaxKind.PublicKeyword);
let publicKeyword: ts.Node | undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it could be separated into a tsutils function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
I added a getModifier utility to tsutils. I don't know if I release a new version this week though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if (node.modifiers !== undefined) {
for (const modifier of node.modifiers) {
if (modifier.kind === ts.SyntaxKind.PublicKeyword) {
publicKeyword = modifier;
break;
}
}
}

if (noPublic && isPublic) {
const publicKeyword = node.modifiers!.find((m) => m.kind === ts.SyntaxKind.PublicKeyword)!;
ctx.addFailureAtNode(publicKeyword, Rule.FAILURE_STRING_NO_PUBLIC);
if (noPublic && publicKeyword !== undefined) {
const start = publicKeyword.end - "public".length;
const fixEnd = Lint.isWhiteSpace(ctx.sourceFile.text.charCodeAt(publicKeyword.end)) ? publicKeyword.end + 1 : publicKeyword.end;
ctx.addFailure(
start,
publicKeyword.end,
Rule.FAILURE_STRING_NO_PUBLIC,
Lint.Replacement.deleteFromTo(start, fixEnd),
);
}
if (!noPublic && !isPublic) {
if (!noPublic && publicKeyword === undefined) {
const nameNode = node.kind === ts.SyntaxKind.Constructor
? getChildOfKind(node, ts.SyntaxKind.ConstructorKeyword, ctx.sourceFile)!
: node.name !== undefined ? node.name : node;
const memberName = node.name !== undefined && node.name.kind === ts.SyntaxKind.Identifier ? node.name.text : undefined;
ctx.addFailureAtNode(nameNode, Rule.FAILURE_STRING_FACTORY(typeToString(node), memberName));
ctx.addFailureAtNode(
nameNode,
Rule.FAILURE_STRING_FACTORY(typeToString(node), memberName),
Lint.Replacement.appendText(node.getStart(ctx.sourceFile), "public "),
);
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/rules/member-access/accessor/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Members {
public get g() {
return 1;
}
public set s(o: any) {}

public get publicG() {
return 1;
}
public set publicS(o: any) {}
}

const obj = {
get g() {
return 1;
}
};
10 changes: 10 additions & 0 deletions test/rules/member-access/constructor/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ContructorsNoAccess {
public constructor(i: number);
public constructor(o: any) {}
}

class ContructorsAccess {
public constructor(i: number);
public constructor(o: any) {}
}

48 changes: 48 additions & 0 deletions test/rules/member-access/default/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
declare class AmbientNoAccess {
public a(): number;

public static b(): number;
}

declare class AmbientAccess {
public a(): number;
public static b(): number;
}

class Members {
[x: string]: number;
public i: number;
public static j: number;

public nPublic: number;
protected nProtected: number;
private nPrivate: number;

public static nsPublic: number;
protected static nsProtected: number;
private static nsPrivate: number;

public noAccess(x: number): number;
public noAccess(o: any): any {}

public static noAccess(x: number): number;
public static noAccess(o: any): any {}

public access(x: number): number;
public access(o: any): any {}

public static access(x: number): number;
public static access(o: any): any {}
}

const obj = {
func() {}
};

function main() {
class A {
public i: number;
public static j: number;
public n: number;
}
}
7 changes: 7 additions & 0 deletions test/rules/member-access/no-public/test.ts.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class {
x() {}
constructor() {}
/*some comment*/ get y() {}
public() {}
}

3 changes: 2 additions & 1 deletion test/rules/member-access/no-public/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ class {
~~~~~~ [0]
public constructor() {}
~~~~~~ [0]
public get y() {}
public/*some comment*/ get y() {}
~~~~~~ [0]
public() {}
}

[0]: 'public' is implicit.