From a48b8c6c393997b18591545c4159e21bd5f2950d Mon Sep 17 00:00:00 2001 From: Romain Menke Date: Sun, 3 Mar 2024 11:32:20 +0100 Subject: [PATCH] v2.1.2 --- .tape.js | 24 +++++++++++++++++ CHANGELOG.md | 5 ++++ index.js | 76 ++++++++++++++++++++++++++++++++++++++-------------- package.json | 4 +-- 4 files changed, 87 insertions(+), 22 deletions(-) diff --git a/.tape.js b/.tape.js index 8332a56..adfe378 100644 --- a/.tape.js +++ b/.tape.js @@ -7,6 +7,30 @@ module.exports = { source: 'body { left: 0 }', args: [ 'always', { except: 'left' }], warnings: 0, + }, { + source: 'body { top: -4px; left: 0; }', + args: 'always', + warnings: 2, + }, { + source: 'body { top: -4px; left: 0; }', + args: ['always', { except: 'top' }], + warnings: 1, + }, { + source: 'body { top: -4px; left: 0; }', + args: ['always', { except: 'left' }], + warnings: 1, + }, { + source: 'body { top: -4px; left: 0; }', + args: ['always', { except: ['top', 'left'] }], + warnings: 0, + }, { + source: 'body { margin-top: 0.5rem; margin-bottom: 0.5rem; }', + args: ['always', { except: ['margin-top'] }], + warnings: 1, + }, { + source: 'body { margin-top: 0.5rem; margin-bottom: 0.5rem; }', + args: ['always', { except: ['margin-top', 'margin-bottom'] }], + warnings: 0, }, { source: 'body { top: 0; left: 0 }', args: 'always', diff --git a/CHANGELOG.md b/CHANGELOG.md index d7f9a64..e3020bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changes to Property Use Logical +### 2.1.2 (March 3, 2024) + +- Fix `exports` in `package.json` [#25](https://github.com/csstools/stylelint-use-logical/issues/25) +- Fix `except` plugin option [#3](https://github.com/csstools/stylelint-use-logical/issues/3) + ### 2.1.1 (February 19, 2024) - Updated: peer `stylelint` to `>= 11 < 17` (patch) [#22](https://github.com/csstools/stylelint-use-logical/pull/22) diff --git a/index.js b/index.js index 5d5d29b..066285a 100644 --- a/index.js +++ b/index.js @@ -41,6 +41,15 @@ function ruleFunc(method, opts, context) { // validate or autofix 4 physical properties as logical shorthands physical4Prop.forEach(([ props, prop ]) => { validateRuleWithProps(node, props, (blockStartDecl, blockStartIndex, inlineStartDecl, inlineStartIndex, blockEndDecl, blockEndIndex, inlineEndDecl, inlineEndIndex) => { // eslint-disable-line + if ( + isDeclAnException(blockStartDecl, propExceptions) || + isDeclAnException(inlineStartDecl, propExceptions) || + isDeclAnException(blockEndDecl, propExceptions) || + isDeclAnException(inlineEndDecl, propExceptions) + ) { + return; + } + const firstInlineDecl = blockStartDecl; if (isAutofix) { @@ -81,6 +90,13 @@ function ruleFunc(method, opts, context) { // validate or autofix 2 physical properties as logical shorthands physical2Prop().forEach(([ props, prop ]) => { validateRuleWithProps(node, props, (blockStartDecl, blockStartIndex, inlineStartDecl, inlineStartIndex) => { // eslint-disable-line + if ( + isDeclAnException(blockStartDecl, propExceptions) || + isDeclAnException(inlineStartDecl, propExceptions) + ) { + return; + } + const firstInlineDecl = blockStartIndex < inlineStartIndex ? blockStartDecl : inlineStartDecl; @@ -107,33 +123,41 @@ function ruleFunc(method, opts, context) { // validate or autofix physical properties as logical physicalProp(dir).forEach(([ props, prop ]) => { validateRuleWithProps(node, props, physicalDecl => { - if (!isDeclAnException(physicalDecl, propExceptions)) { - if (isAutofix) { - physicalDecl.prop = prop; - } else if (!isDeclReported(physicalDecl)) { - reportUnexpectedProperty(physicalDecl, prop); + if (isDeclAnException(physicalDecl, propExceptions)) { + return; + } - reportedDecls.set(physicalDecl); - } + if (isAutofix) { + physicalDecl.prop = prop; + } else if (!isDeclReported(physicalDecl)) { + reportUnexpectedProperty(physicalDecl, prop); + + reportedDecls.set(physicalDecl); } }); }); // validate or autofix physical values as logical physicalValue(dir).forEach(([ regexp, props ]) => { - if (isNodeMatchingDecl(node, regexp) && !isDeclAnException(node, propExceptions)) { - const valuekey = node.value.toLowerCase(); + if (!isNodeMatchingDecl(node, regexp)) { + return; + } - if (valuekey in props) { - const value = props[valuekey]; + if (isDeclAnException(node, propExceptions)) { + return; + } - if (isAutofix) { - node.value = value; - } else { - reportUnexpectedValue(node, value); + const valuekey = node.value.toLowerCase(); - reportedDecls.set(node); - } + if (valuekey in props) { + const value = props[valuekey]; + + if (isAutofix) { + node.value = value; + } else { + reportUnexpectedValue(node, value); + + reportedDecls.set(node); } } }); @@ -149,7 +173,19 @@ const isMethodIndifferent = method => method === 'ignore' || method === false || const isMethodAlways = method => method === 'always' || method === true; const isContextAutofixing = context => Boolean(Object(context).fix); const isNodeMatchingDecl = (decl, regexp) => decl.type === 'decl' && regexp.test(decl.prop); -const isDeclAnException = (decl, propExceptions) => propExceptions.some(match => match instanceof RegExp - ? match.test(decl.prop) -: String(match || '').toLowerCase() === String(decl.prop || '').toLowerCase()); + +const isDeclAnException = (decl, propExceptions) => { + if (!decl || decl.type !== 'decl') { + return false; + } + + return propExceptions.some((match) => { + if (match instanceof RegExp) { + return match.test(decl.prop); + } + + return String(match || '').toLowerCase() === String(decl.prop || '').toLowerCase(); + }); +} + const isDeclReported = decl => reportedDecls.has(decl); diff --git a/package.json b/package.json index 23ae844..6784291 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stylelint-use-logical", - "version": "2.1.1", + "version": "2.1.2", "description": "Enforce usage of logical properties and values in CSS", "license": "CC0-1.0", "author": "Jonathan Neal ", @@ -12,7 +12,7 @@ "exports": { ".": { "import": "./index.mjs", - "node": "./index.cjs" + "require": "./index.cjs" } }, "files": [