Skip to content

Commit

Permalink
v2.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke committed Mar 3, 2024
1 parent 3f0a30c commit a48b8c6
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 22 deletions.
24 changes: 24 additions & 0 deletions .tape.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
76 changes: 56 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
});
Expand All @@ -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);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <jonathantneal@hotmail.com>",
Expand All @@ -12,7 +12,7 @@
"exports": {
".": {
"import": "./index.mjs",
"node": "./index.cjs"
"require": "./index.cjs"
}
},
"files": [
Expand Down

0 comments on commit a48b8c6

Please sign in to comment.