Skip to content

Commit 6d4aa56

Browse files
committed
Exclude reserved words from named exports
1 parent 1691bcd commit 6d4aa56

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

src/cssModuleToInterface.js

+46
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,52 @@ export const filterNonWordClasses = (cssModuleKeys) => {
2828
return [filteredClassNames, nonWordClassNames,];
2929
};
3030

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+
3177
export const filenameToTypingsFilename = (filename) => {
3278
const dirName = path.dirname(filename);
3379
const baseName = path.basename(filename);

src/index.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'colour';
55

66
import {
77
filterNonWordClasses,
8+
filterReservedWordClasses,
89
generateNamedExports,
910
generateGenericExportInterface,
1011
filenameToTypingsFilename,
@@ -61,7 +62,17 @@ The following classes will not be available as named exports:
6162
${skippedDefinitions.map(sd => ` - "${sd}"`).join('\n').red}
6263
`.yellow);
6364
}
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);
6576
}
6677
if (cssModuleDefinition.trim() === '') {
6778
// Ensure empty CSS modules export something

test/example-namedexport.css

+101
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,104 @@
55
.bar-baz {
66
color: green;
77
}
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+
}

0 commit comments

Comments
 (0)