|
| 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 | +} |
0 commit comments