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

Ignore less variables (#4200) #4405

Merged
merged 3 commits into from Nov 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions lib/rules/length-zero-no-unit/__tests__/index.js
Expand Up @@ -562,3 +562,51 @@ testRule(rule, {
},
],
});

testRule(rule, {
ruleName,
config: [true],
fix: true,
syntax: 'less',
accept: [
{
code: '@variable: 0px;',
description: 'ignore Less variables',
},
{
code: '@detached-ruleset: { line-height: 0px; };',
description: 'ignore Less detached ruleset with line-height',
},
{
code: '@detached-ruleset: { font: normal normal 400 0/0px cursive; };',
description: 'ignore Less detached ruleset with font',
},
],

reject: [
{
code: '@detached-ruleset: { font-size: 0px; };',
fixed: '@detached-ruleset: { font-size: 0; };',

message: messages.rejected,
line: 1,
column: 34,
},
{
code: '@detached-ruleset: { grid-template-columns: 0fr; font-size: 0px; line-height: 0px; };',
fixed: '@detached-ruleset: { grid-template-columns: 0fr; font-size: 0; line-height: 0px; };',

message: messages.rejected,
line: 1,
column: 62,
},
{
code: '@detached-ruleset: { font: normal normal 400 0px/0px cursive; };',
fixed: '@detached-ruleset: { font: normal normal 400 0/0px cursive; };',

message: messages.rejected,
line: 1,
column: 47,
},
],
});
6 changes: 6 additions & 0 deletions lib/rules/length-zero-no-unit/index.js
Expand Up @@ -5,6 +5,7 @@ const beforeBlockString = require('../../utils/beforeBlockString');
const blurComments = require('../../utils/blurComments');
const hasBlock = require('../../utils/hasBlock');
const isCustomProperty = require('../../utils/isCustomProperty');
const isLessVariable = require('../../utils/isLessVariable');
const isMathFunction = require('../../utils/isMathFunction');
const keywordSets = require('../../reference/keywordSets');
const optionsMatches = require('../../utils/optionsMatches');
Expand Down Expand Up @@ -65,6 +66,11 @@ const rule = function(actual, secondary, context) {
});

root.walkAtRules((atRule) => {
// Ignore Less variables
if (isLessVariable(atRule)) {
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should move this to utils?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable.
Added util function and some tests.


const source = hasBlock(atRule)
? beforeBlockString(atRule, { noRawBefore: true })
: atRule.toString();
Expand Down
46 changes: 46 additions & 0 deletions lib/utils/__tests__/isLessVariable.test.js
@@ -0,0 +1,46 @@
'use strict';

const isLessVariable = require('../isLessVariable');
const less = require('postcss-less');

describe('isLessVariable', () => {
it('is less variable', () => {
lessAtRules('@var: 10px;', (atRule) => {
expect(isLessVariable(atRule)).toBeTruthy();
});
});

it('is less variable with function', () => {
lessAtRules('@hover-color: darken(@color, 10%);', (atRule) => {
expect(isLessVariable(atRule)).toBeTruthy();
});
});

it('@charset is not a less variable', () => {
lessAtRules('@charset UTF-8;', (atRule) => {
expect(isLessVariable(atRule)).toBeFalsy();
});
});

it('@import is not a less variable', () => {
lessAtRules('@import url("some-styles.css");', (atRule) => {
expect(isLessVariable(atRule)).toBeFalsy();
});
});

it('@media is not a less variable', () => {
lessAtRules('@media (min-width: 100px) {};', (atRule) => {
expect(isLessVariable(atRule)).toBeFalsy();
});
});

it('detached ruleset is not a less variable', () => {
lessAtRules('@detached-ruleset: { margin: 0 };', (atRule) => {
expect(isLessVariable(atRule)).toBeFalsy();
});
});
});

function lessAtRules(css, cb) {
less.parse(css).walkAtRules(cb);
}
8 changes: 8 additions & 0 deletions lib/utils/isLessVariable.js
@@ -0,0 +1,8 @@
/* @flow */
'use strict';

const hasBlock = require('./hasBlock');

module.exports = function(atRule /*: Object*/) /*: boolean*/ {
return atRule.type === 'atrule' && atRule.variable && !hasBlock(atRule);
};