Skip to content

Commit

Permalink
Arr isLessVariable util, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fanich37 committed Nov 11, 2019
1 parent 1bf1d4f commit 9361495
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
3 changes: 2 additions & 1 deletion 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 @@ -66,7 +67,7 @@ const rule = function(actual, secondary, context) {

root.walkAtRules((atRule) => {
// Ignore Less variables
if (atRule.variable && !hasBlock(atRule)) {
if (isLessVariable(atRule)) {
return;
}

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);
};

0 comments on commit 9361495

Please sign in to comment.