From efeaf759def855a97fa8350cbac9269d55d8590b Mon Sep 17 00:00:00 2001 From: Bogdan Bogdanov Date: Mon, 25 Oct 2021 14:21:30 +0300 Subject: [PATCH] feat(eslint-plugin-clarity-adoption): add datalist fixer Signed-off-by: Bogdan Bogdanov --- .../src/rules/html-fixer-helpers.ts | 13 +++- .../src/rules/no-clr-datalist/index.ts | 11 ++++ .../no-clr-datalist/no-clr-datalist.spec.ts | 66 +++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin-clarity-adoption/src/rules/html-fixer-helpers.ts b/packages/eslint-plugin-clarity-adoption/src/rules/html-fixer-helpers.ts index b6ebec45f..97b130100 100644 --- a/packages/eslint-plugin-clarity-adoption/src/rules/html-fixer-helpers.ts +++ b/packages/eslint-plugin-clarity-adoption/src/rules/html-fixer-helpers.ts @@ -97,7 +97,13 @@ export function getAttributeNameFixes( return [fixer.replaceTextRange([start, end], newName)]; } -export function getTagFixes(fixer: RuleFixer, node: HTMLElement, oldTag: string, newTag: string): Array { +export function getTagFixes( + fixer: RuleFixer, + node: HTMLElement, + oldTag: string, + newTag: string, + newAttributes: Array = [] +): Array { const openingTag = `<${oldTag}`; const closingTag = ``; @@ -109,7 +115,10 @@ export function getTagFixes(fixer: RuleFixer, node: HTMLElement, oldTag: string, const closingTagEnd = closingTagStart + closingTag.length - 1; return [ - fixer.replaceTextRange([openingTagStart, openingTagEnd], `<${newTag}`), + fixer.replaceTextRange( + [openingTagStart, openingTagEnd], + newAttributes.length ? `<${newTag}` + ' ' + newAttributes?.join(' ') : `<${newTag}` + ), fixer.replaceTextRange([closingTagStart, closingTagEnd], ` ``); export type MessageIds = 'clrDatalistFailure'; @@ -26,9 +27,19 @@ export default createESLintRule({ create(context) { return { [`HTMLElement[tagName="${disallowedTag}"]`](node: HTMLElement): void { + const classNode = node.attributes?.find(attribute => attribute.attributeName.value === 'class'); + context.report({ node: node as any, messageId: 'clrDatalistFailure', + fix: fixer => { + const tagFixes = getTagFixes(fixer, node, 'clr-datalist-container', 'cds-datalist', [ + `control-width="shrink"`, + ]); + const attributeFixes = getDeprecatedClassFixes(fixer, classNode, [] as any); + + return [...tagFixes, ...attributeFixes]; + }, }); }, 'ClassDeclaration > Decorator'(node: TSESTree.Decorator): void { diff --git a/packages/eslint-plugin-clarity-adoption/src/rules/no-clr-datalist/no-clr-datalist.spec.ts b/packages/eslint-plugin-clarity-adoption/src/rules/no-clr-datalist/no-clr-datalist.spec.ts index 7c5b17d0b..2191c5727 100644 --- a/packages/eslint-plugin-clarity-adoption/src/rules/no-clr-datalist/no-clr-datalist.spec.ts +++ b/packages/eslint-plugin-clarity-adoption/src/rules/no-clr-datalist/no-clr-datalist.spec.ts @@ -15,6 +15,27 @@ htmlRuleTester.run('no-clr-datalist', rule, { `, + output: ` + + + + + `, + locations: [{ line: 1, column: 1 }], + }), + getInvalidDatalistTest({ + code: ` + + + + + `, + output: ` + + + + + `, locations: [{ line: 1, column: 1 }], }), getInvalidDatalistTest({ @@ -27,6 +48,15 @@ htmlRuleTester.run('no-clr-datalist', rule, { `, + output: ` +
+ + + + + + + `, locations: [{ line: 3, column: 9 }], }), getInvalidDatalistTest({ @@ -40,8 +70,33 @@ htmlRuleTester.run('no-clr-datalist', rule, { `, + output: ` +
+ + + + + + +
+ `, locations: [{ line: 3, column: 11 }], }), + getInvalidDatalistTest({ + code: ` + + + + + `, + output: ` + + + + + `, + locations: [{ line: 1, column: 1 }], + }), getInvalidDatalistTest({ code: `
@@ -54,6 +109,17 @@ htmlRuleTester.run('no-clr-datalist', rule, {
`, + output: ` +
+ + + + + + +
+ + `, locations: [ { line: 3, column: 11 }, { line: 10, column: 9 },