Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error messages in blueprint-icon-components lint rule #2457

Merged
merged 1 commit into from
May 4, 2018
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
18 changes: 11 additions & 7 deletions packages/tslint-config/src/rules/blueprintIconComponentsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class Rule extends Lint.Rules.AbstractRule {
typescriptOnly: false,
};

public static COMPONENT_MESSAGE = "use <NamedIcon /> component for icon prop";
public static LITERAL_MESSAGE = "use IconName string literal for icon prop";
public static componentMessage = (component: string) => `Replace icon literal with component: ${component}`;
public static literalMessage = (literal: string) => `Replace icon component with literal: ${literal}`;

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const [option = OPTION_COMPONENT] = this.ruleArguments;
Expand All @@ -46,20 +46,24 @@ function walk(ctx: Lint.WalkContext<string>): void {
const { initializer } = node;
const option = ctx.options;
if (ts.isStringLiteral(initializer) && option === OPTION_COMPONENT) {
addFailure(ctx, node, Rule.COMPONENT_MESSAGE, `icon={<${pascalCase(initializer.text)}Icon />}`);
// "tick" -> <TickIcon />
const iconName = `<${pascalCase(initializer.text)}Icon />`;
addFailure(ctx, node, Rule.componentMessage(iconName), `{${iconName}}`);
} else if (ts.isJsxExpression(initializer) && option === OPTION_LITERAL) {
// <TickIcon /> -> "tick"
const match = /<(\w+)Icon /.exec(initializer.getText());
const literal = match == null ? undefined : `icon="${dashCase(match[1])}"`;
addFailure(ctx, node, Rule.LITERAL_MESSAGE, literal);
const literal = match == null ? undefined : `"${dashCase(match[1])}"`;
addFailure(ctx, node, Rule.literalMessage(literal), literal);
}
}
return ts.forEachChild(node, cb);
});
}

function addFailure(ctx: Lint.WalkContext<string>, node: ts.Declaration, message: string, replacement?: string) {
const start = node.getStart(ctx.sourceFile);
const width = node.getWidth(ctx.sourceFile);
const offsetLength = "icon=".length;
const start = node.getStart(ctx.sourceFile) + offsetLength;
const width = node.getWidth(ctx.sourceFile) - offsetLength;
const fixer = replacement == null ? undefined : new Lint.Replacement(start, width, replacement);
ctx.addFailureAt(start, width, message, fixer);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<Button icon="tick" />
~~~~~~~~~~~ [use <NamedIcon /> component for icon prop]
~~~~~~ [err % ("<TickIcon />")]

<Button icon={<TickIcon />} />
<InputGroup rightElement={<Button />} />

[err]: Replace icon literal with component: %s
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<Button icon={<TickIcon />} />
~~~~~~~~~~~~~~~~~~~ [use IconName string literal for icon prop]
~~~~~~~~~~~~~~ [err % ('"tick"')]

<Button icon="tick" />
<InputGroup rightElement={<Button />} />

[err]: Replace icon component with literal: %s
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<Button icon="tick" />
~~~~~~~~~~~ [use <NamedIcon /> component for icon prop]
~~~~~~ [err % ("<TickIcon />")]

<Button icon={<TickIcon />} />
<InputGroup rightElement={<Button />} />

[err]: Replace icon literal with component: %s