@@ -14,8 +14,7 @@ const DEFINE_DIRECTIVE = new RegExp(
1414 `(?:\\*?\\s*@define ${ DEFINE_VALUE } )|(?:\\s*postcss-bem-linter: define ${ DEFINE_VALUE } )\\s*`
1515) ;
1616const END_DIRECTIVE = new RegExp (
17- '(?:\\*\\s*@end\\s*)|' +
18- '(?:\\s*postcss-bem-linter: end)\\s*'
17+ '(?:\\*\\s*@end\\s*)|' + '(?:\\s*postcss-bem-linter: end)\\s*'
1918) ;
2019const UTILITIES_IDENT = 'utilities' ;
2120const WEAK_IDENT = 'weak' ;
@@ -34,128 +33,150 @@ function stripUnderscore(str) {
3433 * @param {Object|String } primaryOptions
3534 * @param {Object } [secondaryOptions]
3635 */
37- module . exports = postcss . plugin ( 'postcss-bem-linter' , ( primaryOptions , secondaryOptions ) => {
38- const config = generateConfig ( primaryOptions , secondaryOptions ) ;
39- const patterns = config . patterns ;
40-
41- return ( root , result ) => {
42- const ranges = findRanges ( root ) ;
43-
44- root . walkRules ( ( rule ) => {
45- if ( rule . parent && rule . parent . name === 'keyframes' ) return ;
46- if ( ! rule . source ) return ;
47-
48- const ruleStartLine = rule . source . start . line ;
49- ranges . forEach ( ( range ) => {
50- if ( ruleStartLine < range . start ) return ;
51- if ( range . end && ruleStartLine > range . end ) return ;
52- checkRule ( rule , range ) ;
36+ module . exports = postcss . plugin (
37+ 'postcss-bem-linter' ,
38+ ( primaryOptions , secondaryOptions ) => {
39+ const config = generateConfig ( primaryOptions , secondaryOptions ) ;
40+ const patterns = config . patterns ;
41+
42+ return ( root , result ) => {
43+ const ranges = findRanges ( root ) ;
44+
45+ root . walkRules ( rule => {
46+ if ( rule . parent && rule . parent . name === 'keyframes' ) return ;
47+ if ( ! rule . source ) return ;
48+
49+ const ruleStartLine = rule . source . start . line ;
50+ ranges . forEach ( range => {
51+ if ( ruleStartLine < range . start ) return ;
52+ if ( range . end && ruleStartLine > range . end ) return ;
53+ checkRule ( rule , range ) ;
54+ } ) ;
5355 } ) ;
54- } ) ;
5556
56- function checkRule ( rule , range ) {
57- if ( range . defined === UTILITIES_IDENT ) {
58- if ( ! patterns . utilitySelectors ) {
57+ function checkRule ( rule , range ) {
58+ if ( range . defined === UTILITIES_IDENT ) {
59+ if ( ! patterns . utilitySelectors ) {
60+ throw new Error (
61+ 'You tried to `@define utilities` but have not provided ' +
62+ 'a `utilitySelectors` pattern'
63+ ) ;
64+ }
65+ validateUtilities ( {
66+ rule,
67+ utilityPattern : toRegexp ( patterns . utilitySelectors ) ,
68+ ignorePattern : toRegexp ( patterns . ignoreSelectors ) ,
69+ result,
70+ } ) ;
71+ return ;
72+ }
73+
74+ if ( ! patterns . componentSelectors ) {
5975 throw new Error (
60- 'You tried to `@define utilities` but have not provided ' +
61- 'a `utilitySelectors ` pattern'
76+ 'You tried to `@define` a component but have not provided ' +
77+ 'a `componentSelectors ` pattern'
6278 ) ;
6379 }
64- validateUtilities ( {
80+ validateCustomProperties ( {
6581 rule,
66- utilityPattern : toRegexp ( patterns . utilitySelectors ) ,
82+ componentName : range . defined ,
83+ result,
84+ ignorePattern : toRegexp ( patterns . ignoreCustomProperties ) ,
85+ } ) ;
86+ validateSelectors ( {
87+ rule,
88+ componentName : range . defined ,
89+ weakMode : range . weakMode ,
90+ selectorPattern : patterns . componentSelectors ,
91+ selectorPatternOptions : config . presetOptions ,
6792 ignorePattern : toRegexp ( patterns . ignoreSelectors ) ,
6893 result,
6994 } ) ;
70- return ;
7195 }
7296
73- if ( ! patterns . componentSelectors ) {
74- throw new Error (
75- 'You tried to `@define` a component but have not provided ' +
76- 'a `componentSelectors` pattern'
77- ) ;
78- }
79- validateCustomProperties ( {
80- rule,
81- componentName : range . defined ,
82- result,
83- ignorePattern : toRegexp ( patterns . ignoreCustomProperties ) ,
84- } ) ;
85- validateSelectors ( {
86- rule,
87- componentName : range . defined ,
88- weakMode : range . weakMode ,
89- selectorPattern : patterns . componentSelectors ,
90- selectorPatternOptions : config . presetOptions ,
91- ignorePattern : toRegexp ( patterns . ignoreSelectors ) ,
92- result,
93- } ) ;
94- }
97+ function findRanges ( root ) {
98+ const ranges = [ ] ;
99+
100+ if ( root . source && root . source . input && root . source . input . file ) {
101+ const filename = root . source . input . file ;
102+ if (
103+ checkImplicit . isImplicitUtilities (
104+ config . implicitUtilities ,
105+ filename
106+ )
107+ ) {
108+ ranges . push ( {
109+ defined : 'utilities' ,
110+ start : 0 ,
111+ weakMode : false ,
112+ } ) ;
113+ } else if (
114+ checkImplicit . isImplicitComponent (
115+ config . implicitComponents ,
116+ filename
117+ )
118+ ) {
119+ let defined = stripUnderscore (
120+ path . basename ( filename ) . split ( '.' ) [ 0 ]
121+ ) ;
122+ if ( defined === 'index' ) {
123+ defined = path . basename ( path . join ( filename , '..' ) ) ;
124+ }
125+
126+ if (
127+ defined !== UTILITIES_IDENT &&
128+ ! toRegexp ( config . componentNamePattern ) . test ( defined )
129+ ) {
130+ result . warn (
131+ `Invalid component name from implicit conversion from filename ${ filename } `
132+ ) ;
133+ }
134+ ranges . push ( {
135+ defined,
136+ start : 0 ,
137+ weakMode : false ,
138+ } ) ;
139+ }
140+ }
95141
96- function findRanges ( root ) {
97- const ranges = [ ] ;
142+ root . walkComments ( comment => {
143+ const commentStartLine = comment . source
144+ ? comment . source . start . line
145+ : null ;
146+ if ( ! commentStartLine ) return ;
98147
99- if ( root . source && root . source . input && root . source . input . file ) {
100- const filename = root . source . input . file ;
101- if ( checkImplicit . isImplicitUtilities ( config . implicitUtilities , filename ) ) {
102- ranges . push ( {
103- defined : 'utilities' ,
104- start : 0 ,
105- weakMode : false ,
106- } ) ;
107- } else if ( checkImplicit . isImplicitComponent ( config . implicitComponents , filename ) ) {
108- let defined = stripUnderscore ( path . basename ( filename ) . split ( '.' ) [ 0 ] ) ;
109- if ( defined === 'index' ) {
110- defined = path . basename ( path . join ( filename , '..' ) ) ;
148+ if ( END_DIRECTIVE . test ( comment . text ) ) {
149+ endCurrentRange ( commentStartLine ) ;
150+ return ;
111151 }
112152
113- if ( defined !== UTILITIES_IDENT && ! toRegexp ( config . componentNamePattern ) . test ( defined ) ) {
114- result . warn (
115- `Invalid component name from implicit conversion from filename ${ filename } `
116- ) ;
153+ const directiveMatch = comment . text . match ( DEFINE_DIRECTIVE ) ;
154+ if ( ! directiveMatch ) return ;
155+ const defined = ( directiveMatch [ 1 ] || directiveMatch [ 3 ] ) . trim ( ) ;
156+ if (
157+ defined !== UTILITIES_IDENT &&
158+ ! toRegexp ( config . componentNamePattern ) . test ( defined )
159+ ) {
160+ result . warn ( `Invalid component name in definition /*${ comment } */` , {
161+ node : comment ,
162+ } ) ;
117163 }
164+ endCurrentRange ( commentStartLine ) ;
118165 ranges . push ( {
119166 defined,
120- start : 0 ,
121- weakMode : false ,
167+ start : commentStartLine ,
168+ weakMode : directiveMatch [ 2 ] === WEAK_IDENT ,
122169 } ) ;
123- }
124- }
125-
126- root . walkComments ( ( comment ) => {
127- const commentStartLine = ( comment . source ) ? comment . source . start . line : null ;
128- if ( ! commentStartLine ) return ;
129-
130- if ( END_DIRECTIVE . test ( comment . text ) ) {
131- endCurrentRange ( commentStartLine ) ;
132- return ;
133- }
134-
135- const directiveMatch = comment . text . match ( DEFINE_DIRECTIVE ) ;
136- if ( ! directiveMatch ) return ;
137- const defined = ( directiveMatch [ 1 ] || directiveMatch [ 3 ] ) . trim ( ) ;
138- if ( defined !== UTILITIES_IDENT && ! toRegexp ( config . componentNamePattern ) . test ( defined ) ) {
139- result . warn (
140- `Invalid component name in definition /*${ comment } */` ,
141- { node : comment }
142- ) ;
143- }
144- endCurrentRange ( commentStartLine ) ;
145- ranges . push ( {
146- defined,
147- start : commentStartLine ,
148- weakMode : directiveMatch [ 2 ] === WEAK_IDENT ,
149170 } ) ;
150- } ) ;
151- return ranges ;
171+ return ranges ;
152172
153- function endCurrentRange ( line ) {
154- if ( ! ranges . length ) return ;
155- const lastRange = ranges [ ranges . length - 1 ] ;
156- if ( lastRange . end ) return ;
157- lastRange . end = line ;
173+ function endCurrentRange ( line ) {
174+ if ( ! ranges . length ) return ;
175+ const lastRange = ranges [ ranges . length - 1 ] ;
176+ if ( lastRange . end ) return ;
177+ lastRange . end = line ;
178+ }
158179 }
159- }
160- } ;
161- } ) ;
180+ } ;
181+ }
182+ ) ;
0 commit comments