Skip to content

Commit 9a4e4f9

Browse files
andieelmessimonsmith
authored andcommitted
fix css properties check in bem mod files
1 parent 3aa8d76 commit 9a4e4f9

7 files changed

+111
-1
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const generateConfig = require('./lib/generate-config');
77
const toRegexp = require('./lib/to-regexp');
88
const path = require('path');
99
const checkImplicit = require('./lib/check-implicit');
10+
const getComponentNameFromFilename = require('./lib/get-component-name-from-filename');
1011

1112
const DEFINE_VALUE = '([-_a-zA-Z0-9]+)\\s*(?:;\\s*(weak))?';
1213
const DEFINE_DIRECTIVE = new RegExp(
@@ -120,16 +121,19 @@ const plugin = (primaryOptions, secondaryOptions) => {
120121
);
121122
if (defined === 'index') {
122123
defined = path.basename(path.join(filename, '..'));
124+
} else {
125+
defined = getComponentNameFromFilename(defined, config);
123126
}
124127

125128
if (
126129
defined !== UTILITIES_IDENT &&
127130
!toRegexp(config.componentNamePattern).test(defined)
128131
) {
129132
result.warn(
130-
`Invalid component name from implicit conversion from filename ${filename}`
133+
`Invalid component name ${defined} from implicit conversion from filename ${filename}`
131134
);
132135
}
136+
133137
ranges.push({
134138
defined,
135139
start: 0,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
/**
4+
* @param {String} filename
5+
* @param {Object} config
6+
* @param {String} config.componentNamePattern
7+
*/
8+
module.exports = (filename, config) => {
9+
const {componentNamePattern} = config;
10+
11+
if (componentNamePattern.test(filename)) return filename;
12+
13+
for (let i = 0; i < filename.length; i++) {
14+
const part = filename.slice(0, -i);
15+
if (componentNamePattern.test(part)) return part;
16+
}
17+
18+
return filename;
19+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.component-name__element {
2+
--component-name--color: red;
3+
}
4+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.component-name_boolean-mod {
2+
--component-name--color: green;
3+
}
4+
5+
.component-name_mod_value {
6+
--component-name--color: red;
7+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.component-name {
2+
--component-name--color: green;
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** @define component-name */
2+
3+
.component-name {
4+
--component-name--color: green;
5+
}
6+
7+
.component-name_boolean-mod {
8+
--component-name--color: red;
9+
}
10+
11+
.component-name_mod_value {
12+
--component-name--color: red;
13+
}
14+
15+
.component-name__element {
16+
--component-name--color: red;
17+
}

test/property-validation.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,67 @@ const util = require('./test-util');
22

33
const assertSuccess = util.assertSuccess;
44
const assertSingleFailure = util.assertSingleFailure;
5+
const assertFailure = util.assertFailure;
56
const fixture = util.fixture;
67

78
describe('property validation', () => {
89
it('accepts custom properties that begin with the component name', () => {
910
assertSuccess(fixture('properties-valid'));
11+
assertSuccess(fixture('properties-bem-valid'), {preset: 'bem'});
12+
});
13+
14+
it('accepts custom properties that implicitly begin with the component name', () => {
15+
const filenameBlock = `${process.cwd()}/component-name.css`;
16+
assertSuccess(
17+
fixture('implicit-component-properties-valid'),
18+
{preset: 'bem', implicitComponents: true},
19+
null,
20+
filenameBlock
21+
);
22+
});
23+
24+
it('accepts custom properties that implicitly begin with the component name in bem files', () => {
25+
const filenameMod = `${process.cwd()}/component-name_mod.css`;
26+
assertSuccess(
27+
fixture('implicit-component-mod-properties-valid'),
28+
{preset: 'bem', implicitComponents: true},
29+
null,
30+
filenameMod
31+
);
32+
33+
const filenameElement = `${process.cwd()}/component-name__element.css`;
34+
assertSuccess(
35+
fixture('implicit-component-element-properties-valid'),
36+
{preset: 'bem', implicitComponents: true},
37+
null,
38+
filenameElement
39+
);
40+
});
41+
42+
it('rejects custom properties that do not begin with the implicit component name in bem files', () => {
43+
const filenameBlock = `${process.cwd()}/component-name-invalid.css`;
44+
assertFailure(
45+
fixture('implicit-component-properties-valid'),
46+
{preset: 'bem', implicitComponents: true},
47+
null,
48+
filenameBlock
49+
);
50+
51+
const filenameMod = `${process.cwd()}/component-name-invalid_mod.css`;
52+
assertFailure(
53+
fixture('implicit-component-mod-properties-valid'),
54+
{preset: 'bem', implicitComponents: true},
55+
null,
56+
filenameMod
57+
);
58+
59+
const filenameElement = `${process.cwd()}/component-name-invalid__element.css`;
60+
assertFailure(
61+
fixture('implicit-component-element-properties-valid'),
62+
{preset: 'bem', implicitComponents: true},
63+
null,
64+
filenameElement
65+
);
1066
});
1167

1268
const invDef = '/** @define InvalidRootVars */';

0 commit comments

Comments
 (0)