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

member-access: fix insertion position of fixer #3162

Merged
merged 1 commit into from
Aug 24, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/rules/memberAccessRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import { getChildOfKind, getModifier, getNextToken, isClassLikeDeclaration } from "tsutils";
import { getChildOfKind, getModifier, getNextToken, getTokenAtPosition, isClassLikeDeclaration } from "tsutils";
import * as ts from "typescript";

import { showWarningOnce } from "../error";
Expand Down Expand Up @@ -136,12 +136,17 @@ function walk(ctx: Lint.WalkContext<Options>) {
ctx.addFailureAtNode(
nameNode,
Rule.FAILURE_STRING_FACTORY(typeToString(node), memberName),
Lint.Replacement.appendText(node.getStart(ctx.sourceFile), "public "),
Lint.Replacement.appendText(getInsertionPosition(node, ctx.sourceFile), "public "),
);
}
}
}

function getInsertionPosition(member: ts.ClassElement, sourceFile: ts.SourceFile): number {
const node = member.decorators === undefined ? member : getTokenAtPosition(member, member.decorators.end, sourceFile)!;
return node.getStart(sourceFile);
}

function typeToString(node: ts.ClassElement): string {
switch (node.kind) {
case ts.SyntaxKind.MethodDeclaration:
Expand Down
6 changes: 6 additions & 0 deletions test/rules/member-access/default/test.ts.fix
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ function main() {
public n: number;
}
}

abstract class Decorated {
@decorator public prop;
@decorator public abstract method() {}
@decorator @moreDecorator({}) public readonly static PROP = 'FOO';
}
9 changes: 9 additions & 0 deletions test/rules/member-access/default/test.ts.lint
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ function main() {
public n: number;
}
}

abstract class Decorated {
@decorator prop;
~~~~ [The class property 'prop' must be marked either 'private', 'public', or 'protected']
@decorator abstract method() {}
~~~~~~ [The class method 'method' must be marked either 'private', 'public', or 'protected']
@decorator @moreDecorator({}) readonly static PROP = 'FOO';
~~~~ [The class property 'PROP' must be marked either 'private', 'public', or 'protected']
}