From b64b5720972ff6954e5c6905bc9aeb6678b818b9 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Fri, 26 Aug 2022 13:01:11 -0700 Subject: [PATCH 01/17] Move to helper --- test/no-default-alt-text.test.js | 42 ++++++-------------------------- test/utils/run-test.js | 31 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 35 deletions(-) create mode 100644 test/utils/run-test.js diff --git a/test/no-default-alt-text.test.js b/test/no-default-alt-text.test.js index 796933e..935d3d3 100644 --- a/test/no-default-alt-text.test.js +++ b/test/no-default-alt-text.test.js @@ -1,33 +1,5 @@ -const markdownlint = require("markdownlint"); const altTextRule = require("../no-default-alt-text"); - -const thisRuleName = altTextRule.names[1]; - -const config = { - config: { - default: false, - [thisRuleName]: true, - }, - customRules: [altTextRule], -}; - -async function runTest(strings) { - return await Promise.all( - strings.map((variation) => { - const thisTestConfig = { - ...config, - strings: [variation], - }; - - return new Promise((resolve, reject) => { - markdownlint(thisTestConfig, (err, result) => { - if (err) reject(err); - resolve(result[0][0]); - }); - }); - }) - ); -} +const runTest = require("./utils/run-test").runTest; describe("GH001: No Default Alt Text", () => { describe("successes", () => { @@ -36,7 +8,7 @@ describe("GH001: No Default Alt Text", () => { "![Chart with a single root node reading 'Example'](https://user-images.githubusercontent.com/abcdef.png)", ]; - const results = await runTest(strings); + const results = await runTest(strings, altTextRule); for (const result of results) { expect(result).not.toBeDefined(); @@ -47,7 +19,7 @@ describe("GH001: No Default Alt Text", () => { 'A helpful description', ]; - const results = await runTest(strings); + const results = await runTest(strings, altTextRule); for (const result of results) { expect(result).not.toBeDefined(); @@ -63,7 +35,7 @@ describe("GH001: No Default Alt Text", () => { "![Screenshot 2022-06-26 at 7 41 30 PM](https://user-images.githubusercontent.com/abcdef.png)", ]; - const results = await runTest(strings); + const results = await runTest(strings, altTextRule); const failedRules = results .map((result) => result.ruleNames) @@ -72,7 +44,7 @@ describe("GH001: No Default Alt Text", () => { expect(failedRules).toHaveLength(4); for (const rule of failedRules) { - expect(rule).toBe(thisRuleName); + expect(rule).toBe("no-default-alt-text"); } }); @@ -84,7 +56,7 @@ describe("GH001: No Default Alt Text", () => { 'Screenshot 2022-06-26 at 7 41 30 PM', ]; - const results = await runTest(strings); + const results = await runTest(strings, altTextRule); const failedRules = results .map((result) => result.ruleNames) @@ -93,7 +65,7 @@ describe("GH001: No Default Alt Text", () => { expect(failedRules).toHaveLength(4); for (const rule of failedRules) { - expect(rule).toBe(thisRuleName); + expect(rule).toBe("no-default-alt-text"); } }); diff --git a/test/utils/run-test.js b/test/utils/run-test.js new file mode 100644 index 0000000..07b39ce --- /dev/null +++ b/test/utils/run-test.js @@ -0,0 +1,31 @@ +const markdownlint = require("markdownlint"); + +async function runTest(strings, rule) { + const thisRuleName = rule.names[1]; + + const config = { + config: { + default: false, + [thisRuleName]: true, + }, + customRules: [rule], + }; + + return await Promise.all( + strings.map((variation) => { + const thisTestConfig = { + ...config, + strings: [variation], + }; + + return new Promise((resolve, reject) => { + markdownlint(thisTestConfig, (err, result) => { + if (err) reject(err); + resolve(result[0][0]); + }); + }); + }) + ); +} + +exports.runTest = runTest; From 7f778c1a4bc4c54e495f3d0a3a235665527ceceb Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Fri, 26 Aug 2022 13:05:49 -0700 Subject: [PATCH 02/17] Add test file --- test/no-generic-link-text.test.js | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/no-generic-link-text.test.js diff --git a/test/no-generic-link-text.test.js b/test/no-generic-link-text.test.js new file mode 100644 index 0000000..887e99f --- /dev/null +++ b/test/no-generic-link-text.test.js @@ -0,0 +1,42 @@ +const noGenericLinkTextRule = require("../no-generic-link-text"); +const runTest = require("./utils/run-test").runTest; + +describe("GH002: No Generic Link Text", () => { + describe("successes", () => { + test("inline", async () => { + const strings = [ + "[GitHub](https://www.github.com)", + "[Read more about GitHub](https://www.github.com/about)", + ]; + + const results = await runTest(strings, noGenericLinkTextRule); + + for (const result of results) { + expect(result).not.toBeDefined(); + } + }); + }); + describe("failures", () => { + test("markdown example", async () => { + const strings = [ + "[Click here](www.github.com)", + "[here](www.github.com)", + "[read more](www.github.com)", + "[more](www.github.com)", + "[learn more](www.github.com)", + ]; + + const results = await runTest(strings, noGenericLinkTextRule); + + const failedRules = results + .map((result) => result.ruleNames) + .flat() + .filter((name) => !name.includes("GH")); + + expect(failedRules).toHaveLength(5); + for (const rule of failedRules) { + expect(rule).toBe("no-generic-link-text"); + } + }); + }); +}); From def43d6a3e4267819e30f2a4b5ff8c297b6b5be4 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Fri, 26 Aug 2022 13:55:15 -0700 Subject: [PATCH 03/17] Add no-generic-link-text rule --- no-generic-link-text.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 no-generic-link-text.js diff --git a/no-generic-link-text.js b/no-generic-link-text.js new file mode 100644 index 0000000..bb5bb15 --- /dev/null +++ b/no-generic-link-text.js @@ -0,0 +1,39 @@ +const bannedLinkText = [ + "read more", + "learn more", + "more", + "here", + "click here", + "link", +]; + +module.exports = { + names: ["GH002", "no-generic-link-text"], + description: + "Avoid using generic link text like `Learn more` or `Click here`", + information: new URL( + "https://primer.style/design/accessibility/links#writing-link-text" + ), + tags: ["accessibility", "links"], + function: function GH001(params, onError) { + // markdown syntax + const inlineTokens = params.tokens.filter((t) => t.type === "inline"); + for (const token of inlineTokens) { + if (token.children.some((t) => t.type === "link_open")) { + const linkTextTokens = token.children.filter((t) => t.type === "text"); + if (linkTextTokens) { + const linkText = linkTextTokens[0]; + if ( + linkText.content && + bannedLinkText.includes(linkText.content.toLowerCase().trim()) + ) { + onError({ + lineNumber: linkText.lineNumber, + details: `For link: ${linkText.content}`, + }); + } + } + } + } + }, +}; From 24071c4a952a44e61fc26ca214f65117d6352932 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Mon, 29 Aug 2022 09:13:47 -0700 Subject: [PATCH 04/17] Add tests with punctuation --- test/no-generic-link-text.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/no-generic-link-text.test.js b/test/no-generic-link-text.test.js index 887e99f..4f15dac 100644 --- a/test/no-generic-link-text.test.js +++ b/test/no-generic-link-text.test.js @@ -24,6 +24,8 @@ describe("GH002: No Generic Link Text", () => { "[read more](www.github.com)", "[more](www.github.com)", "[learn more](www.github.com)", + "[learn more.](www.github.com)", + "[click here!](www.github.com)", ]; const results = await runTest(strings, noGenericLinkTextRule); From b908be641505e0e04f5f746ec55314440cf533fa Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Mon, 29 Aug 2022 09:21:12 -0700 Subject: [PATCH 05/17] Update tests and account for blank link - We don't want anything to break when link is blank. - Tests should account for links with punctuation text. --- no-generic-link-text.js | 14 +++++++++++++- test/no-generic-link-text.test.js | 3 ++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/no-generic-link-text.js b/no-generic-link-text.js index bb5bb15..98bf16d 100644 --- a/no-generic-link-text.js +++ b/no-generic-link-text.js @@ -7,6 +7,15 @@ const bannedLinkText = [ "link", ]; +/* Downcase and strip extra whitespaces and punctuation */ +const stripAndDowncaseText = (text) => { + return text + .toLowerCase() + .replace(/[.,/#!$%^&*;:{}=\-_`~()]/g, "") + .replace(/\s{2,}/g, " ") + .trim(); +}; + module.exports = { names: ["GH002", "no-generic-link-text"], description: @@ -24,8 +33,11 @@ module.exports = { if (linkTextTokens) { const linkText = linkTextTokens[0]; if ( + linkText && linkText.content && - bannedLinkText.includes(linkText.content.toLowerCase().trim()) + bannedLinkText.includes( + stripAndDowncaseText(linkTextTokens[0].content) + ) ) { onError({ lineNumber: linkText.lineNumber, diff --git a/test/no-generic-link-text.test.js b/test/no-generic-link-text.test.js index 4f15dac..922e868 100644 --- a/test/no-generic-link-text.test.js +++ b/test/no-generic-link-text.test.js @@ -7,6 +7,7 @@ describe("GH002: No Generic Link Text", () => { const strings = [ "[GitHub](https://www.github.com)", "[Read more about GitHub](https://www.github.com/about)", + "[](www.github.com)", ]; const results = await runTest(strings, noGenericLinkTextRule); @@ -35,7 +36,7 @@ describe("GH002: No Generic Link Text", () => { .flat() .filter((name) => !name.includes("GH")); - expect(failedRules).toHaveLength(5); + expect(failedRules).toHaveLength(7); for (const rule of failedRules) { expect(rule).toBe("no-generic-link-text"); } From 550896e76cf5faee3f1e1e2727073f06b271f5db Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Tue, 20 Dec 2022 16:48:17 -0500 Subject: [PATCH 06/17] Ensure rule is configured --- index.js | 3 ++- no-generic-link-text.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 62fefba..c00a98e 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,9 @@ const _ = require("lodash"); const accessibilityRules = require("./style/accessibility.json"); const base = require("./style/base.json"); const noDefaultAltText = require("./no-default-alt-text"); +const noGenericLinkText = require("./no-generic-link-text"); -const customRules = [noDefaultAltText]; +const customRules = [noDefaultAltText, noGenericLinkText]; module.exports = [...customRules]; diff --git a/no-generic-link-text.js b/no-generic-link-text.js index 98bf16d..8e65e70 100644 --- a/no-generic-link-text.js +++ b/no-generic-link-text.js @@ -24,7 +24,7 @@ module.exports = { "https://primer.style/design/accessibility/links#writing-link-text" ), tags: ["accessibility", "links"], - function: function GH001(params, onError) { + function: function GH002(params, onError) { // markdown syntax const inlineTokens = params.tokens.filter((t) => t.type === "inline"); for (const token of inlineTokens) { From aa094d7556108fd304f698e6e2fa2feb71a7b490 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Tue, 20 Dec 2022 16:48:43 -0500 Subject: [PATCH 07/17] Add rule to accessibility --- style/accessibility.json | 1 + 1 file changed, 1 insertion(+) diff --git a/style/accessibility.json b/style/accessibility.json index 3937674..61c3082 100644 --- a/style/accessibility.json +++ b/style/accessibility.json @@ -2,6 +2,7 @@ "no-default-alt-text": true, "no-duplicate-header": true, "no-emphasis-as-header": true, + "no-generic-link-text": true, "no-space-in-links": false, "ol-prefix": "ordered", "single-h1": true, From 0f42a592727c91235f307f7aa0c06259acd29333 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Tue, 20 Dec 2022 17:06:13 -0500 Subject: [PATCH 08/17] Fix tests and run linter --- no-generic-link-text.js | 4 +--- test/usage.test.js | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/no-generic-link-text.js b/no-generic-link-text.js index 8e65e70..f7c8458 100644 --- a/no-generic-link-text.js +++ b/no-generic-link-text.js @@ -35,9 +35,7 @@ module.exports = { if ( linkText && linkText.content && - bannedLinkText.includes( - stripAndDowncaseText(linkTextTokens[0].content) - ) + bannedLinkText.includes(stripAndDowncaseText(linkText.content)) ) { onError({ lineNumber: linkText.lineNumber, diff --git a/test/usage.test.js b/test/usage.test.js index 9d923e4..0e31e8e 100644 --- a/test/usage.test.js +++ b/test/usage.test.js @@ -4,7 +4,7 @@ describe("usage", () => { describe("default export", () => { test("custom rules on default export", () => { const rules = githubMarkdownLint; - expect(rules).toHaveLength(1); + expect(rules).toHaveLength(2); expect(rules[0].names).toEqual(["GH001", "no-default-alt-text"]); }); }); @@ -17,6 +17,7 @@ describe("usage", () => { "no-space-in-links": false, "single-h1": true, "no-emphasis-as-header": true, + "no-generic-link-text": true, "ul-style": true, default: true, "no-inline-html": false, From 85b876e60d385e3ce7d1f55ee413490e74579ed0 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Tue, 20 Dec 2022 17:40:43 -0500 Subject: [PATCH 09/17] More updates --- no-generic-link-text.js | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/no-generic-link-text.js b/no-generic-link-text.js index f7c8458..2569a63 100644 --- a/no-generic-link-text.js +++ b/no-generic-link-text.js @@ -28,20 +28,32 @@ module.exports = { // markdown syntax const inlineTokens = params.tokens.filter((t) => t.type === "inline"); for (const token of inlineTokens) { - if (token.children.some((t) => t.type === "link_open")) { - const linkTextTokens = token.children.filter((t) => t.type === "text"); - if (linkTextTokens) { - const linkText = linkTextTokens[0]; - if ( - linkText && - linkText.content && - bannedLinkText.includes(stripAndDowncaseText(linkText.content)) - ) { + const { children } = token; + let inLink = false; + let linkText = ""; + let lineIndex = 0; + let { lineNumber } = token; + for (const child of children) { + const { content, markup, type } = child; + if (type === "link_open") { + inLink = true; + linkText = ""; + } else if (type === "link_close") { + inLink = false; + if (bannedLinkText.includes(stripAndDowncaseText(linkText))) { onError({ - lineNumber: linkText.lineNumber, - details: `For link: ${linkText.content}`, + lineNumber, + lineIndex, + detail: `For link: ${linkText}`, }); } + } else if (type === "softbreak" || type === "hardbreak") { + lineNumber++; + lineIndex = 0; + } else if (inLink) { + linkText += type.endsWith("_inline") + ? `${markup}${content}${markup}` + : content || markup; } } } From b63e5e23225dd5eed6916ae7ecabd1ca7a1983f7 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Tue, 20 Dec 2022 18:12:59 -0500 Subject: [PATCH 10/17] Make tests more robust --- test/no-generic-link-text.test.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/no-generic-link-text.test.js b/test/no-generic-link-text.test.js index 922e868..998da9c 100644 --- a/test/no-generic-link-text.test.js +++ b/test/no-generic-link-text.test.js @@ -8,6 +8,8 @@ describe("GH002: No Generic Link Text", () => { "[GitHub](https://www.github.com)", "[Read more about GitHub](https://www.github.com/about)", "[](www.github.com)", + "![Image](www.github.com)", + "I am not a link, and unrelated", ]; const results = await runTest(strings, noGenericLinkTextRule); @@ -22,9 +24,9 @@ describe("GH002: No Generic Link Text", () => { const strings = [ "[Click here](www.github.com)", "[here](www.github.com)", - "[read more](www.github.com)", + "Please [read more](www.github.com)", "[more](www.github.com)", - "[learn more](www.github.com)", + "You may [learn more](www.github.com) at GitHub", "[learn more.](www.github.com)", "[click here!](www.github.com)", ]; From 4ce66a8f99c3b59311139e80039d81943e6e16e6 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Wed, 21 Dec 2022 11:08:47 -0500 Subject: [PATCH 11/17] Allow additional words to be configured --- no-generic-link-text.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/no-generic-link-text.js b/no-generic-link-text.js index 2569a63..bdee555 100644 --- a/no-generic-link-text.js +++ b/no-generic-link-text.js @@ -26,6 +26,7 @@ module.exports = { tags: ["accessibility", "links"], function: function GH002(params, onError) { // markdown syntax + const allBannedLinkTexts = bannedLinkText.concat(params.config.banned_link_texts || []) const inlineTokens = params.tokens.filter((t) => t.type === "inline"); for (const token of inlineTokens) { const { children } = token; @@ -40,7 +41,7 @@ module.exports = { linkText = ""; } else if (type === "link_close") { inLink = false; - if (bannedLinkText.includes(stripAndDowncaseText(linkText))) { + if (allBannedLinkTexts.includes(stripAndDowncaseText(linkText))) { onError({ lineNumber, lineIndex, From cce4a057282b85d4c75f95d5900ac43f7d46707c Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Wed, 21 Dec 2022 11:11:15 -0500 Subject: [PATCH 12/17] Run lint --- no-generic-link-text.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/no-generic-link-text.js b/no-generic-link-text.js index bdee555..575904a 100644 --- a/no-generic-link-text.js +++ b/no-generic-link-text.js @@ -26,7 +26,9 @@ module.exports = { tags: ["accessibility", "links"], function: function GH002(params, onError) { // markdown syntax - const allBannedLinkTexts = bannedLinkText.concat(params.config.banned_link_texts || []) + const allBannedLinkTexts = bannedLinkText.concat( + params.config.banned_link_texts || [] + ); const inlineTokens = params.tokens.filter((t) => t.type === "inline"); for (const token of inlineTokens) { const { children } = token; From f5b1e486e9edc7c6279d083b7b4097f865744ef0 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Wed, 21 Dec 2022 11:25:34 -0500 Subject: [PATCH 13/17] Allow rule params to be configured --- no-generic-link-text.js | 4 ++-- test/no-generic-link-text.test.js | 27 ++++++++++++++++++++++++--- test/utils/run-test.js | 4 ++-- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/no-generic-link-text.js b/no-generic-link-text.js index 575904a..ad99318 100644 --- a/no-generic-link-text.js +++ b/no-generic-link-text.js @@ -12,7 +12,7 @@ const stripAndDowncaseText = (text) => { return text .toLowerCase() .replace(/[.,/#!$%^&*;:{}=\-_`~()]/g, "") - .replace(/\s{2,}/g, " ") + .replace(/\s+/g, " ") .trim(); }; @@ -27,7 +27,7 @@ module.exports = { function: function GH002(params, onError) { // markdown syntax const allBannedLinkTexts = bannedLinkText.concat( - params.config.banned_link_texts || [] + params.config.additional_banned_texts || [] ); const inlineTokens = params.tokens.filter((t) => t.type === "inline"); for (const token of inlineTokens) { diff --git a/test/no-generic-link-text.test.js b/test/no-generic-link-text.test.js index 998da9c..3e7ed0c 100644 --- a/test/no-generic-link-text.test.js +++ b/test/no-generic-link-text.test.js @@ -9,7 +9,11 @@ describe("GH002: No Generic Link Text", () => { "[Read more about GitHub](https://www.github.com/about)", "[](www.github.com)", "![Image](www.github.com)", - "I am not a link, and unrelated", + ` + ## Hello + I am not a link, and unrelated. + ![GitHub](some_image.png) + `, ]; const results = await runTest(strings, noGenericLinkTextRule); @@ -20,12 +24,13 @@ describe("GH002: No Generic Link Text", () => { }); }); describe("failures", () => { - test("markdown example", async () => { + test("inline", async () => { const strings = [ "[Click here](www.github.com)", "[here](www.github.com)", "Please [read more](www.github.com)", "[more](www.github.com)", + "[link](www.github.com)", "You may [learn more](www.github.com) at GitHub", "[learn more.](www.github.com)", "[click here!](www.github.com)", @@ -38,10 +43,26 @@ describe("GH002: No Generic Link Text", () => { .flat() .filter((name) => !name.includes("GH")); - expect(failedRules).toHaveLength(7); + expect(failedRules).toHaveLength(8); for (const rule of failedRules) { expect(rule).toBe("no-generic-link-text"); } }); + + test("additional words can be configured", async () => { + const results = await runTest( + ["[something](www.github.com)"], + noGenericLinkTextRule, + // eslint-disable-next-line camelcase + { additional_banned_texts: ["something"] } + ); + + const failedRules = results + .map((result) => result.ruleNames) + .flat() + .filter((name) => !name.includes("GH")); + + expect(failedRules).toHaveLength(1); + }); }); }); diff --git a/test/utils/run-test.js b/test/utils/run-test.js index 07b39ce..1fe69c0 100644 --- a/test/utils/run-test.js +++ b/test/utils/run-test.js @@ -1,12 +1,12 @@ const markdownlint = require("markdownlint"); -async function runTest(strings, rule) { +async function runTest(strings, rule, ruleConfig) { const thisRuleName = rule.names[1]; const config = { config: { default: false, - [thisRuleName]: true, + [thisRuleName]: ruleConfig || true, }, customRules: [rule], }; From 7ebcdeef013f7cb5d0016ca0ef09f0e5825302a4 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Wed, 21 Dec 2022 11:31:10 -0500 Subject: [PATCH 14/17] Add test --- test/no-generic-link-text.test.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/no-generic-link-text.test.js b/test/no-generic-link-text.test.js index 3e7ed0c..f51a1db 100644 --- a/test/no-generic-link-text.test.js +++ b/test/no-generic-link-text.test.js @@ -49,6 +49,17 @@ describe("GH002: No Generic Link Text", () => { } }); + test("error message", async () => { + const strings = ["[Click here](www.github.com)"]; + + const results = await runTest(strings, noGenericLinkTextRule); + + expect(results[0].ruleDescription).toMatch( + /Avoid using generic link text like `Learn more` or `Click here`/ + ); + expect(results[0].errorDetail).toBe("For link: Click here"); + }); + test("additional words can be configured", async () => { const results = await runTest( ["[something](www.github.com)"], From 91d4422e0e4eb254b2343ea3ab339644547941cc Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Wed, 21 Dec 2022 11:33:47 -0500 Subject: [PATCH 15/17] Rebase conflicts --- test/no-default-alt-text.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/no-default-alt-text.test.js b/test/no-default-alt-text.test.js index 935d3d3..5032250 100644 --- a/test/no-default-alt-text.test.js +++ b/test/no-default-alt-text.test.js @@ -75,7 +75,7 @@ describe("GH001: No Default Alt Text", () => { 'Screen Shot 2022-06-26 at 7 41 30 PM', ]; - const results = await runTest(strings); + const results = await runTest(strings, altTextRule); expect(results[0].ruleDescription).toMatch( /Images should not use the MacOS default screenshot filename as alternate text/ From 1bf18aee4a5c7b601d3726c7ae48b53ee624a02a Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Wed, 21 Dec 2022 11:43:09 -0500 Subject: [PATCH 16/17] Clean up code --- no-generic-link-text.js | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/no-generic-link-text.js b/no-generic-link-text.js index ad99318..4b9d327 100644 --- a/no-generic-link-text.js +++ b/no-generic-link-text.js @@ -34,10 +34,9 @@ module.exports = { const { children } = token; let inLink = false; let linkText = ""; - let lineIndex = 0; - let { lineNumber } = token; + for (const child of children) { - const { content, markup, type } = child; + const { content, type } = child; if (type === "link_open") { inLink = true; linkText = ""; @@ -45,18 +44,12 @@ module.exports = { inLink = false; if (allBannedLinkTexts.includes(stripAndDowncaseText(linkText))) { onError({ - lineNumber, - lineIndex, + lineNumber: child.lineNumber, detail: `For link: ${linkText}`, }); } - } else if (type === "softbreak" || type === "hardbreak") { - lineNumber++; - lineIndex = 0; } else if (inLink) { - linkText += type.endsWith("_inline") - ? `${markup}${content}${markup}` - : content || markup; + linkText += content; } } } From 42691334d8cdd8cb1c0f29b278aeb2d47e141716 Mon Sep 17 00:00:00 2001 From: Kate Higa Date: Wed, 21 Dec 2022 11:58:37 -0500 Subject: [PATCH 17/17] Move helper out and add tests --- helpers/strip-and-downcase-text.js | 10 ++++++++++ no-generic-link-text.js | 11 ++--------- test/helpers/strip-and-downcase-text.test.js | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 helpers/strip-and-downcase-text.js create mode 100644 test/helpers/strip-and-downcase-text.test.js diff --git a/helpers/strip-and-downcase-text.js b/helpers/strip-and-downcase-text.js new file mode 100644 index 0000000..af5c238 --- /dev/null +++ b/helpers/strip-and-downcase-text.js @@ -0,0 +1,10 @@ +/* Downcase and strip extra whitespaces and punctuation */ +function stripAndDowncaseText(text) { + return text + .toLowerCase() + .replace(/[.,/#!$%^&*;:{}=\-_`~()]/g, "") + .replace(/\s+/g, " ") + .trim(); +} + +module.exports = { stripAndDowncaseText }; diff --git a/no-generic-link-text.js b/no-generic-link-text.js index 4b9d327..c16cd67 100644 --- a/no-generic-link-text.js +++ b/no-generic-link-text.js @@ -1,3 +1,5 @@ +const { stripAndDowncaseText } = require("./helpers/strip-and-downcase-text"); + const bannedLinkText = [ "read more", "learn more", @@ -7,15 +9,6 @@ const bannedLinkText = [ "link", ]; -/* Downcase and strip extra whitespaces and punctuation */ -const stripAndDowncaseText = (text) => { - return text - .toLowerCase() - .replace(/[.,/#!$%^&*;:{}=\-_`~()]/g, "") - .replace(/\s+/g, " ") - .trim(); -}; - module.exports = { names: ["GH002", "no-generic-link-text"], description: diff --git a/test/helpers/strip-and-downcase-text.test.js b/test/helpers/strip-and-downcase-text.test.js new file mode 100644 index 0000000..2617b25 --- /dev/null +++ b/test/helpers/strip-and-downcase-text.test.js @@ -0,0 +1,20 @@ +const { + stripAndDowncaseText, +} = require("../../helpers/strip-and-downcase-text"); + +describe("stripAndDowncaseText", () => { + test("strips extra whitespace", () => { + expect(stripAndDowncaseText(" read more ")).toBe("read more"); + expect(stripAndDowncaseText(" learn ")).toBe("learn"); + }); + + test("strips punctuation", () => { + expect(stripAndDowncaseText("learn more!!!!")).toBe("learn more"); + expect(stripAndDowncaseText("I like dogs...")).toBe("i like dogs"); + }); + + test("downcases text", () => { + expect(stripAndDowncaseText("HeRe")).toBe("here"); + expect(stripAndDowncaseText("CLICK HERE")).toBe("click here"); + }); +});