eslint-plugin-vue
Version:
Official ESLint plugin for Vue.js
237 lines (236 loc) • 7.32 kB
JavaScript
const require_index = require("../utils/index.js");
// lib/rules/comment-directive.ts
/**
* @author Toru Nagashima <https://github.com/mysticatea>
*/
const COMMENT_DIRECTIVE_B = /^\s*(eslint-(?:en|dis)able)(?:\s+|$)/;
const COMMENT_DIRECTIVE_L = /^\s*(eslint-disable(?:-next)?-line)(?:\s+|$)/;
/**
* Remove the ignored part from a given directive comment and trim it.
*/
function stripDirectiveComment(value) {
return value.split(/\s-{2,}\s/u, 1)[0];
}
/**
* Parse a given comment.
*/
function parse(pattern, comment) {
const text = stripDirectiveComment(comment);
const match = pattern.exec(text);
if (match == null) return null;
const type = match[1];
const rules = [];
const rulesRe = /([^\s,]+)[\s,]*/g;
let startIndex = match[0].length;
rulesRe.lastIndex = startIndex;
let res;
while (res = rulesRe.exec(text)) {
const ruleId = res[1].trim();
rules.push({
ruleId,
index: startIndex
});
startIndex = rulesRe.lastIndex;
}
return {
type,
rules
};
}
/**
* Enable rules.
*/
function enable(context, loc, group, rule) {
if (rule) context.report({
loc,
messageId: group === "block" ? "enableBlockRule" : "enableLineRule",
data: { rule }
});
else context.report({
loc,
messageId: group === "block" ? "enableBlock" : "enableLine"
});
}
/**
* Disable rules.
*/
function disable(context, loc, group, rule, key) {
if (rule) context.report({
loc,
messageId: group === "block" ? "disableBlockRule" : "disableLineRule",
data: {
rule,
key
}
});
else context.report({
loc,
messageId: group === "block" ? "disableBlock" : "disableLine",
data: { key }
});
}
/**
* Process a given comment token.
* If the comment is `eslint-disable` or `eslint-enable` then it reports the comment.
*/
function processBlock(context, comment, shouldReportUnusedDisableDirectives) {
const parsed = parse(COMMENT_DIRECTIVE_B, comment.value);
if (parsed === null) return;
if (parsed.type === "eslint-disable") {
if (parsed.rules.length > 0) {
const rules = shouldReportUnusedDisableDirectives ? reportUnusedRules(context, comment, parsed.type, parsed.rules) : parsed.rules;
for (const rule of rules) disable(context, comment.loc.start, "block", rule.ruleId, rule.key || "*");
} else {
const key = shouldReportUnusedDisableDirectives ? reportUnused(context, comment, parsed.type) : "";
disable(context, comment.loc.start, "block", null, key);
}
} else if (parsed.rules.length > 0) for (const rule of parsed.rules) enable(context, comment.loc.start, "block", rule.ruleId);
else enable(context, comment.loc.start, "block", null);
}
/**
* Process a given comment token.
* If the comment is `eslint-disable-line` or `eslint-disable-next-line` then it reports the comment.
*/
function processLine(context, comment, shouldReportUnusedDisableDirectives) {
const parsed = parse(COMMENT_DIRECTIVE_L, comment.value);
if (parsed == null || comment.loc.start.line !== comment.loc.end.line) return;
const line = comment.loc.start.line + (parsed.type === "eslint-disable-line" ? 0 : 1);
const column = -1;
if (parsed.rules.length > 0) {
const rules = shouldReportUnusedDisableDirectives ? reportUnusedRules(context, comment, parsed.type, parsed.rules) : parsed.rules;
for (const rule of rules) {
disable(context, {
line,
column
}, "line", rule.ruleId, rule.key || "");
enable(context, {
line: line + 1,
column
}, "line", rule.ruleId);
}
} else {
const key = shouldReportUnusedDisableDirectives ? reportUnused(context, comment, parsed.type) : "";
disable(context, {
line,
column
}, "line", null, key);
enable(context, {
line: line + 1,
column
}, "line", null);
}
}
/**
* Reports unused disable directive.
* Do not check the use of directives here. Filter the directives used with postprocess.
*/
function reportUnused(context, comment, kind) {
const loc = comment.loc;
context.report({
loc,
messageId: "unused",
data: { kind }
});
return locToKey(loc.start);
}
/**
* Reports unused disable directive rules.
* Do not check the use of directives here. Filter the directives used with postprocess.
*/
function reportUnusedRules(context, comment, kind, rules) {
const sourceCode = context.sourceCode;
const commentStart = comment.range[0] + 4;
return rules.map((rule) => {
const start = sourceCode.getLocFromIndex(commentStart + rule.index);
const end = sourceCode.getLocFromIndex(commentStart + rule.index + rule.ruleId.length);
context.report({
loc: {
start,
end
},
messageId: "unusedRule",
data: {
rule: rule.ruleId,
kind
}
});
return {
ruleId: rule.ruleId,
key: locToKey(start)
};
});
}
/**
* Gets the key of location
*/
function locToKey(location) {
return `line:${location.line},column${location.column}`;
}
/**
* Extracts the top-level elements in document fragment.
*/
function extractTopLevelHTMLElements(documentFragment) {
return documentFragment.children.filter(require_index.default.isVElement);
}
/**
* Extracts the top-level comments in document fragment.
*/
function extractTopLevelDocumentFragmentComments(documentFragment) {
const elements = extractTopLevelHTMLElements(documentFragment);
return documentFragment.comments.filter((comment) => elements.every((element) => comment.range[1] <= element.range[0] || element.range[1] <= comment.range[0]));
}
var comment_directive_default = {
meta: {
type: "problem",
docs: {
description: "support comment-directives in `<template>`",
categories: ["base"],
url: "https://eslint.vuejs.org/rules/comment-directive.html"
},
schema: [{
type: "object",
properties: { reportUnusedDisableDirectives: { type: "boolean" } },
additionalProperties: false
}],
messages: {
disableBlock: "--block {{key}}",
enableBlock: "++block",
disableLine: "--line {{key}}",
enableLine: "++line",
disableBlockRule: "-block {{rule}} {{key}}",
enableBlockRule: "+block {{rule}}",
disableLineRule: "-line {{rule}} {{key}}",
enableLineRule: "+line {{rule}}",
clear: "clear",
unused: "Unused {{kind}} directive (no problems were reported).",
unusedRule: "Unused {{kind}} directive (no problems were reported from '{{rule}}')."
}
},
create(context) {
const shouldReportUnusedDisableDirectives = (context.options[0] || {}).reportUnusedDisableDirectives;
const sourceCode = context.sourceCode;
const documentFragment = sourceCode.parserServices.getDocumentFragment && sourceCode.parserServices.getDocumentFragment();
return { Program(node) {
if (node.templateBody) {
for (const comment of node.templateBody.comments) {
processBlock(context, comment, shouldReportUnusedDisableDirectives);
processLine(context, comment, shouldReportUnusedDisableDirectives);
}
context.report({
loc: node.templateBody.loc.end,
messageId: "clear"
});
}
if (!documentFragment) return;
for (const comment of extractTopLevelDocumentFragmentComments(documentFragment)) {
processBlock(context, comment, shouldReportUnusedDisableDirectives);
processLine(context, comment, shouldReportUnusedDisableDirectives);
}
for (const element of extractTopLevelHTMLElements(documentFragment)) context.report({
loc: element.loc.end,
messageId: "clear"
});
} };
}
};
exports.default = comment_directive_default;