|
| 1 | +/** |
| 2 | + * @author Toru Nagashima |
| 3 | + * @copyright 2016 Toru Nagashima. All rights reserved. |
| 4 | + * See LICENSE file in root directory for full license. |
| 5 | + */ |
| 6 | +"use strict" |
| 7 | + |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | +// Requirements |
| 10 | +//------------------------------------------------------------------------------ |
| 11 | + |
| 12 | +const DisabledArea = require("../disabled-area") |
| 13 | +const utils = require("../utils") |
| 14 | + |
| 15 | +//------------------------------------------------------------------------------ |
| 16 | +// Rule Definition |
| 17 | +//------------------------------------------------------------------------------ |
| 18 | + |
| 19 | +module.exports = { |
| 20 | + meta: { |
| 21 | + docs: { |
| 22 | + description: "disallows unused `eslint-disable` comments", |
| 23 | + category: "Best Practices", |
| 24 | + recommended: false, |
| 25 | + }, |
| 26 | + fixable: false, |
| 27 | + schema: [], |
| 28 | + }, |
| 29 | + |
| 30 | + create(context) { |
| 31 | + const originalReport = context.eslint.report |
| 32 | + const sourceCode = context.getSourceCode() |
| 33 | + const disabledArea = DisabledArea.get(sourceCode) |
| 34 | + |
| 35 | + // Override `report` method to mark disabled-area as reported. |
| 36 | + context.eslint.report = function(ruleId, _severity, node, locationArg) { |
| 37 | + const location = (typeof locationArg === "string") |
| 38 | + ? node.loc.start |
| 39 | + : locationArg.start || locationArg |
| 40 | + |
| 41 | + disabledArea.report(ruleId, location) |
| 42 | + |
| 43 | + originalReport.apply(this, arguments) |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Reports the result. |
| 48 | + * |
| 49 | + * @returns {void} |
| 50 | + */ |
| 51 | + function report() { |
| 52 | + for (const area of disabledArea.areas) { |
| 53 | + if (area.reported) { |
| 54 | + continue |
| 55 | + } |
| 56 | + |
| 57 | + context.report({ |
| 58 | + loc: utils.toRuleIdLocation(area.comment, area.ruleId), |
| 59 | + message: (area.ruleId) ? |
| 60 | + "'{{ruleId}}' rule is disabled but never reported." : |
| 61 | + "ESLint rules are disabled but never reported.", |
| 62 | + data: area, |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + // Restore |
| 67 | + context.eslint.report = originalReport |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + Program() { |
| 72 | + // Ensure that this listener is the last in `Program:exit` listeners |
| 73 | + // even if this rule was initialized before other rules. |
| 74 | + context.eslint.on("Program:exit", report) |
| 75 | + }, |
| 76 | + } |
| 77 | + }, |
| 78 | +} |
0 commit comments