ESLint 插件包
说明
用于帮助建设ESLint插件的公用设施集.
安装
对于节点和相容运行时间:
npm install @eslint/plugin-kit
# or
yarn add @eslint/plugin-kit
# or
pnpm install @eslint/plugin-kit
# or
bun add @eslint/plugin-kit
对于德诺:
deno add @eslint/plugin-kit
使用量
该套件出口以下公用事业:
ConfigCommentParser- 用于解析ESLint配置注释(即,/* eslint-disable rule */(中文(简体) )VisitNodeStep财务报告和审定财务报表CallMethodStep- 用于帮助执行SourceCode#traverse()Directive- 用于帮助执行SourceCode#getDisableDirectives()TextSourceCodeBase- 基础课,帮助执行SourceCode接口
ConfigCommentParser
用来使用 ConfigCommentParser 类,从软件包中导入并创建新实例,例如:
import { ConfigCommentParser } from "@eslint/plugin-kit";
// create a new instance
const commentParser = new ConfigCommentParser();
// pass in a comment string without the comment delimiters
const directive = commentParser.parseDirective(
"eslint-disable prefer-const, no-var -- I don't want to use these.",
);
// will be undefined when a directive can't be parsed
if (directive) {
console.log(directive.label); // "eslint-disable"
console.log(directive.value); // "prefer-const, no-var"
console.log(directive.justification); // "I don't want to use these."
}
指令值有不同的样式,您需要分别分析才能得到正确的格式:
import { ConfigCommentParser } from "@eslint/plugin-kit";
// create a new instance
const commentParser = new ConfigCommentParser();
// list format
const list = commentParser.parseListConfig("prefer-const, no-var");
console.log(Object.entries(list)); // [["prefer-const", true], ["no-var", true]]
// string format
const strings = commentParser.parseStringConfig("foo:off, bar");
console.log(Object.entries(strings)); // [["foo", "off"], ["bar", null]]
// JSON-like config format
const jsonLike = commentParser.parseJSONLikeConfig(
"radix:[error, always], prefer-const: warn",
);
console.log(Object.entries(jsonLike.config)); // [["radix", ["error", "always"]], ["prefer-const", "warn"]]
VisitNodeStep 财务报告和审定财务报表 CallMethodStep
这个 VisitNodeStep 财务报告和审定财务报表 CallMethodStep 类代表源代码的倒转中的步骤。 他们执行正确的接口从 SourceCode#traverse() 方法.
这个 VisitNodeStep 类是两者中比较常见的,其中你描述在转弯时访问某个特定节点. 构造者接受三个论点:
target- 被访问的节点。 这用于确定规则内调用的方法. 例如,如果节点的类型是Literal然后ESLint将调用一个命名的方法Literal()(如果有的话).phase- 进站1个,出站2个.args- 将各种论据传入规则的访客方法.
例如:
import { VisitNodeStep } from "@eslint/plugin-kit";
class MySourceCode {
traverse() {
const steps = [];
for (const { node, parent, phase } of iterator(this.ast)) {
steps.push(
new VisitNodeStep({
target: node,
phase: phase === "enter" ? 1 : 2,
args: [node, parent],
}),
);
}
return steps;
}
}
这个 CallMethodStep 类不太常见,用来告诉ESLint在规则上调用特定的方法. 构造者接受两个论点:
target- 调用方法的名称,经常从"on"例如"onCodePathStart"。 。 。 .args- 要传递到方法上的一系列论据.
例如:
import { VisitNodeStep, CallMethodStep } from "@eslint/plugin-kit";
class MySourceCode {
traverse() {
const steps = [];
for (const { node, parent, phase } of iterator(this.ast)) {
steps.push(
new VisitNodeStep({
target: node,
phase: phase === "enter" ? 1 : 2,
args: [node, parent],
}),
);
// call a method indicating how many times we've been through the loop
steps.push(
new CallMethodStep({
target: "onIteration",
args: [steps.length]
});
)
}
return steps;
}
}
Directive
这个 Directive 类代表源代码中的禁用指令并执行 Directive 界面从 @eslint/core您可以告诉 ESLint 关于使用 SourceCode#getDisableDirectives() 方法,其中部分返回值为 Directive 对象。 举个例子:
import { Directive, ConfigCommentParser } from "@eslint/plugin-kit";
class MySourceCode {
getDisableDirectives() {
const directives = [];
const problems = [];
const commentParser = new ConfigCommentParser();
// read in the inline config nodes to check each one
this.getInlineConfigNodes().forEach(comment => {
// Step 1: Parse the directive
const { label, value, justification } =
commentParser.parseDirective(comment.value);
// Step 2: Extract the directive value and create the `Directive` object
switch (label) {
case "eslint-disable":
case "eslint-enable":
case "eslint-disable-next-line":
case "eslint-disable-line": {
const directiveType = label.slice("eslint-".length);
directives.push(
new Directive({
type: directiveType,
node: comment,
value,
justification,
}),
);
}
// ignore any comments that don't begin with known labels
}
});
return {
directives,
problems,
};
}
}
TextSourceCodeBase
这个 TextSourceCodeBase class 意在是一个基础类,其若干共同成员存在于 SourceCode 已执行对象。 这些成员是:
lines- 构造器调用时自动创建的一行文字.getLoc(nodeOrToken)- 得到一个节点或标志的位置。 为具有ESLint风格的节点工作loc具有Unist 风格的属性和节点position属性。如果使用不同位置格式的AST,你还需要自己执行这种方法.getLocFromIndex(index)- 将源文本索引转换为{ line: number, column: number }双人 (要使这种方法发挥作用,根节点应始终涵盖整个源代码文本,以及getLoc()方法需要正确执行。 )getIndexFromLoc(loc)- 转换一个{ line: number, column: number }组合成源文本索引。 (要使这种方法发挥作用,根节点应始终涵盖整个源代码文本,以及getLoc()方法需要正确执行。 )getRange(nodeOrToken)- 获得源文本中节点或信使的范围。 为具有 ESLint 风格的节点工作range具有Unist 风格的属性和节点position属性。如果使用不同范围格式的AST,则还需要自己执行这种方法.getText(node, beforeCount, afterCount)- 获取带有范围信息的给定节点的源文本。 若为node省略或变质(例如:undefined, (中文(简体) ),null,或空字符串),返回整个源文本。 可选的,可以在给定节点前后还原附加字符. 璶getRange()正确实施,这种方法将只是工作.getAncestors(node)- 返回节点的祖先。 要做到这一点,你必须执行getParent()自己来.
举个例子:
import { TextSourceCodeBase } from "@eslint/plugin-kit";
export class MySourceCode extends TextSourceCodeBase {
#parents = new Map();
constructor({ ast, text }) {
super({ ast, text });
}
getParent(node) {
return this.#parents.get(node);
}
traverse() {
const steps = [];
for (const { node, parent, phase } of iterator(this.ast)) {
//save the parent information
this.#parents.set(node, parent);
steps.push(
new VisitNodeStep({
target: node,
phase: phase === "enter" ? 1 : 2,
args: [node, parent],
}),
);
}
return steps;
}
}
一般来说,收集父母信息是安全的 traverse() 方法为 getParent() 财务报告和审定财务报表 getAncestors() 只能从规则中调用 一旦AST至少经过一次.
许可证
Apache 2.0 (英语)
发起人
以下公司,组织,个人支持ESLint的持续维护与发展. 成为发起人 让你的标志在我们的读取器和 网站。 。 。 .



