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

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobirukov committed Aug 28, 2018
1 parent c16bd79 commit fa35ff5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/rules/objectLiteralShorthandRule.ts
Expand Up @@ -84,9 +84,20 @@ export class Rule extends Lint.Rules.AbstractRule {
};
/* tslint:enable:object-literal-sort-keys */

public static LONGHAND_PROPERTY = "Expected property shorthand in object literal ";
public static LONGHAND_METHOD = "Expected method shorthand in object literal ";
public static SHORTHAND_ASSIGNMENT = "Shorthand property assignments have been disallowed.";
public static getLonghandPropertyErrorMessage(nodeText: string) {
return `Expected property shorthand in object literal ('${nodeText}').`;
}
public static getLonghandMethodErrorMessage(nodeText: string) {
return `Expected method shorthand in object literal ('${nodeText}').`;
}
public static getDisallowedShorthandErrorMessage(options: Options) {
if (options.enforceShorthandMethods && !options.enforceShorthandProperties) {
return "Shorthand property assignments have been disallowed.";
} else if (!options.enforceShorthandMethods && options.enforceShorthandProperties) {
return "Shorthand method assignments have been disallowed.";
}
return "Shorthand property and method assignments have been disallowed.";
}

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.ruleArguments));
Expand Down Expand Up @@ -129,7 +140,7 @@ function walk(ctx: Lint.WalkContext<Options>) {
) {
ctx.addFailureAtNode(
node,
`${Rule.LONGHAND_PROPERTY}('{${node.name.text}}').`,
Rule.getLonghandPropertyErrorMessage(`{${node.name.text}}`),
Lint.Replacement.deleteFromTo(node.name.end, node.end)
);
} else if (
Expand All @@ -143,13 +154,13 @@ function walk(ctx: Lint.WalkContext<Options>) {
ctx.addFailure(
node.getStart(ctx.sourceFile),
getChildOfKind(node.initializer, ts.SyntaxKind.OpenParenToken, ctx.sourceFile)!.pos,
`${Rule.LONGHAND_METHOD}('{${name}() {...}}').`,
Rule.getLonghandMethodErrorMessage(`{${name}() {...}}`),
fix
);
} else if (!enforceShorthandProperties && isShorthandPropertyAssignment(node)) {
ctx.addFailureAtNode(
node.name,
Rule.SHORTHAND_ASSIGNMENT,
Rule.getDisallowedShorthandErrorMessage(ctx.options),
Lint.Replacement.appendText(node.getStart(ctx.sourceFile), `${node.name.text}: `)
);
} else if (
Expand All @@ -159,7 +170,7 @@ function walk(ctx: Lint.WalkContext<Options>) {
) {
ctx.addFailureAtNode(
node.name,
Rule.SHORTHAND_ASSIGNMENT,
Rule.getDisallowedShorthandErrorMessage(ctx.options),
fixShorthandMethodDeclaration(node, ctx.sourceFile)
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/rules/object-literal-shorthand/never/test.ts.lint
Expand Up @@ -69,4 +69,4 @@ export class ClassA extends ClassZ {
~~~ [SHORTHAND_ASSIGNMENT]
~~~ [SHORTHAND_ASSIGNMENT]

[SHORTHAND_ASSIGNMENT]: Shorthand property assignments have been disallowed.
[SHORTHAND_ASSIGNMENT]: Shorthand property and method assignments have been disallowed.

0 comments on commit fa35ff5

Please sign in to comment.