diff --git a/CHANGELOG.md b/CHANGELOG.md index a08eeac..7c76be5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,16 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [3.1.0](https://github.com/postcss-modules-local-by-default/compare/v3.0.0...v3.1.0) - 2023-12-21 + +### Fixes + +- scoped class attribute + +### Feature + +- pass a node to the `generateExportEntry` option + ## [3.0.0](https://github.com/postcss-modules-local-by-default/compare/v3.0.0-rc.2...v3.0.0) - 2020-10-13 ### Fixes diff --git a/package.json b/package.json index 9605f10..c8382e3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postcss-modules-scope", - "version": "3.0.0", + "version": "3.1.0", "description": "A CSS Modules transform to extract export statements from local-scope classes", "main": "src/index.js", "engines": { @@ -45,7 +45,7 @@ "husky": "^4.3.0", "jest": "^26.4.2", "lint-staged": "^10.4.0", - "postcss": "^8.1.0", + "postcss": "^8.3.0", "prettier": "^2.1.2" }, "peerDependencies": { diff --git a/src/index.js b/src/index.js index 3197752..66b78dd 100644 --- a/src/index.js +++ b/src/index.js @@ -91,17 +91,19 @@ const plugin = (options = {}) => { Once(root, { rule }) { const exports = Object.create(null); - function exportScopedName(name, rawName) { + function exportScopedName(name, rawName, node) { const scopedName = generateScopedName( rawName ? rawName : name, root.source.input.from, - root.source.input.css + root.source.input.css, + node ); const exportEntry = generateExportEntry( rawName ? rawName : name, scopedName, root.source.input.from, - root.source.input.css + root.source.input.css, + node ); const { key, value } = exportEntry; @@ -123,17 +125,29 @@ const plugin = (options = {}) => { return selectorParser.className({ value: exportScopedName( node.value, - node.raws && node.raws.value ? node.raws.value : null + node.raws && node.raws.value ? node.raws.value : null, + node ), }); case "id": { return selectorParser.id({ value: exportScopedName( node.value, - node.raws && node.raws.value ? node.raws.value : null + node.raws && node.raws.value ? node.raws.value : null, + node ), }); } + case "attribute": { + if (node.attribute === "class" && node.operator === "=") { + return selectorParser.attribute({ + attribute: node.attribute, + operator: node.operator, + quoteMark: "'", + value: exportScopedName(node.value), + }); + } + } } throw new Error( @@ -150,7 +164,7 @@ const plugin = (options = {}) => { } const selector = localizeNode(node.first, node.spaces); - // move the spaces that were around the psuedo selector to the first + // move the spaces that were around the pseudo selector to the first // non-container node selector.first.spaces = node.spaces; diff --git a/test/test-cases/error-when-attribute-is-href/expected.error.txt b/test/test-cases/error-when-attribute-is-href/expected.error.txt new file mode 100644 index 0000000..c8d1dae --- /dev/null +++ b/test/test-cases/error-when-attribute-is-href/expected.error.txt @@ -0,0 +1 @@ +attribute \("\[href\^="https"]\"\) is not allowed in a :local block diff --git a/test/test-cases/error-when-attribute-is-href/source.css b/test/test-cases/error-when-attribute-is-href/source.css new file mode 100644 index 0000000..8eceb47 --- /dev/null +++ b/test/test-cases/error-when-attribute-is-href/source.css @@ -0,0 +1,3 @@ +:local(.exportName1[href^="https"]) { + color: blue; +} diff --git a/test/test-cases/error-when-attribute-is-target/expected.error.txt b/test/test-cases/error-when-attribute-is-target/expected.error.txt new file mode 100644 index 0000000..a7c691f --- /dev/null +++ b/test/test-cases/error-when-attribute-is-target/expected.error.txt @@ -0,0 +1 @@ +attribute \("\[target\="_blank"]\"\) is not allowed in a :local block diff --git a/test/test-cases/error-when-attribute-is-target/source.css b/test/test-cases/error-when-attribute-is-target/source.css new file mode 100644 index 0000000..48d4bdd --- /dev/null +++ b/test/test-cases/error-when-attribute-is-target/source.css @@ -0,0 +1,3 @@ +:local(.exportName1[target="_blank"]) { + color: blue; +} diff --git a/test/test-cases/error-when-attribute-is-title/expected.error.txt b/test/test-cases/error-when-attribute-is-title/expected.error.txt new file mode 100644 index 0000000..a679f06 --- /dev/null +++ b/test/test-cases/error-when-attribute-is-title/expected.error.txt @@ -0,0 +1 @@ +attribute \("\[title\="flower"]\"\) is not allowed in a :local block diff --git a/test/test-cases/error-when-attribute-is-title/source.css b/test/test-cases/error-when-attribute-is-title/source.css new file mode 100644 index 0000000..c87f72b --- /dev/null +++ b/test/test-cases/error-when-attribute-is-title/source.css @@ -0,0 +1,3 @@ +:local(.exportName1[title="flower"]) { + color: blue; +} diff --git a/test/test-cases/error-when-attribute-is-type/expected.error.txt b/test/test-cases/error-when-attribute-is-type/expected.error.txt new file mode 100644 index 0000000..593f6df --- /dev/null +++ b/test/test-cases/error-when-attribute-is-type/expected.error.txt @@ -0,0 +1 @@ +attribute \("\[type\="text"]\"\) is not allowed in a :local block diff --git a/test/test-cases/error-when-attribute-is-type/source.css b/test/test-cases/error-when-attribute-is-type/source.css new file mode 100644 index 0000000..e6b3918 --- /dev/null +++ b/test/test-cases/error-when-attribute-is-type/source.css @@ -0,0 +1,3 @@ +:local(.exportName1[type="text"]) { + color: blue; +} diff --git a/test/test-cases/export-class-attribute/expected.css b/test/test-cases/export-class-attribute/expected.css new file mode 100644 index 0000000..802a6d4 --- /dev/null +++ b/test/test-cases/export-class-attribute/expected.css @@ -0,0 +1,16 @@ +._input__exportName1 { + color: red; +} + +._input__exportName2 { + color: green; +} + +._input__exportName2[class=_input__exportName1] { + color: blue; +} + +:export { + exportName1: _input__exportName1; + exportName2: _input__exportName2; +} diff --git a/test/test-cases/export-class-attribute/source.css b/test/test-cases/export-class-attribute/source.css new file mode 100644 index 0000000..3f38524 --- /dev/null +++ b/test/test-cases/export-class-attribute/source.css @@ -0,0 +1,11 @@ +:local(.exportName1) { + color: red; +} + +:local(.exportName2) { + color: green; +} + +:local(.exportName2[class="exportName1"]) { + color: blue; +} diff --git a/yarn.lock b/yarn.lock index c9b5b1f..3c7e60e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -643,9 +643,9 @@ ansi-escapes@^4.2.1, ansi-escapes@^4.3.0: type-fest "^0.11.0" ansi-regex@^4.1.0: - version "4.1.0" - resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-4.1.0.tgz#8b9f8f08cf1acb843756a839ca8c7e3168c51997" - integrity sha512-1apePfXM1UOSqw0o9IiFAovVz9M5S1Dg+4TrDwfMewQ6p/rmMueb7tWZjQ1rx4Loy1ArBggoqGpfqqdI4rondg== + version "4.1.1" + resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-4.1.1.tgz#164daac87ab2d6f6db3a29875e2d1766582dabed" + integrity sha512-ILlv4k/3f6vfQ4OoP2AGvirOktlQ98ZEL1k9FaQjxa3L1abBgbuTDAdPOpvbGncC0BTVQrl+OM8xZGK6tWXt7g== ansi-regex@^5.0.0: version "5.0.0" @@ -817,9 +817,9 @@ babel-preset-jest@^26.5.0: babel-preset-current-node-syntax "^0.1.3" balanced-match@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767" - integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c= + version "1.0.2" + resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee" + integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw== base@^0.11.1: version "0.11.2" @@ -1039,10 +1039,10 @@ color-name@~1.1.4: resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2" integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== -colorette@^1.2.1: - version "1.2.1" - resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.1.tgz#4d0b921325c14faf92633086a536db6e89564b1b" - integrity sha512-puCDz0CzydiSYOrnXpz/PKd69zRrribezjtE9yd4zvytoRc8+RY/KJPvtPFKZS3E3wP6neGyMe0vOTlHO5L3Pw== +colorette@^1.2.2: + version "1.2.2" + resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.2.tgz#cbcc79d5e99caea2dbf10eb3a26fd8b3e6acfa94" + integrity sha512-MKGMzyfeuutC/ZJ1cba9NqcNpfeqMUcYmyF1ZFY6/Cn7CNSAKx6a+s48sqLqyAiZuaP2TcqMhoo+dlwFnVxT9w== combined-stream@^1.0.6, combined-stream@~1.0.6: version "1.0.8" @@ -1069,7 +1069,7 @@ component-emitter@^1.2.1: concat-map@0.0.1: version "0.0.1" resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b" - integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s= + integrity sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg== convert-source-map@^1.4.0, convert-source-map@^1.6.0, convert-source-map@^1.7.0: version "1.7.0" @@ -1729,9 +1729,9 @@ getpass@^0.1.1: assert-plus "^1.0.0" glob-parent@^5.0.0: - version "5.1.1" - resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.1.tgz#b6c1ef417c4e5663ea498f1c45afac6916bbc229" - integrity sha512-FnI+VGOpnlGHWZxthPGR+QhR78fuiK0sNLkHQv+bL9fQi57lNNdquIbna/WrfROrolq8GK5Ek6BiMwqL/voRYQ== + version "5.1.2" + resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-5.1.2.tgz#869832c58034fe68a4093c17dc15e8340d8401c4" + integrity sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow== dependencies: is-glob "^4.0.1" @@ -2099,7 +2099,7 @@ is-wsl@^2.2.0: dependencies: is-docker "^2.0.0" -isarray@1.0.0, isarray@^1.0.0: +isarray@1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11" integrity sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE= @@ -2619,11 +2619,9 @@ json-stringify-safe@~5.0.1: integrity sha1-Epai1Y/UXxmg9s4B1lcB4sc1tus= json5@^2.1.2: - version "2.1.3" - resolved "https://registry.yarnpkg.com/json5/-/json5-2.1.3.tgz#c9b0f7fa9233bfe5807fe66fcf3a5617ed597d43" - integrity sha512-KXPvOm8K9IJKFM0bmdn8QXh7udDh1g/giieX0NLCaMnb4hEiVFqnop2ImTXCc5e0/oHz3LTqmHGtExn5hfMkOA== - dependencies: - minimist "^1.2.5" + version "2.2.3" + resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283" + integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg== jsprim@^1.2.2: version "1.4.1" @@ -2690,14 +2688,6 @@ levn@~0.3.0: prelude-ls "~1.1.2" type-check "~0.3.2" -line-column@^1.0.2: - version "1.0.2" - resolved "https://registry.yarnpkg.com/line-column/-/line-column-1.0.2.tgz#d25af2936b6f4849172b312e4792d1d987bc34a2" - integrity sha1-0lryk2tvSEkXKzEuR5LR2Ye8NKI= - dependencies: - isarray "^1.0.0" - isobject "^2.0.0" - lines-and-columns@^1.1.6: version "1.1.6" resolved "https://registry.yarnpkg.com/lines-and-columns/-/lines-and-columns-1.1.6.tgz#1c00c743b433cd0a4e80758f7b64a57440d9ff00" @@ -2853,16 +2843,16 @@ mimic-fn@^2.1.0: integrity sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg== minimatch@^3.0.4: - version "3.0.4" - resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083" - integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA== + version "3.1.2" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b" + integrity sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw== dependencies: brace-expansion "^1.1.7" minimist@^1.1.1, minimist@^1.2.0, minimist@^1.2.5: - version "1.2.5" - resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602" - integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw== + version "1.2.7" + resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.7.tgz#daa1c4d91f507390437c6a8bc01078e7000c4d18" + integrity sha512-bzfL1YUZsP41gmu/qjrEk0Q6i2ix/cVeAhbCbqH9u3zYutS1cLg00qhrD0M2MVdCcx4Sc0UpP2eBWo9rotpq6g== mixin-deep@^1.2.0: version "1.3.2" @@ -2889,10 +2879,10 @@ ms@2.1.2: resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w== -nanoid@^3.1.12: - version "3.1.12" - resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.12.tgz#6f7736c62e8d39421601e4a0c77623a97ea69654" - integrity sha512-1qstj9z5+x491jfiC4Nelk+f8XBad7LN20PmyWINJEMRSf3wcAjAWysw1qaA8z6NSKe2sjq1hRSDpBH5paCb6A== +nanoid@^3.1.23: + version "3.3.4" + resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.4.tgz#730b67e3cd09e2deacf03c027c81c9d9dbc5e8ab" + integrity sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw== nanomatch@^1.2.9: version "1.2.13" @@ -3194,15 +3184,14 @@ postcss-selector-parser@^6.0.4: uniq "^1.0.1" util-deprecate "^1.0.2" -postcss@^8.1.0: - version "8.1.1" - resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.1.1.tgz#c3a287dd10e4f6c84cb3791052b96a5d859c9389" - integrity sha512-9DGLSsjooH3kSNjTZUOt2eIj2ZTW0VI2PZ/3My+8TC7KIbH2OKwUlISfDsf63EP4aiRUt3XkEWMWvyJHvJelEg== +postcss@^8.3.0: + version "8.3.0" + resolved "https://registry.yarnpkg.com/postcss/-/postcss-8.3.0.tgz#b1a713f6172ca427e3f05ef1303de8b65683325f" + integrity sha512-+ogXpdAjWGa+fdYY5BQ96V/6tAo+TdSSIMP5huJBIygdWwKtVoB5JWZ7yUd4xZ8r+8Kvvx4nyg/PQ071H4UtcQ== dependencies: - colorette "^1.2.1" - line-column "^1.0.2" - nanoid "^3.1.12" - source-map "^0.6.1" + colorette "^1.2.2" + nanoid "^3.1.23" + source-map-js "^0.6.2" prelude-ls@^1.2.1: version "1.2.1" @@ -3622,6 +3611,11 @@ snapdragon@^0.8.1: source-map-resolve "^0.5.0" use "^3.1.0" +source-map-js@^0.6.2: + version "0.6.2" + resolved "https://registry.yarnpkg.com/source-map-js/-/source-map-js-0.6.2.tgz#0bb5de631b41cfbda6cfba8bd05a80efdfd2385e" + integrity sha512-/3GptzWzu0+0MBQFrDKzw/DvvMTUORvgY6k6jd/VS6iCR4RDTKWH6v6WPwQoUO8667uQEf9Oe38DxAYWY5F/Ug== + source-map-resolve@^0.5.0: version "0.5.3" resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.3.tgz#190866bece7553e1f8f267a2ee82c606b5509a1a" @@ -4200,9 +4194,9 @@ write@1.0.3: mkdirp "^0.5.1" ws@^7.2.3: - version "7.3.1" - resolved "https://registry.yarnpkg.com/ws/-/ws-7.3.1.tgz#d0547bf67f7ce4f12a72dfe31262c68d7dc551c8" - integrity sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA== + version "7.4.6" + resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.6.tgz#5654ca8ecdeee47c33a9a4bf6d28e2be2980377c" + integrity sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A== xml-name-validator@^3.0.0: version "3.0.0"