File tree 3 files changed +159
-1
lines changed
3 files changed +159
-1
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,52 @@ export const filterNonWordClasses = (cssModuleKeys) => {
28
28
return [ filteredClassNames , nonWordClassNames , ] ;
29
29
} ;
30
30
31
+ // Documented here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Reserved_keywords_as_of_ECMAScript_2015
32
+ const reservedWords = [
33
+ 'break' ,
34
+ 'case' ,
35
+ 'catch' ,
36
+ 'class' ,
37
+ 'const' ,
38
+ 'continue' ,
39
+ 'debugger' ,
40
+ 'default' ,
41
+ 'delete' ,
42
+ 'do' ,
43
+ 'else' ,
44
+ 'export' ,
45
+ 'extends' ,
46
+ 'finally' ,
47
+ 'for' ,
48
+ 'function' ,
49
+ 'if' ,
50
+ 'import' ,
51
+ 'in' ,
52
+ 'instanceof' ,
53
+ 'new' ,
54
+ 'return' ,
55
+ 'super' ,
56
+ 'switch' ,
57
+ 'this' ,
58
+ 'throw' ,
59
+ 'try' ,
60
+ 'typeof' ,
61
+ 'var' ,
62
+ 'void' ,
63
+ 'while' ,
64
+ 'with' ,
65
+ 'yield'
66
+ ] ;
67
+ export const filterReservedWordClasses = ( cssModuleKeys ) => {
68
+ const filteredClassNames = cssModuleKeys . filter ( classname => reservedWords . indexOf ( classname ) === - 1 ) ;
69
+ if ( filteredClassNames . length === cssModuleKeys . length ) {
70
+ return [ filteredClassNames , [ ] , ] ;
71
+ }
72
+ const reservedWordClassNames = cssModuleKeys . filter ( classname => reservedWords . indexOf ( classname ) !== - 1 ) ;
73
+ return [ filteredClassNames , reservedWordClassNames , ] ;
74
+ } ;
75
+
76
+
31
77
export const filenameToTypingsFilename = ( filename ) => {
32
78
const dirName = path . dirname ( filename ) ;
33
79
const baseName = path . basename ( filename ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import 'colour';
5
5
6
6
import {
7
7
filterNonWordClasses ,
8
+ filterReservedWordClasses ,
8
9
generateNamedExports ,
9
10
generateGenericExportInterface ,
10
11
filenameToTypingsFilename ,
@@ -61,7 +62,17 @@ The following classes will not be available as named exports:
61
62
${ skippedDefinitions . map ( sd => ` - "${ sd } "` ) . join ( '\n' ) . red }
62
63
` . yellow ) ;
63
64
}
64
- cssModuleDefinition = generateNamedExports ( cleanedDefinitions ) ;
65
+
66
+ const [ nonReservedWordDefinitions , reservedWordDefinitions , ] = filterReservedWordClasses ( cleanedDefinitions ) ;
67
+ if ( reservedWordDefinitions . length > 0 ) {
68
+ logger ( 'warn' , `Your css contains classes which are reserved words in JavaScript.
69
+ Consequently the following classes will not be available as named exports:
70
+ ${ reservedWordDefinitions . map ( rwd => ` - "${ rwd } "` ) . join ( '\n' ) . red }
71
+ These can be accessed using the object literal syntax; eg styles['delete'] instead of styles.delete.
72
+ ` . yellow ) ;
73
+ }
74
+
75
+ cssModuleDefinition = generateNamedExports ( nonReservedWordDefinitions ) ;
65
76
}
66
77
if ( cssModuleDefinition . trim ( ) === '' ) {
67
78
// Ensure empty CSS modules export something
Original file line number Diff line number Diff line change 5
5
.bar-baz {
6
6
color : green;
7
7
}
8
+
9
+ /* All of the below are reserved words in JavaScript and cannot be emitted as a named export */
10
+ .break {
11
+ color : red;
12
+ }
13
+ .case {
14
+ color : red;
15
+ }
16
+ .catch {
17
+ color : red;
18
+ }
19
+ .class {
20
+ color : red;
21
+ }
22
+ .const {
23
+ color : red;
24
+ }
25
+ .continue {
26
+ color : red;
27
+ }
28
+ .debugger {
29
+ color : red;
30
+ }
31
+ .default {
32
+ color : red;
33
+ }
34
+ .delete {
35
+ color : red;
36
+ }
37
+ .do {
38
+ color : red;
39
+ }
40
+ .else {
41
+ color : red;
42
+ }
43
+ .export {
44
+ color : red;
45
+ }
46
+ .extends {
47
+ color : red;
48
+ }
49
+ .finally {
50
+ color : red;
51
+ }
52
+ .for {
53
+ color : red;
54
+ }
55
+ .function {
56
+ color : red;
57
+ }
58
+ .if {
59
+ color : red;
60
+ }
61
+ .import {
62
+ color : red;
63
+ }
64
+ .in {
65
+ color : red;
66
+ }
67
+ .instanceof {
68
+ color : red;
69
+ }
70
+ .new {
71
+ color : red;
72
+ }
73
+ .return {
74
+ color : red;
75
+ }
76
+ .super {
77
+ color : red;
78
+ }
79
+ .switch {
80
+ color : red;
81
+ }
82
+ .this {
83
+ color : red;
84
+ }
85
+ .throw {
86
+ color : red;
87
+ }
88
+ .try {
89
+ color : red;
90
+ }
91
+ .typeof {
92
+ color : red;
93
+ }
94
+ .var {
95
+ color : red;
96
+ }
97
+ .void {
98
+ color : red;
99
+ }
100
+ .while {
101
+ color : red;
102
+ }
103
+ .with {
104
+ color : red;
105
+ }
106
+ .yield {
107
+ color : red;
108
+ }
You can’t perform that action at this time.
0 commit comments