Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(unstable): lint plugin fix :has(), :is/where/matches and :not() selectors #28348

Merged
merged 3 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
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
84 changes: 77 additions & 7 deletions cli/js/40_lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const PropFlags = {
/** @typedef {import("./40_lint_types.d.ts").LintState} LintState */
/** @typedef {import("./40_lint_types.d.ts").TransformFn} TransformFn */
/** @typedef {import("./40_lint_types.d.ts").MatchContext} MatchContext */
/** @typedef {import("./40_lint_types.d.ts").MatcherFn} MatcherFn */

/** @type {LintState} */
const state = {
Expand Down Expand Up @@ -770,18 +771,23 @@ function getString(strTable, id) {

/** @implements {MatchContext} */
class MatchCtx {
parentLimitIdx = 0;

/**
* @param {AstContext} ctx
* @param {CancellationToken} cancellationToken
*/
constructor(ctx) {
constructor(ctx, cancellationToken) {
this.ctx = ctx;
this.cancellationToken = cancellationToken;
}

/**
* @param {number} idx
* @returns {number}
*/
getParent(idx) {
if (idx === this.parentLimitIdx) return AST_IDX_INVALID;
const parent = readParent(this.ctx.buf, idx);

const parentType = readType(this.ctx.buf, parent);
Expand Down Expand Up @@ -953,13 +959,31 @@ class MatchCtx {

return out;
}

/**
* Used for `:has()` and `:not()`
* @param {MatcherFn[]} selectors
* @param {number} idx
* @returns {boolean}
*/
subSelect(selectors, idx) {
const prevLimit = this.parentLimitIdx;
this.parentLimitIdx = idx;

try {
return subTraverse(this.ctx, selectors, idx, idx, this.cancellationToken);
} finally {
this.parentLimitIdx = prevLimit;
}
}
}

/**
* @param {Uint8Array} buf
* @param {CancellationToken} token
* @returns {AstContext}
*/
function createAstContext(buf) {
function createAstContext(buf, token) {
/** @type {Map<number, string>} */
const strTable = new Map();

Expand Down Expand Up @@ -1039,7 +1063,7 @@ function createAstContext(buf) {
propByStr,
matcher: /** @type {*} */ (null),
};
ctx.matcher = new MatchCtx(ctx);
ctx.matcher = new MatchCtx(ctx, token);

setNodeGetters(ctx);

Expand All @@ -1060,7 +1084,8 @@ const NOOP = (_node) => {};
* @param {Uint8Array} serializedAst
*/
export function runPluginsForFile(fileName, serializedAst) {
const ctx = createAstContext(serializedAst);
const token = new CancellationToken();
const ctx = createAstContext(serializedAst, token);

/** @type {Map<string, CompiledVisitor["info"]>}>} */
const bySelector = new Map();
Expand Down Expand Up @@ -1169,7 +1194,6 @@ export function runPluginsForFile(fileName, serializedAst) {
visitors.push({ info, matcher });
}

const token = new CancellationToken();
// Traverse ast with all visitors at the same time to avoid traversing
// multiple times.
try {
Expand All @@ -1191,11 +1215,12 @@ export function runPluginsForFile(fileName, serializedAst) {
* @param {CancellationToken} cancellationToken
*/
function traverse(ctx, visitors, idx, cancellationToken) {
const { buf } = ctx;

while (idx !== AST_IDX_INVALID) {
if (cancellationToken.isCancellationRequested()) return;

const { buf } = ctx;
const nodeType = readType(ctx.buf, idx);
const nodeType = readType(buf, idx);

/** @type {VisitorFn[] | null} */
let exits = null;
Expand Down Expand Up @@ -1240,6 +1265,51 @@ function traverse(ctx, visitors, idx, cancellationToken) {
}
}

/**
* Used for subqueries in `:has()` and `:not()`
* @param {AstContext} ctx
* @param {MatcherFn[]} selectors
* @param {number} rootIdx
* @param {number} idx
* @param {CancellationToken} cancellationToken
* @returns {boolean}
*/
function subTraverse(ctx, selectors, rootIdx, idx, cancellationToken) {
const { buf } = ctx;

while (idx > AST_IDX_INVALID) {
if (cancellationToken.isCancellationRequested()) return false;

const nodeType = readType(buf, idx);

if (nodeType !== AST_GROUP_TYPE) {
for (let i = 0; i < selectors.length; i++) {
const sel = selectors[i];

if (sel(ctx.matcher, idx)) {
return true;
}
}
}

const childIdx = readChild(buf, idx);
if (
childIdx > AST_IDX_INVALID &&
subTraverse(ctx, selectors, rootIdx, childIdx, cancellationToken)
) {
return true;
}

if (idx === rootIdx) {
break;
}

idx = readNext(buf, idx);
}

return false;
}

/**
* This is useful debugging helper to display the buffer's contents.
* @param {AstContext} ctx
Expand Down
Loading