Skip to content

Commit 7977c85

Browse files
committed
Chore: refactoring
1 parent e2b6357 commit 7977c85

26 files changed

+387
-337
lines changed

index.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,7 @@
22
"use strict"
33

44
module.exports = {
5-
configs: {
6-
recommended: {
7-
plugins: ["eslint-comments"],
8-
rules: {
9-
"eslint-comments/disable-enable-pair": "error",
10-
"eslint-comments/no-aggregating-enable": "error",
11-
"eslint-comments/no-duplicate-disable": "error",
12-
"eslint-comments/no-restricted-disable": "off",
13-
"eslint-comments/no-unlimited-disable": "error",
14-
"eslint-comments/no-unused-disable": "off",
15-
"eslint-comments/no-unused-enable": "error",
16-
"eslint-comments/no-use": "off",
17-
},
18-
},
19-
},
20-
rules: {
21-
"disable-enable-pair": require("./lib/rules/disable-enable-pair"),
22-
"no-aggregating-enable": require("./lib/rules/no-aggregating-enable"),
23-
"no-duplicate-disable": require("./lib/rules/no-duplicate-disable"),
24-
"no-restricted-disable": require("./lib/rules/no-restricted-disable"),
25-
"no-unlimited-disable": require("./lib/rules/no-unlimited-disable"),
26-
"no-unused-disable": require("./lib/rules/no-unused-disable"),
27-
"no-unused-enable": require("./lib/rules/no-unused-enable"),
28-
"no-use": require("./lib/rules/no-use"),
29-
},
30-
utils: {
31-
patch: require("./lib/patch.js").patch,
32-
},
5+
configs: require("./lib/configs"),
6+
rules: require("./lib/rules"),
7+
utils: require("./lib/utils"),
338
}

lib/configs.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** DON'T EDIT THIS FILE; was created by scripts. */
2+
"use strict"
3+
4+
module.exports = {
5+
recommended: require("./configs/recommended"),
6+
}

lib/configs/recommended.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** DON'T EDIT THIS FILE; was created by scripts. */
2+
"use strict"
3+
4+
module.exports = {
5+
plugins: ["eslint-comments"],
6+
rules: {
7+
"eslint-comments/disable-enable-pair": "error",
8+
"eslint-comments/no-aggregating-enable": "error",
9+
"eslint-comments/no-duplicate-disable": "error",
10+
"eslint-comments/no-unlimited-disable": "error",
11+
"eslint-comments/no-unused-enable": "error",
12+
},
13+
}
File renamed without changes.

lib/internal/get-linters.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @author Toru Nagashima <https://github.com/mysticatea>
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
"use strict"
6+
7+
const path = require("path")
8+
const needle = `${path.sep}eslint${path.sep}`
9+
10+
module.exports = () => {
11+
const eslintPaths = new Set(
12+
Object.keys(require.cache)
13+
.filter(id => id.includes(needle))
14+
.map(id => id.slice(0, id.indexOf(needle) + needle.length))
15+
)
16+
return Array.from(eslintPaths).map(eslintPath => require(eslintPath).Linter)
17+
}

lib/internal/utils.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @author Toru Nagashima <https://github.com/mysticatea>
3+
* See LICENSE file in root directory for full license.
4+
*/
5+
"use strict"
6+
7+
const escapeStringRegexp = require("escape-string-regexp")
8+
const LINE_PATTERN = /[^\r\n\u2028\u2029]*(?:\r\n|[\r\n\u2028\u2029]|$)/g
9+
10+
module.exports = {
11+
/**
12+
* Make the location ignoring `eslint-disable` comments.
13+
*
14+
* @param {object} location - The location to convert.
15+
* @returns {object} Converted location.
16+
*/
17+
toForceLocation(location) {
18+
return {
19+
start: {
20+
line: location.start.line,
21+
column: -1,
22+
},
23+
end: location.end,
24+
}
25+
},
26+
27+
/**
28+
* Calculate the location of the given rule in the given comment token.
29+
*
30+
* @param {Token} comment - The comment token to calculate.
31+
* @param {string|null} ruleId - The rule name to calculate.
32+
* @returns {object} The location of the given information.
33+
*/
34+
toRuleIdLocation(comment, ruleId) {
35+
if (ruleId == null) {
36+
return module.exports.toForceLocation(comment.loc)
37+
}
38+
39+
const lines = comment.value.match(LINE_PATTERN)
40+
const ruleIdPattern = new RegExp(
41+
`([\\s,]|^)${escapeStringRegexp(ruleId)}(?:[\\s,]|$)`
42+
)
43+
44+
{
45+
const m = ruleIdPattern.exec(lines[0])
46+
if (m != null) {
47+
const start = comment.loc.start
48+
return {
49+
start: {
50+
line: start.line,
51+
column: 2 + start.column + m.index + m[1].length,
52+
},
53+
end: {
54+
line: start.line,
55+
column:
56+
2 +
57+
start.column +
58+
m.index +
59+
m[1].length +
60+
ruleId.length,
61+
},
62+
}
63+
}
64+
}
65+
66+
for (let i = 1; i < lines.length; ++i) {
67+
const m = ruleIdPattern.exec(lines[i])
68+
if (m != null) {
69+
const start = comment.loc.start
70+
return {
71+
start: {
72+
line: start.line + i,
73+
column: m.index + m[1].length,
74+
},
75+
end: {
76+
line: start.line + i,
77+
column: m.index + m[1].length + ruleId.length,
78+
},
79+
}
80+
}
81+
}
82+
83+
/*istanbul ignore next : foolproof */
84+
return comment.loc
85+
},
86+
87+
/**
88+
* Checks `a` is less than `b` or `a` equals `b`.
89+
*
90+
* @param {{line: number, column: number}} a - A location to compare.
91+
* @param {{line: number, column: number}} b - Another location to compare.
92+
* @returns {boolean} `true` if `a` is less than `b` or `a` equals `b`.
93+
*/
94+
lte(a, b) {
95+
return a.line < b.line || (a.line === b.line && a.column <= b.column)
96+
},
97+
}

lib/rules.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** DON'T EDIT THIS FILE; was created by scripts. */
2+
"use strict"
3+
4+
module.exports = {
5+
"disable-enable-pair": require("./rules/disable-enable-pair"),
6+
"no-aggregating-enable": require("./rules/no-aggregating-enable"),
7+
"no-duplicate-disable": require("./rules/no-duplicate-disable"),
8+
"no-restricted-disable": require("./rules/no-restricted-disable"),
9+
"no-unlimited-disable": require("./rules/no-unlimited-disable"),
10+
"no-unused-disable": require("./rules/no-unused-disable"),
11+
"no-unused-enable": require("./rules/no-unused-enable"),
12+
"no-use": require("./rules/no-use"),
13+
}

lib/rules/disable-enable-pair.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*/
55
"use strict"
66

7-
const DisabledArea = require("../disabled-area")
8-
const utils = require("../utils")
7+
const DisabledArea = require("../internal/disabled-area")
8+
const utils = require("../internal/utils")
99

1010
module.exports = {
1111
meta: {
@@ -15,7 +15,7 @@ module.exports = {
1515
category: "Best Practices",
1616
recommended: true,
1717
url:
18-
"https://github.com/mysticatea/eslint-plugin-eslint-comments/blob/v3.0.0-beta.2/docs/rules/disable-enable-pair.md",
18+
"https://mysticatea.github.io/eslint-plugin-eslint-comments/rules/disable-enable-pair.html",
1919
},
2020
fixable: null,
2121
schema: [

lib/rules/no-aggregating-enable.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*/
55
"use strict"
66

7-
const DisabledArea = require("../disabled-area")
8-
const utils = require("../utils")
7+
const DisabledArea = require("../internal/disabled-area")
8+
const utils = require("../internal/utils")
99

1010
module.exports = {
1111
meta: {
@@ -15,7 +15,7 @@ module.exports = {
1515
category: "Best Practices",
1616
recommended: true,
1717
url:
18-
"https://github.com/mysticatea/eslint-plugin-eslint-comments/blob/v3.0.0-beta.2/docs/rules/no-aggregating-enable.md",
18+
"https://mysticatea.github.io/eslint-plugin-eslint-comments/rules/no-aggregating-enable.html",
1919
},
2020
fixable: null,
2121
schema: [],

lib/rules/no-duplicate-disable.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*/
55
"use strict"
66

7-
const DisabledArea = require("../disabled-area")
8-
const utils = require("../utils")
7+
const DisabledArea = require("../internal/disabled-area")
8+
const utils = require("../internal/utils")
99

1010
module.exports = {
1111
meta: {
@@ -14,7 +14,7 @@ module.exports = {
1414
category: "Best Practices",
1515
recommended: true,
1616
url:
17-
"https://github.com/mysticatea/eslint-plugin-eslint-comments/blob/v3.0.0-beta.2/docs/rules/no-duplicate-disable.md",
17+
"https://mysticatea.github.io/eslint-plugin-eslint-comments/rules/no-duplicate-disable.html",
1818
},
1919
fixable: null,
2020
schema: [],

0 commit comments

Comments
 (0)