-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Move to helper * Add test file * Add no-generic-link-text rule * Add tests with punctuation * 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. * Ensure rule is configured * Add rule to accessibility * Fix tests and run linter * More updates * Make tests more robust * Allow additional words to be configured * Run lint * Allow rule params to be configured * Add test * Rebase conflicts * Clean up code * Move helper out and add tests
- Loading branch information
Showing
9 changed files
with
203 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const { stripAndDowncaseText } = require("./helpers/strip-and-downcase-text"); | ||
|
||
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 GH002(params, onError) { | ||
// markdown syntax | ||
const allBannedLinkTexts = bannedLinkText.concat( | ||
params.config.additional_banned_texts || [] | ||
); | ||
const inlineTokens = params.tokens.filter((t) => t.type === "inline"); | ||
for (const token of inlineTokens) { | ||
const { children } = token; | ||
let inLink = false; | ||
let linkText = ""; | ||
|
||
for (const child of children) { | ||
const { content, type } = child; | ||
if (type === "link_open") { | ||
inLink = true; | ||
linkText = ""; | ||
} else if (type === "link_close") { | ||
inLink = false; | ||
if (allBannedLinkTexts.includes(stripAndDowncaseText(linkText))) { | ||
onError({ | ||
lineNumber: child.lineNumber, | ||
detail: `For link: ${linkText}`, | ||
}); | ||
} | ||
} else if (inLink) { | ||
linkText += content; | ||
} | ||
} | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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)", | ||
"[](www.github.com)", | ||
"![Image](www.github.com)", | ||
` | ||
## Hello | ||
I am not a link, and unrelated. | ||
![GitHub](some_image.png) | ||
`, | ||
]; | ||
|
||
const results = await runTest(strings, noGenericLinkTextRule); | ||
|
||
for (const result of results) { | ||
expect(result).not.toBeDefined(); | ||
} | ||
}); | ||
}); | ||
describe("failures", () => { | ||
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)", | ||
]; | ||
|
||
const results = await runTest(strings, noGenericLinkTextRule); | ||
|
||
const failedRules = results | ||
.map((result) => result.ruleNames) | ||
.flat() | ||
.filter((name) => !name.includes("GH")); | ||
|
||
expect(failedRules).toHaveLength(8); | ||
for (const rule of failedRules) { | ||
expect(rule).toBe("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)"], | ||
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const markdownlint = require("markdownlint"); | ||
|
||
async function runTest(strings, rule, ruleConfig) { | ||
const thisRuleName = rule.names[1]; | ||
|
||
const config = { | ||
config: { | ||
default: false, | ||
[thisRuleName]: ruleConfig || 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; |