From 91a3d11786e68002346791d8fd19247a2214c2ef Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Tue, 9 Jul 2024 13:52:19 +0200 Subject: [PATCH 1/3] pure selectors mark all children pure --- src/index.js | 13 ++++++++++++- test/index.test.js | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index eeaef93..3ead814 100644 --- a/src/index.js +++ b/src/index.js @@ -590,6 +590,7 @@ module.exports = (options = {}) => { } }); + const localRules = new Set(); root.walkRules((rule) => { if ( rule.parent && @@ -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; diff --git a/test/index.test.js b/test/index.test.js index 99285fc..430f913 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -868,6 +868,24 @@ 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: "css nesting", input: ` From 55d9b43efbfeb037c14f6db0d4ae227642a56489 Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Tue, 9 Jul 2024 20:37:12 +0200 Subject: [PATCH 2/3] try to fix ci --- .github/workflows/nodejs.yml | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index c0358f4..b1fade3 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -23,7 +23,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - node-version: [lts/*] + node-version: ["lts/*"] runs-on: ${{ matrix.os }} @@ -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 @@ -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 }} @@ -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 From 6cea2d6e5df2d388084bc9fdd6a71dadbe0967e6 Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Tue, 6 Aug 2024 17:12:51 +0200 Subject: [PATCH 3/3] add test for html { a_value: some-value; .foo { a_value: some-value; } } --- test/index.test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/index.test.js b/test/index.test.js index 430f913..5f2eb36 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -886,6 +886,12 @@ const tests = [ 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: `