Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions napi/oxlint2/src-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ async function loadPluginImpl(path) {
registeredPluginPaths.add(path);

// TODO: Use a validation library to assert the shape of the plugin, and of rules
let ruleId = registeredRules.length;
const rules = [];
const ret = {
name: plugin.meta.name,
offset: registeredRules.length,
offset: ruleId,
rules,
};

for (const [ruleName, rule] of Object.entries(plugin.rules)) {
rules.push(ruleName);
registeredRules.push(rule);
registeredRules.push({ rule, context: new Context(ruleId) });
ruleId++;
}

return JSON.stringify({ Success: ret });
Expand Down Expand Up @@ -117,7 +119,9 @@ function lintFile([filePath, bufferId, buffer, ruleIds]) {
// Get visitors for this file from all rules
initCompiledVisitor();
for (const ruleId of ruleIds) {
const visitor = registeredRules[ruleId].create(new Context(ruleId, filePath));
const { rule: { create }, context } = registeredRules[ruleId];
context.physicalFilename = filePath;
const visitor = create(context);
addVisitorToCompiled(visitor);
}
const needsVisit = finalizeCompiledVisitor();
Expand Down Expand Up @@ -146,20 +150,20 @@ function lintFile([filePath, bufferId, buffer, ruleIds]) {
/**
* Context class.
*
* A `Context` is passed to each rule's `create` function.
* Each rule has its own `Context` object. It is passed to that rule's `create` function.
*/
class Context {
// Rule ID. Index into `registeredRules` array.
#ruleId;
// Absolute path of file being linted. Set before calling `rule`'s `create` method.
physicalFilename;

/**
* @constructor
* @param {number} ruleId - Rule ID
* @param {string} filePath - Absolute path of file being linted
*/
constructor(ruleId, filePath) {
constructor(ruleId) {
this.#ruleId = ruleId;
this.physicalFilename = filePath;
}

/**
Expand Down
Loading