Skip to content

Commit e02d52a

Browse files
authored
Add linting (#9)
* Add linting and prettier, address all issues except foreach * fix foreach lint issue * update script * fix package.json * update workflow title * regenerate package-lock * Don't download self in package.json
1 parent d5e681d commit e02d52a

File tree

9 files changed

+7807
-426
lines changed

9 files changed

+7807
-426
lines changed

.eslintrc.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = {
2+
root: true,
3+
parserOptions: {
4+
ecmaVersion: 2020,
5+
},
6+
env: {
7+
es6: true,
8+
node: true,
9+
jest: true,
10+
},
11+
plugins: ["github"],
12+
extends: ["plugin:github/recommended"],
13+
rules: {
14+
"import/no-commonjs": "off",
15+
"filenames/match-regex": "off",
16+
},
17+
};

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Lint markdown files
1+
name: Run linter and tests
22
on:
33
push:
44
branches:

index.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
const _ = require('lodash')
1+
const _ = require("lodash");
22

3-
const accessibilityRules = require('./style/accessibility.json')
4-
const base = require('./style/base.json')
5-
const noDefaultAltText = require('./no-default-alt-text')
3+
const accessibilityRules = require("./style/accessibility.json");
4+
const base = require("./style/base.json");
5+
const noDefaultAltText = require("./no-default-alt-text");
66

7-
const customRules = [
8-
noDefaultAltText
9-
]
7+
const customRules = [noDefaultAltText];
108

11-
module.exports = [...customRules]
9+
module.exports = [...customRules];
1210

13-
customRules.forEach(rule => {
14-
base[rule.names[1]] = true
15-
})
11+
for (const rule of customRules) {
12+
base[rule.names[1]] = true;
13+
}
1614

1715
module.exports.init = function init(consumerConfig) {
18-
// left overwrites right
19-
return _.defaultsDeep(consumerConfig, accessibilityRules, base)
20-
}
16+
// left overwrites right
17+
return _.defaultsDeep(consumerConfig, accessibilityRules, base);
18+
};

no-default-alt-text.js

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
1-
// Regex to match alt text that is the same as the default image filename
1+
// Regex to match alt text that is the same as the default image filename
22
// e.g. "Screen Shot 2020-10-20 at 2 52 27 PM"
33
// e.g. "Screenshot 2020-10-20 at 2 52 27 PM"
4-
const altTextRegex = /^Screen ?[S|s]hot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M$/gi
5-
const altTextTagRegex = /alt=\"Screen ?[S|s]hot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M\"/gi
4+
const altTextRegex =
5+
/^Screen ?[S|s]hot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M$/gi;
6+
const altTextTagRegex =
7+
/alt="Screen ?[S|s]hot \d{4}-\d{2}-\d{2} at \d \d{2} \d{2} [A|P]M"/gi;
68

79
module.exports = {
8-
"names": [ "GH001", "no-default-alt-text" ],
9-
"description": "Images should not use the MacOS default screenshot filename as alternate text (alt text). If you have not changed this file, try merging main with your branch. For more information see: https://primer.style/design/accessibility/alternative-text-for-images",
10-
"information": new URL("https://primer.style/design/accessibility/alternative-text-for-images"),
11-
"tags": [ "accessibility", "images" ],
12-
"function": function GH001(params, onError) {
13-
// markdown syntax
14-
params.tokens.filter(t => t.type === "inline").forEach(token => {
15-
token.children.filter(t => t.type === "image").forEach(image => {
16-
if (image.content.match(altTextRegex)) {
17-
onError({
18-
lineNumber: image.lineNumber,
19-
details: `For image: ${image.content}`
20-
})
21-
}
22-
})
23-
})
10+
names: ["GH001", "no-default-alt-text"],
11+
description:
12+
"Images should not use the MacOS default screenshot filename as alternate text (alt text). If you have not changed this file, try merging main with your branch. For more information see: https://primer.style/design/accessibility/alternative-text-for-images",
13+
information: new URL(
14+
"https://primer.style/design/accessibility/alternative-text-for-images"
15+
),
16+
tags: ["accessibility", "images"],
17+
function: function GH001(params, onError) {
18+
// markdown syntax
19+
const inlineTokens = params.tokens.filter((t) => t.type === "inline");
20+
for (const token of inlineTokens) {
21+
const imageTokens = token.children.filter((t) => t.type === "image");
22+
for (const image of imageTokens) {
23+
if (image.content.match(altTextRegex)) {
24+
onError({
25+
lineNumber: image.lineNumber,
26+
details: `For image: ${image.content}`,
27+
});
28+
}
29+
}
30+
}
2431

25-
// html syntax
26-
let lineNumber = 1
27-
params.lines.forEach(line => {
28-
if (line.match(altTextTagRegex)) {
29-
onError({
30-
lineNumber: lineNumber,
31-
details: `For line: ${line}`
32-
})
33-
}
34-
lineNumber++
35-
})
32+
// html syntax
33+
let lineNumber = 1;
34+
for (const line of params.lines) {
35+
if (line.match(altTextTagRegex)) {
36+
onError({
37+
lineNumber,
38+
details: `For image: ${line}`,
39+
});
40+
}
41+
lineNumber++;
3642
}
37-
}
43+
},
44+
};

0 commit comments

Comments
 (0)