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

pure selectors mark all children pure #72

Closed
wants to merge 3 commits into from
Closed
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
18 changes: 11 additions & 7 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
node-version: [lts/*]
node-version: ["lts/*"]

runs-on: ${{ matrix.os }}

Expand All @@ -32,15 +32,17 @@ jobs:
cancel-in-progress: true

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
architecture: "x64"
cache: "yarn"
cache-dependency-path: "yarn.lock"

- name: Install dependencies
run: yarn --frozen-lockfile
Expand All @@ -54,7 +56,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: [10.x, 12.x, 14.x, 16.x, 18.x, 20.x]
node-version: ["10", "12", "14", "16", "18", "20"]
webpack-version: [latest]

runs-on: ${{ matrix.os }}
Expand All @@ -68,13 +70,15 @@ jobs:
if: matrix.os == 'windows-latest'
run: git config --global core.autocrlf input

- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
node-version: "${{ matrix.node-version }}"
architecture: "x64"
cache: "yarn"
cache-dependency-path: "yarn.lock"

- name: Install dependencies
run: yarn --frozen-lockfile
Expand Down
13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ module.exports = (options = {}) => {
}
});

const localRules = new Set();
root.walkRules((rule) => {
if (
rule.parent &&
Expand All @@ -605,13 +606,23 @@ module.exports = (options = {}) => {
context.options = options;
context.localAliasMap = localAliasMap;

if (pureMode && context.hasPureGlobals) {
if (
pureMode &&
context.hasPureGlobals &&
!localRules.has(rule.parent)
) {
throw rule.error(
'Selector "' +
rule.selector +
'" is not pure ' +
"(pure selectors must contain at least one local class or id)"
);
} else {
// Once a parent is pure all children are also pure
// For example the span inside this .foo is also pure
// although it is not local itself:
// .foo { span { color: red; } }
localRules.add(rule);
}

rule.selector = context.selector;
Expand Down
24 changes: 24 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,30 @@ const tests = [
options: { mode: "pure" },
error: /is not pure/,
},
{
name: "consider nonlocal children of a pure parent as pure",
input: ".foo { span { a_value: some-value; } }",
options: { mode: "pure" },
expected: ":local(.foo) { span { a_value: some-value; } }",
},
{
name: "consider nested nonlocal children of a pure parent as pure",
input: ".foo { span { a { a_value: some-value; } } }",
options: { mode: "pure" },
expected: ":local(.foo) { span { a { a_value: some-value; } } }",
},
{
name: "throw on mixed parents",
input: ".foo, html { span { a_value: some-value; } }",
options: { mode: "pure" },
error: /is not pure/,
},
{
name: "throw on global styles with a local selector",
input: `html { a_value: some-value; .foo { a_value: some-value; } }`,
options: { mode: "pure" },
error: /is not pure/,
},
{
name: "css nesting",
input: `
Expand Down
Loading