Skip to content

Commit 9e1a5ba

Browse files
committed
feat: add test for exclude option with function returning false
1 parent f86e91e commit 9e1a5ba

2 files changed

Lines changed: 39 additions & 19 deletions

File tree

index.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ const typeToString = s =>
2626
.toLowerCase();
2727

2828
const types = [
29-
"String",
30-
"Array",
31-
"Undefined",
32-
"Boolean",
33-
"Number",
34-
"Function",
35-
"Symbol",
36-
"Object"
29+
'String',
30+
'Array',
31+
'Undefined',
32+
'Boolean',
33+
'Number',
34+
'Function',
35+
'Symbol',
36+
'Object',
37+
'RegExp'
3738
];
3839

3940
const type = types.reduce((acc, str) => {
40-
acc["is" + str] = val => typeToString(val) === str.toLowerCase();
41+
acc['is' + str] = val => typeToString(val) === str.toLowerCase();
4142
return acc;
4243
}, {});
4344

@@ -75,7 +76,8 @@ function createUnitRegex(customUnitList, unitList) {
7576
if (unitList.length === 0) {
7677
// Empty unitList means no units should be processed
7778
filteredUnits = [];
78-
} else {
79+
}
80+
else {
7981
const satisfyUnitList = createUnitListMatcher(unitList);
8082
filteredUnits = filteredUnits.filter(unit => satisfyUnitList(unit));
8183
}
@@ -135,12 +137,12 @@ function toFixed(number, precision) {
135137
}
136138

137139
function blacklistedSelector(blacklist, selector) {
138-
if (typeof selector !== "string") {
140+
if (typeof selector !== 'string') {
139141
return;
140142
}
141143

142144
return blacklist.some(regex => {
143-
if (typeof regex === "string") {
145+
if (typeof regex === 'string') {
144146
return selector.indexOf(regex) !== -1;
145147
}
146148

@@ -149,7 +151,7 @@ function blacklistedSelector(blacklist, selector) {
149151
}
150152

151153
function createPropListMatcher(propList) {
152-
const hasWild = propList.indexOf("*") > -1;
154+
const hasWild = propList.indexOf('*') > -1;
153155
const matchAll = hasWild && propList.length === 1;
154156
const lists = {
155157
exact: filterPropList.exact(propList),
@@ -196,7 +198,7 @@ function createPropListMatcher(propList) {
196198
}
197199

198200
function createUnitListMatcher(unitList) {
199-
const hasWild = unitList.indexOf("*") > -1;
201+
const hasWild = unitList.indexOf('*') > -1;
200202
const matchAll = hasWild && unitList.length === 1;
201203
const lists = {
202204
exact: filterPropList.exact(unitList),
@@ -254,7 +256,7 @@ module.exports = (options = {}) => {
254256
const unitRegex = createUnitRegex(customUnitList, unitList);
255257

256258
return {
257-
postcssPlugin: "postcss-unit-processor",
259+
postcssPlugin: 'postcss-unit-processor',
258260

259261
Once(css) {
260262
const filePath = css.source.input.file;
@@ -263,11 +265,12 @@ module.exports = (options = {}) => {
263265
exclude &&
264266
filePath &&
265267
((type.isFunction(exclude) && exclude(filePath)) ||
266-
(type.isString(exclude) && filePath.indexOf(exclude) !== -1) ||
267-
filePath.match(exclude) !== null)
268+
(type.isString(exclude) && filePath.indexOf(exclude) !== -1) ||
269+
(type.isRegExp(exclude) && filePath.match(exclude) !== null))
268270
) {
269271
isExcludeFile = true;
270-
} else {
272+
}
273+
else {
271274
isExcludeFile = false;
272275
}
273276

@@ -312,7 +315,7 @@ module.exports = (options = {}) => {
312315

313316
unitRegex.lastIndex = 0;
314317

315-
if (opts.mediaQuery && atRule.name === "media") {
318+
if (opts.mediaQuery && atRule.name === 'media') {
316319
if (atRule.__unitProcessorFinished === true || !unitRegex.test(atRule.params)) {
317320
return;
318321
}

index.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,23 @@ describe('postcss-unit-processor', () => {
335335
);
336336
});
337337

338+
// Test exclude option with function that returns false
339+
it('should exclude files when exclude function returns false', async () => {
340+
const processor = (value, unit) => {
341+
if (unit === 'px') {
342+
return { value: value * 2, unit: 'px' };
343+
}
344+
return { value, unit };
345+
};
346+
347+
await testProcess(
348+
'div { width: 100px; }',
349+
'div { width: 200px; }',
350+
{ processor, exclude: () => '(' !== '(' },
351+
{ from: 'test.css' }
352+
);
353+
});
354+
338355
// Test exclude option with string that matches file path
339356
it('should exclude files when file path contains exclude string', async () => {
340357
const processor = (value, unit) => {

0 commit comments

Comments
 (0)