From e551a6f94df05a9ea4e65089677182961bb1d673 Mon Sep 17 00:00:00 2001 From: tanmoyopenroot Date: Tue, 25 Jun 2019 11:42:56 +0530 Subject: [PATCH 1/2] fix tslint table error and add test --- .../src/rules/blueprintHtmlComponentsRule.ts | 12 +++++++++++- .../rules/blueprint-html-components/true/all.tsx.fix | 4 ++-- .../blueprint-html-components/true/all.tsx.lint | 3 ++- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/tslint-config/src/rules/blueprintHtmlComponentsRule.ts b/packages/tslint-config/src/rules/blueprintHtmlComponentsRule.ts index b38f35e355..bf827a5e16 100644 --- a/packages/tslint-config/src/rules/blueprintHtmlComponentsRule.ts +++ b/packages/tslint-config/src/rules/blueprintHtmlComponentsRule.ts @@ -20,6 +20,7 @@ import { addImportToFile } from "./utils/addImportToFile"; import { replaceTagName } from "./utils/replaceTagName"; const PATTERN = /^(h[1-6]|code|pre|blockquote|table)$/; +const BLUEPRINT_HTMLTABLE_COMPONENT = "HTMLTable"; export class Rule extends Lint.Rules.AbstractRule { public static metadata: Lint.IRuleMetadata = { @@ -42,6 +43,10 @@ export class Rule extends Lint.Rules.AbstractRule { } } +function isTableTag(tagName: string): boolean { + return tagName === "Table"; +} + function walk(ctx: Lint.WalkContext): void { const tagFailures: Array<{ jsxTag: ts.JsxTagNameExpression; @@ -54,7 +59,12 @@ function walk(ctx: Lint.WalkContext): void { if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) { const match = PATTERN.exec(node.tagName.getFullText()); if (match != null) { - const newTagName = match[1].charAt(0).toUpperCase() + match[1].slice(1); + let newTagName = match[1].charAt(0).toUpperCase() + match[1].slice(1); + + if (isTableTag(newTagName)) { + newTagName = BLUEPRINT_HTMLTABLE_COMPONENT; + } + const replacements = [replaceTagName(node.tagName, newTagName)]; if (ts.isJsxOpeningElement(node)) { diff --git a/packages/tslint-config/test/rules/blueprint-html-components/true/all.tsx.fix b/packages/tslint-config/test/rules/blueprint-html-components/true/all.tsx.fix index e72fce679b..19d9e547f4 100644 --- a/packages/tslint-config/test/rules/blueprint-html-components/true/all.tsx.fix +++ b/packages/tslint-config/test/rules/blueprint-html-components/true/all.tsx.fix @@ -1,7 +1,7 @@ -import { Blockquote, Code, H2, H5, Pre } from "@blueprintjs/core"; +import { Blockquote, Code, H2, H5, HTMLTable, Pre } from "@blueprintjs/core";
Subtitle
block

Title

code element
- +table element diff --git a/packages/tslint-config/test/rules/blueprint-html-components/true/all.tsx.lint b/packages/tslint-config/test/rules/blueprint-html-components/true/all.tsx.lint index a9b3c32dae..4f48f78a76 100644 --- a/packages/tslint-config/test/rules/blueprint-html-components/true/all.tsx.lint +++ b/packages/tslint-config/test/rules/blueprint-html-components/true/all.tsx.lint @@ -8,5 +8,6 @@ ~~~~ [err % ("Code")]
~~~~~~~~~~ [err % ("Blockquote")] - +table element
+ ~~~~~ [err % ("HTMLTable")] [err]: use Blueprint <%s> component instead of JSX intrinsic element. From eee6e39ef6511360ea752cf0dfa958ee3759dfd6 Mon Sep 17 00:00:00 2001 From: Adi Dahiya Date: Tue, 25 Jun 2019 11:52:47 -0400 Subject: [PATCH 2/2] Code style tweaks --- .../src/rules/blueprintHtmlComponentsRule.ts | 21 +++++++++---------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/tslint-config/src/rules/blueprintHtmlComponentsRule.ts b/packages/tslint-config/src/rules/blueprintHtmlComponentsRule.ts index bf827a5e16..52a18adc6a 100644 --- a/packages/tslint-config/src/rules/blueprintHtmlComponentsRule.ts +++ b/packages/tslint-config/src/rules/blueprintHtmlComponentsRule.ts @@ -20,7 +20,6 @@ import { addImportToFile } from "./utils/addImportToFile"; import { replaceTagName } from "./utils/replaceTagName"; const PATTERN = /^(h[1-6]|code|pre|blockquote|table)$/; -const BLUEPRINT_HTMLTABLE_COMPONENT = "HTMLTable"; export class Rule extends Lint.Rules.AbstractRule { public static metadata: Lint.IRuleMetadata = { @@ -43,10 +42,6 @@ export class Rule extends Lint.Rules.AbstractRule { } } -function isTableTag(tagName: string): boolean { - return tagName === "Table"; -} - function walk(ctx: Lint.WalkContext): void { const tagFailures: Array<{ jsxTag: ts.JsxTagNameExpression; @@ -59,12 +54,7 @@ function walk(ctx: Lint.WalkContext): void { if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) { const match = PATTERN.exec(node.tagName.getFullText()); if (match != null) { - let newTagName = match[1].charAt(0).toUpperCase() + match[1].slice(1); - - if (isTableTag(newTagName)) { - newTagName = BLUEPRINT_HTMLTABLE_COMPONENT; - } - + const newTagName = getNewTagName(match[1]); const replacements = [replaceTagName(node.tagName, newTagName)]; if (ts.isJsxOpeningElement(node)) { @@ -92,3 +82,12 @@ function walk(ctx: Lint.WalkContext): void { ctx.addFailureAt(jsxTag.getFullStart(), jsxTag.getFullWidth(), Rule.getFailure(newTagName), replacements), ); } + +function getNewTagName(tagName: string) { + switch (tagName) { + case "table": + return "HTMLTable"; + default: + return tagName.charAt(0).toUpperCase() + tagName.slice(1); + } +}