Skip to content

Commit

Permalink
feat: add selector support
Browse files Browse the repository at this point in the history
Fixes eslint#60
  • Loading branch information
rviscomi committed Feb 22, 2025
1 parent a9692b0 commit 988fa1d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/rules/require-baseline.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
atRules,
mediaConditions,
types,
selectors,
} from "../data/baseline-data.js";
import { namedColors } from "../data/colors.js";

Expand Down Expand Up @@ -347,6 +348,8 @@ export default {
"Type '{{type}}' is not a {{availability}} available baseline feature.",
notBaselineMediaCondition:
"Media condition '{{condition}}' is not a {{availability}} available baseline feature.",
notBaselineSelector:
"Selector '{{selector}}' is not a {{availability}} available baseline feature.",
},
},

Expand Down Expand Up @@ -625,6 +628,48 @@ export default {
});
}
},

Selector(node) {
for (const child of node.children) {
const selector = child.name;

if (!selectors.has(selector)) {
continue;
}

const ruleLevel = selectors.get(selector);

if (ruleLevel < baselineLevel) {
const loc = child.loc;

// some selectors are prefixed with the : or :: symbols
let prefixSymbolLength = 0;
if (child.type === "PseudoClassSelector") {
prefixSymbolLength = 1;
} else if (child.type === "PseudoElementSelector") {
prefixSymbolLength = 2;
}

context.report({
loc: {
start: loc.start,
end: {
line: loc.start.line,
column:
loc.start.column +
selector.length +
prefixSymbolLength,
},
},
messageId: "notBaselineSelector",
data: {
selector,
availability,
},
});
}
}
},
};
},
};
32 changes: 32 additions & 0 deletions tests/rules/require-baseline.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,5 +340,37 @@ ruleTester.run("require-baseline", rule, {
},
],
},
{
code: "h1:has(+ h2) { margin: 0 0 0.25rem 0; }",
errors: [
{
messageId: "notBaselineSelector",
data: {
selector: "has",
availability: "widely",
},
line: 1,
column: 3,
endLine: 1,
endColumn: 7,
},
],
},
{
code: "details::details-content { background-color: #a29bfe; }",
errors: [
{
messageId: "notBaselineSelector",
data: {
selector: "details-content",
availability: "widely",
},
line: 1,
column: 8,
endLine: 1,
endColumn: 25,
},
],
},
],
});

0 comments on commit 988fa1d

Please sign in to comment.