-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathutils.js
164 lines (147 loc) · 5.06 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @author Toru Nagashima <https://github.com/mysticatea>
* See LICENSE file in root directory for full license.
*/
"use strict"
const escapeStringRegexp = require("escape-string-regexp")
const LINE_PATTERN = /[^\r\n\u2028\u2029]*(?:\r\n|[\r\n\u2028\u2029]|$)/gu
const DIRECTIVE_PATTERN = /^(eslint(?:-env|-enable|-disable(?:(?:-next)?-line)?)?|exported|globals?)(?:\s|$)/u
const LINE_COMMENT_PATTERN = /^eslint-disable-(next-)?line$/u
module.exports = {
/**
* Make the location ignoring `eslint-disable` comments.
*
* @param {object} location - The location to convert.
* @returns {object} Converted location.
*/
toForceLocation(location) {
return {
start: {
line: location.start.line,
column: -1,
},
end: location.end,
}
},
/**
* Calculate the location of the given rule in the given comment token.
*
* @param {Token} comment - The comment token to calculate.
* @param {string|null} ruleId - The rule name to calculate.
* @returns {object} The location of the given information.
*/
toRuleIdLocation(comment, ruleId) {
if (ruleId == null) {
return module.exports.toForceLocation(comment.loc)
}
const lines = comment.value.match(LINE_PATTERN)
//eslint-disable-next-line require-unicode-regexp
const ruleIdPattern = new RegExp(
`([\\s,]|^)${escapeStringRegexp(ruleId)}(?:[\\s,]|$)`
)
{
const m = ruleIdPattern.exec(lines[0])
if (m != null) {
const start = comment.loc.start
return {
start: {
line: start.line,
column: 2 + start.column + m.index + m[1].length,
},
end: {
line: start.line,
column:
2 +
start.column +
m.index +
m[1].length +
ruleId.length,
},
}
}
}
for (let i = 1; i < lines.length; ++i) {
const m = ruleIdPattern.exec(lines[i])
if (m != null) {
const start = comment.loc.start
return {
start: {
line: start.line + i,
column: m.index + m[1].length,
},
end: {
line: start.line + i,
column: m.index + m[1].length + ruleId.length,
},
}
}
}
/*istanbul ignore next : foolproof */
return comment.loc
},
/**
* Checks `a` is less than `b` or `a` equals `b`.
*
* @param {{line: number, column: number}} a - A location to compare.
* @param {{line: number, column: number}} b - Another location to compare.
* @returns {boolean} `true` if `a` is less than `b` or `a` equals `b`.
*/
lte(a, b) {
return a.line < b.line || (a.line === b.line && a.column <= b.column)
},
/**
* Parse the given comment token as a directive comment.
*
* @param {Token} comment - The comment token to parse.
* @returns {{kind: string, value: string, description: string | null}|null} The parsed data of the given comment. If `null`, it is not a directive comment.
*/
parseDirectiveComment(comment) {
const { text, description } = divideDirectiveComment(comment.value)
const match = DIRECTIVE_PATTERN.exec(text)
if (!match) {
return null
}
const directiveText = match[1]
const lineCommentSupported = LINE_COMMENT_PATTERN.test(directiveText)
if (comment.type === "Line" && !lineCommentSupported) {
return null
}
if (
lineCommentSupported &&
comment.loc.start.line !== comment.loc.end.line
) {
// disable-line comment should not span multiple lines.
return null
}
const directiveValue = text.slice(match.index + directiveText.length)
return {
kind: directiveText,
value: directiveValue.trim(),
description,
}
},
/**
* Creates an object hashmap
* @param {Object[]} values
* @param {Function} getKey
*/
keyBy(values, getKey) {
return values.reduce((accumulator, value) => {
accumulator[getKey(value)] = value
return accumulator
}, {})
},
}
/**
* Divides and trims description text and directive comments.
* @param {string} value The comment text to strip.
* @returns {{text: string, description: string | null}} The stripped text.
*/
function divideDirectiveComment(value) {
const divided = value.split(/\s-{2,}\s/u)
const text = divided[0].trim()
return {
text,
description: divided.length > 1 ? divided[1].trim() : null,
}
}