@griffel/postcss-syntax 1.3.11 → 1.3.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/package.json +6 -4
- package/src/constants.d.ts +5 -0
- package/src/constants.js +9 -0
- package/src/constants.js.map +1 -0
- package/src/createSyntax.d.ts +8 -0
- package/src/createSyntax.js +18 -0
- package/src/createSyntax.js.map +1 -0
- package/src/{index.ts → index.d.ts} +6 -6
- package/src/index.js +17 -0
- package/src/index.js.map +1 -0
- package/src/location-preset.d.ts +28 -0
- package/src/location-preset.js +150 -0
- package/src/location-preset.js.map +1 -0
- package/src/parse.d.ts +17 -0
- package/src/parse.js +113 -0
- package/src/parse.js.map +1 -0
- package/src/stringify.d.ts +2 -0
- package/src/stringify.js +14 -0
- package/src/stringify.js.map +1 -0
- package/src/transform-sync.d.ts +13 -0
- package/src/transform-sync.js +30 -0
- package/src/transform-sync.js.map +1 -0
- package/.babelrc +0 -3
- package/CHANGELOG.json +0 -618
- package/CHANGELOG.md +0 -263
- package/eslint.config.mjs +0 -18
- package/project.json +0 -51
- package/src/constants.ts +0 -5
- package/src/createSyntax.ts +0 -19
- package/src/location-preset.ts +0 -206
- package/src/parse.test.ts +0 -501
- package/src/parse.ts +0 -155
- package/src/stringify.ts +0 -12
- package/src/transform-sync.test.ts +0 -300
- package/src/transform-sync.ts +0 -36
- package/tsconfig.json +0 -16
- package/tsconfig.lib.json +0 -11
- package/tsconfig.spec.json +0 -20
- package/vitest.config.mts +0 -18
package/src/parse.test.ts
DELETED
|
@@ -1,501 +0,0 @@
|
|
|
1
|
-
import type postcss from 'postcss';
|
|
2
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
GRIFFEL_DECLARATOR_LOCATION_RAW,
|
|
6
|
-
GRIFFEL_DECLARATOR_RAW,
|
|
7
|
-
GRIFFEL_SLOT_LOCATION_RAW,
|
|
8
|
-
GRIFFEL_SLOT_RAW,
|
|
9
|
-
GRIFFEL_SRC_RAW,
|
|
10
|
-
} from './constants';
|
|
11
|
-
import { parse } from './parse';
|
|
12
|
-
import { stringify } from './stringify';
|
|
13
|
-
|
|
14
|
-
describe('parse', () => {
|
|
15
|
-
describe('makeStyles', () => {
|
|
16
|
-
it('should return postcss AST of valid CSS', () => {
|
|
17
|
-
const fixture = `
|
|
18
|
-
import { makeStyles } from '@griffel/react';
|
|
19
|
-
|
|
20
|
-
const mixin = () => ({
|
|
21
|
-
marginTop: '4px',
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
export const useStyles = makeStyles({
|
|
25
|
-
root: {
|
|
26
|
-
color: 'red',
|
|
27
|
-
backgroundColor: 'green',
|
|
28
|
-
...mixin()
|
|
29
|
-
}
|
|
30
|
-
})
|
|
31
|
-
`;
|
|
32
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
33
|
-
expect(root.toString()).toMatchInlineSnapshot(
|
|
34
|
-
`".fe3e8s9{color:red;}.fcnqdeg{background-color:green;}.fvjh0tl{margin-top:4px;}"`,
|
|
35
|
-
);
|
|
36
|
-
root.walk(node => {
|
|
37
|
-
expect(node.raw(GRIFFEL_DECLARATOR_RAW)).toEqual('useStyles');
|
|
38
|
-
expect(node.raw(GRIFFEL_SLOT_RAW)).toEqual('root');
|
|
39
|
-
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
|
|
40
|
-
start: { line: 9, column: 2, index: 134 },
|
|
41
|
-
end: { line: 13, column: 3, index: 208 },
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
expect(root.source?.input.file?.endsWith('fixture.styles.ts')).toBe(true);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('should handle different module source', () => {
|
|
49
|
-
const fixture = `
|
|
50
|
-
import { foo} from '@foo/foo';
|
|
51
|
-
|
|
52
|
-
const mixin = () => ({
|
|
53
|
-
marginTop: '4px',
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
export const useStyles = foo({
|
|
57
|
-
root: {
|
|
58
|
-
color: 'red',
|
|
59
|
-
backgroundColor: 'green',
|
|
60
|
-
...mixin()
|
|
61
|
-
}
|
|
62
|
-
})
|
|
63
|
-
`;
|
|
64
|
-
const root = parse(fixture, {
|
|
65
|
-
from: 'fixture.styles.ts',
|
|
66
|
-
modules: [{ moduleSource: '@foo/foo', importName: 'foo' }],
|
|
67
|
-
});
|
|
68
|
-
expect(root.toString()).toMatchInlineSnapshot(
|
|
69
|
-
`".fe3e8s9{color:red;}.fcnqdeg{background-color:green;}.fvjh0tl{margin-top:4px;}"`,
|
|
70
|
-
);
|
|
71
|
-
|
|
72
|
-
root.walk(node => {
|
|
73
|
-
expect(node.raw(GRIFFEL_DECLARATOR_RAW)).toEqual('useStyles');
|
|
74
|
-
expect(node.raw(GRIFFEL_SLOT_RAW)).toEqual('root');
|
|
75
|
-
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
|
|
76
|
-
start: { line: 9, column: 2, index: 113 },
|
|
77
|
-
end: { line: 13, column: 3, index: 187 },
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('should map style locations to slots', () => {
|
|
83
|
-
const fixture = `
|
|
84
|
-
import { makeStyles } from '@griffel/react';
|
|
85
|
-
|
|
86
|
-
export const useStyles = makeStyles({
|
|
87
|
-
slot1: {
|
|
88
|
-
color: 'red',
|
|
89
|
-
},
|
|
90
|
-
|
|
91
|
-
slot2: {
|
|
92
|
-
color: 'blue',
|
|
93
|
-
}
|
|
94
|
-
})
|
|
95
|
-
`;
|
|
96
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
97
|
-
|
|
98
|
-
expect(root.toString()).toMatchInlineSnapshot(`
|
|
99
|
-
".fe3e8s9{color:red;}
|
|
100
|
-
.f163i14w{color:blue;}"
|
|
101
|
-
`);
|
|
102
|
-
|
|
103
|
-
root.walk(node => {
|
|
104
|
-
const slot = node.raw(GRIFFEL_SLOT_RAW);
|
|
105
|
-
expect(['slot1', 'slot2']).toContain(slot);
|
|
106
|
-
|
|
107
|
-
if (slot === 'slot1') {
|
|
108
|
-
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
|
|
109
|
-
start: { line: 5, column: 2, index: 87 },
|
|
110
|
-
end: { line: 7, column: 3, index: 117 },
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
if (slot === 'slot2') {
|
|
114
|
-
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
|
|
115
|
-
start: { line: 9, column: 2, index: 122 },
|
|
116
|
-
end: { line: 11, column: 3, index: 153 },
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
it('should map style locations to slots when styles contains new lines', () => {
|
|
123
|
-
const fixture = `
|
|
124
|
-
import { makeStyles } from "@griffel/react";
|
|
125
|
-
|
|
126
|
-
export const useStyles = makeStyles({
|
|
127
|
-
slot1: {
|
|
128
|
-
background: \`linear-gradient(#e66465,
|
|
129
|
-
#9198e5)\`,
|
|
130
|
-
},
|
|
131
|
-
|
|
132
|
-
slot2: {
|
|
133
|
-
color: "blue",
|
|
134
|
-
},
|
|
135
|
-
});
|
|
136
|
-
`;
|
|
137
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
138
|
-
|
|
139
|
-
expect(root.toString()).toMatchInlineSnapshot(`
|
|
140
|
-
".f1qmhkic{background:linear-gradient(#e66465, #9198e5);}
|
|
141
|
-
.f163i14w{color:blue;}"
|
|
142
|
-
`);
|
|
143
|
-
|
|
144
|
-
root.walk(node => {
|
|
145
|
-
const slot = node.raw(GRIFFEL_SLOT_RAW);
|
|
146
|
-
expect(['slot1', 'slot2']).toContain(slot);
|
|
147
|
-
|
|
148
|
-
if (slot === 'slot1') {
|
|
149
|
-
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
|
|
150
|
-
start: { line: 5, column: 2, index: 87 },
|
|
151
|
-
end: { line: 8, column: 3, index: 159 },
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
if (slot === 'slot2') {
|
|
155
|
-
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
|
|
156
|
-
start: { line: 10, column: 2, index: 164 },
|
|
157
|
-
end: { line: 12, column: 3, index: 195 },
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it('should map style locations to makeStyles call when it is not called with object expressions', () => {
|
|
164
|
-
const fixture = `
|
|
165
|
-
import { makeStyles } from "@griffel/react";
|
|
166
|
-
|
|
167
|
-
const styles = {
|
|
168
|
-
slot1: {
|
|
169
|
-
color: "red",
|
|
170
|
-
},
|
|
171
|
-
slot2: {
|
|
172
|
-
backgroundColor: "green",
|
|
173
|
-
},
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
export const useStyles = makeStyles(styles);
|
|
177
|
-
`;
|
|
178
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
179
|
-
|
|
180
|
-
expect(root.toString()).toMatchInlineSnapshot(`
|
|
181
|
-
".fe3e8s9{color:red;}
|
|
182
|
-
.fcnqdeg{background-color:green;}"
|
|
183
|
-
`);
|
|
184
|
-
|
|
185
|
-
root.walk(node => {
|
|
186
|
-
const slot = node.raw(GRIFFEL_SLOT_RAW);
|
|
187
|
-
expect(['slot1', 'slot2']).toContain(slot);
|
|
188
|
-
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
|
|
189
|
-
start: { line: 13, column: 25, index: 173 },
|
|
190
|
-
end: { line: 13, column: 43, index: 191 },
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it('should hold original source in document raw field', () => {
|
|
196
|
-
const fixture = `
|
|
197
|
-
import { makeStyles } from '@griffel/react';
|
|
198
|
-
|
|
199
|
-
export const useStyles = makeStyles({
|
|
200
|
-
slot1: {
|
|
201
|
-
color: 'red',
|
|
202
|
-
},
|
|
203
|
-
|
|
204
|
-
slot2: {
|
|
205
|
-
color: 'blue',
|
|
206
|
-
}
|
|
207
|
-
})
|
|
208
|
-
`;
|
|
209
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
210
|
-
expect(root.raw(GRIFFEL_SRC_RAW)).toEqual(fixture);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
it('should handle multiple declarators', () => {
|
|
214
|
-
const fixture = `
|
|
215
|
-
import { makeStyles } from '@griffel/react';
|
|
216
|
-
|
|
217
|
-
export const useStyles1 = makeStyles({
|
|
218
|
-
slot: {
|
|
219
|
-
color: 'red',
|
|
220
|
-
},
|
|
221
|
-
})
|
|
222
|
-
|
|
223
|
-
export const useStyles2 = makeStyles({
|
|
224
|
-
slot: {
|
|
225
|
-
color: 'red',
|
|
226
|
-
},
|
|
227
|
-
})
|
|
228
|
-
`;
|
|
229
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
230
|
-
|
|
231
|
-
expect(root.toString()).toMatchInlineSnapshot(`
|
|
232
|
-
".fe3e8s9{color:red;}
|
|
233
|
-
.fe3e8s9{color:red;}"
|
|
234
|
-
`);
|
|
235
|
-
|
|
236
|
-
root.walk(node => {
|
|
237
|
-
const declarator = node.raw(GRIFFEL_DECLARATOR_RAW);
|
|
238
|
-
expect(['useStyles1', 'useStyles2']).toContain(declarator);
|
|
239
|
-
|
|
240
|
-
if (declarator === 'useStyles1') {
|
|
241
|
-
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
|
|
242
|
-
start: { line: 5, column: 2, index: 88 },
|
|
243
|
-
end: { line: 7, column: 3, index: 117 },
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
if (declarator === 'useStyles2') {
|
|
248
|
-
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
|
|
249
|
-
start: { line: 11, column: 2, index: 164 },
|
|
250
|
-
end: { line: 13, column: 3, index: 193 },
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
it('should transform disable comment directive to stylelint disable', () => {
|
|
257
|
-
const fixture = `
|
|
258
|
-
import { makeStyles } from '@griffel/react';
|
|
259
|
-
|
|
260
|
-
export const useStyles = makeStyles({
|
|
261
|
-
root: {
|
|
262
|
-
color: 'red',
|
|
263
|
-
backgroundColor: 'green',
|
|
264
|
-
},
|
|
265
|
-
|
|
266
|
-
// griffel-csslint-disable foo
|
|
267
|
-
// griffel-csslint-disable bar
|
|
268
|
-
slot: {
|
|
269
|
-
color: 'blue',
|
|
270
|
-
backgroundColor: 'red',
|
|
271
|
-
}
|
|
272
|
-
})
|
|
273
|
-
`;
|
|
274
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
275
|
-
expect(root.toString()).toMatchInlineSnapshot(`
|
|
276
|
-
".fe3e8s9{color:red;}.fcnqdeg{background-color:green;}
|
|
277
|
-
.f163i14w{color:blue;}.f3xbvq9{background-color:red;} /* stylelint-disable-line foo,bar */"
|
|
278
|
-
`);
|
|
279
|
-
});
|
|
280
|
-
|
|
281
|
-
it('should not throw on failure to parse JS code when `silenceOnParseErrors` is set', () => {
|
|
282
|
-
const fixture = `
|
|
283
|
-
import { makeStyles } from '@griffel/react';
|
|
284
|
-
|
|
285
|
-
export const useStyles = makeStyles({
|
|
286
|
-
slot: {
|
|
287
|
-
// simulate user WIP code
|
|
288
|
-
color: 'bl,
|
|
289
|
-
}
|
|
290
|
-
})
|
|
291
|
-
`;
|
|
292
|
-
const root = parse(fixture, { from: 'fixture.styles.ts', silenceParseErrors: true });
|
|
293
|
-
expect(root.toString()).toMatchInlineSnapshot(`"/* Failed to parse griffel styles: fixture.styles.ts */"`);
|
|
294
|
-
expect(() => stringify(root, {} as postcss.Builder)).not.toThrow();
|
|
295
|
-
});
|
|
296
|
-
|
|
297
|
-
it('should throw on failure to parse JS code', () => {
|
|
298
|
-
const fixture = `
|
|
299
|
-
import { makeStyles } from '@griffel/react';
|
|
300
|
-
|
|
301
|
-
export const useStyles = makeStyles({
|
|
302
|
-
slot: {
|
|
303
|
-
// simulate user WIP code
|
|
304
|
-
color: 'bl,
|
|
305
|
-
}
|
|
306
|
-
})
|
|
307
|
-
`;
|
|
308
|
-
expect(() => parse(fixture, { from: 'fixture.styles.ts' })).toThrow('Unterminated string constant');
|
|
309
|
-
});
|
|
310
|
-
});
|
|
311
|
-
|
|
312
|
-
describe('makeResetStyles', () => {
|
|
313
|
-
it('should return postcss AST of valid CSS', () => {
|
|
314
|
-
const fixture = `
|
|
315
|
-
import { makeResetStyles } from '@griffel/react';
|
|
316
|
-
|
|
317
|
-
export const useResetStyles = makeResetStyles({
|
|
318
|
-
color: 'red',
|
|
319
|
-
backgroundColor: 'green',
|
|
320
|
-
})
|
|
321
|
-
`;
|
|
322
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
323
|
-
expect(root.toString()).toMatchInlineSnapshot(`".rbe9p1m{color:red;background-color:green;}"`);
|
|
324
|
-
});
|
|
325
|
-
|
|
326
|
-
it('should handle different module source', () => {
|
|
327
|
-
const fixture = `
|
|
328
|
-
import { foo } from '@foo/foo';
|
|
329
|
-
|
|
330
|
-
export const useResetStyles = foo({
|
|
331
|
-
color: 'red',
|
|
332
|
-
backgroundColor: 'green',
|
|
333
|
-
})
|
|
334
|
-
`;
|
|
335
|
-
const root = parse(fixture, {
|
|
336
|
-
from: 'fixture.styles.ts',
|
|
337
|
-
modules: [{ moduleSource: '@foo/foo', importName: 'makeStyles', resetImportName: 'foo' }],
|
|
338
|
-
});
|
|
339
|
-
expect(root.toString()).toMatchInlineSnapshot(`".rbe9p1m{color:red;background-color:green;}"`);
|
|
340
|
-
root.walk(node => {
|
|
341
|
-
expect(node.raw(GRIFFEL_DECLARATOR_RAW)).toEqual('useResetStyles');
|
|
342
|
-
|
|
343
|
-
expect(node.raw(GRIFFEL_DECLARATOR_LOCATION_RAW)).toEqual({
|
|
344
|
-
start: { line: 4, column: 34, index: 68 },
|
|
345
|
-
end: { line: 7, column: 1, index: 115 },
|
|
346
|
-
});
|
|
347
|
-
});
|
|
348
|
-
});
|
|
349
|
-
|
|
350
|
-
it('should map style locations to reset styles', () => {
|
|
351
|
-
const fixture = `
|
|
352
|
-
import { makeResetStyles } from '@griffel/react';
|
|
353
|
-
|
|
354
|
-
export const useResetStyles = makeResetStyles({
|
|
355
|
-
color: 'red',
|
|
356
|
-
backgroundColor: 'green',
|
|
357
|
-
})
|
|
358
|
-
`;
|
|
359
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
360
|
-
|
|
361
|
-
root.walk(node => {
|
|
362
|
-
const declarator = node.raw(GRIFFEL_DECLARATOR_RAW);
|
|
363
|
-
expect(declarator).toEqual('useResetStyles');
|
|
364
|
-
|
|
365
|
-
expect(node.raw(GRIFFEL_DECLARATOR_LOCATION_RAW)).toEqual({
|
|
366
|
-
start: { line: 4, column: 46, index: 98 },
|
|
367
|
-
end: { line: 7, column: 1, index: 145 },
|
|
368
|
-
});
|
|
369
|
-
});
|
|
370
|
-
});
|
|
371
|
-
|
|
372
|
-
it('should map style locations to reset styles when styles contains new lines', () => {
|
|
373
|
-
const fixture = `
|
|
374
|
-
import { makeResetStyles } from "@griffel/react";
|
|
375
|
-
|
|
376
|
-
export const useResetStyles1 = makeResetStyles({
|
|
377
|
-
color: "red",
|
|
378
|
-
background: \`linear-gradient(#e66465,
|
|
379
|
-
#9198e5)\`,
|
|
380
|
-
});
|
|
381
|
-
export const useResetStyles2 = makeResetStyles({
|
|
382
|
-
color: "red",
|
|
383
|
-
});
|
|
384
|
-
`;
|
|
385
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
386
|
-
|
|
387
|
-
root.walk(node => {
|
|
388
|
-
const declarator = node.raw(GRIFFEL_DECLARATOR_RAW);
|
|
389
|
-
if (declarator === 'useResetStyles1') {
|
|
390
|
-
expect(node.raw(GRIFFEL_DECLARATOR_LOCATION_RAW)).toEqual({
|
|
391
|
-
start: { line: 4, column: 47, index: 99 },
|
|
392
|
-
end: { line: 8, column: 1, index: 174 },
|
|
393
|
-
});
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
if (declarator === 'useResetStyles2') {
|
|
397
|
-
expect(node.raw(GRIFFEL_DECLARATOR_LOCATION_RAW)).toEqual({
|
|
398
|
-
start: { line: 9, column: 47, index: 224 },
|
|
399
|
-
end: { line: 11, column: 1, index: 243 },
|
|
400
|
-
});
|
|
401
|
-
}
|
|
402
|
-
});
|
|
403
|
-
});
|
|
404
|
-
|
|
405
|
-
it('should map style locations to makeResetStyles call when it is not called with object expressions', () => {
|
|
406
|
-
const fixture = `
|
|
407
|
-
import { makeResetStyles } from "@griffel/react";
|
|
408
|
-
|
|
409
|
-
const styles = {
|
|
410
|
-
color: "red",
|
|
411
|
-
backgroundColor: "green",
|
|
412
|
-
};
|
|
413
|
-
|
|
414
|
-
export const useResetStyles = makeResetStyles(styles);
|
|
415
|
-
`;
|
|
416
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
417
|
-
|
|
418
|
-
expect(root.toString()).toMatchInlineSnapshot(`".rbe9p1m{color:red;background-color:green;}"`);
|
|
419
|
-
|
|
420
|
-
root.walk(node => {
|
|
421
|
-
const declarator = node.raw(GRIFFEL_DECLARATOR_RAW);
|
|
422
|
-
expect(declarator).toEqual('useResetStyles');
|
|
423
|
-
expect(node.raw(GRIFFEL_DECLARATOR_LOCATION_RAW)).toEqual({
|
|
424
|
-
start: { line: 9, column: 30, index: 147 },
|
|
425
|
-
end: { line: 9, column: 53, index: 170 },
|
|
426
|
-
});
|
|
427
|
-
});
|
|
428
|
-
});
|
|
429
|
-
|
|
430
|
-
it('should hold original source in document raw field', () => {
|
|
431
|
-
const fixture = `
|
|
432
|
-
import { makeResetStyles } from '@griffel/react';
|
|
433
|
-
|
|
434
|
-
export const useResetStyles = makeResetStyles({
|
|
435
|
-
color: 'red',
|
|
436
|
-
backgroundColor: 'green',
|
|
437
|
-
})
|
|
438
|
-
`;
|
|
439
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
440
|
-
expect(root.raw(GRIFFEL_SRC_RAW)).toEqual(fixture);
|
|
441
|
-
});
|
|
442
|
-
|
|
443
|
-
it('should handle multiple declarators', () => {
|
|
444
|
-
const fixture = `
|
|
445
|
-
import { makeResetStyles } from '@griffel/react';
|
|
446
|
-
|
|
447
|
-
export const useResetStyles1 = makeResetStyles({
|
|
448
|
-
color: 'red',
|
|
449
|
-
backgroundColor: 'green',
|
|
450
|
-
})
|
|
451
|
-
|
|
452
|
-
export const useResetStyles2 = makeResetStyles({
|
|
453
|
-
color: 'blue',
|
|
454
|
-
backgroundColor: 'yellow',
|
|
455
|
-
})
|
|
456
|
-
`;
|
|
457
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
458
|
-
|
|
459
|
-
expect(root.toString()).toMatchInlineSnapshot(`
|
|
460
|
-
".rbe9p1m{color:red;background-color:green;}
|
|
461
|
-
.r1j8igii{color:blue;background-color:yellow;}"
|
|
462
|
-
`);
|
|
463
|
-
|
|
464
|
-
root.walk(node => {
|
|
465
|
-
const declarator = node.raw(GRIFFEL_DECLARATOR_RAW);
|
|
466
|
-
expect(['useResetStyles1', 'useResetStyles2']).toContain(declarator);
|
|
467
|
-
|
|
468
|
-
if (declarator === 'useResetStyles1') {
|
|
469
|
-
expect(node.raw(GRIFFEL_DECLARATOR_LOCATION_RAW)).toEqual({
|
|
470
|
-
start: { line: 4, column: 47, index: 99 },
|
|
471
|
-
end: { line: 7, column: 1, index: 146 },
|
|
472
|
-
});
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
if (declarator === 'useResetStyles2') {
|
|
476
|
-
expect(node.raw(GRIFFEL_DECLARATOR_LOCATION_RAW)).toEqual({
|
|
477
|
-
start: { line: 9, column: 47, index: 196 },
|
|
478
|
-
end: { line: 12, column: 1, index: 245 },
|
|
479
|
-
});
|
|
480
|
-
}
|
|
481
|
-
});
|
|
482
|
-
});
|
|
483
|
-
|
|
484
|
-
it('should transform disable comment directive to stylelint disable', () => {
|
|
485
|
-
const fixture = `
|
|
486
|
-
import { makeResetStyles } from '@griffel/react';
|
|
487
|
-
|
|
488
|
-
// griffel-csslint-disable foo
|
|
489
|
-
// griffel-csslint-disable bar
|
|
490
|
-
export const useResetStyles = makeResetStyles({
|
|
491
|
-
color: 'pink',
|
|
492
|
-
backgroundColor: 'magenta',
|
|
493
|
-
})
|
|
494
|
-
`;
|
|
495
|
-
const root = parse(fixture, { from: 'fixture.styles.ts' });
|
|
496
|
-
expect(root.toString()).toMatchInlineSnapshot(
|
|
497
|
-
`".r70kha3{color:pink;background-color:magenta;} /* stylelint-disable-line foo,bar */"`,
|
|
498
|
-
);
|
|
499
|
-
});
|
|
500
|
-
});
|
|
501
|
-
});
|
package/src/parse.ts
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import * as postcss from 'postcss';
|
|
2
|
-
import transformSync from './transform-sync';
|
|
3
|
-
import {
|
|
4
|
-
GRIFFEL_DECLARATOR_LOCATION_RAW,
|
|
5
|
-
GRIFFEL_DECLARATOR_RAW,
|
|
6
|
-
GRIFFEL_SLOT_LOCATION_RAW,
|
|
7
|
-
GRIFFEL_SLOT_RAW,
|
|
8
|
-
GRIFFEL_SRC_RAW,
|
|
9
|
-
} from './constants';
|
|
10
|
-
import type { BabelPluginOptions } from '@griffel/babel-preset';
|
|
11
|
-
import type { CommentDirective } from './location-preset';
|
|
12
|
-
import * as os from 'os';
|
|
13
|
-
|
|
14
|
-
export type PostCSSParserOptions = Pick<postcss.ProcessOptions<postcss.Document | postcss.Root>, 'from' | 'map'>;
|
|
15
|
-
|
|
16
|
-
export interface ParserOptions extends Pick<PostCSSParserOptions, 'from'>, BabelPluginOptions {
|
|
17
|
-
/**
|
|
18
|
-
* Throws error when griffel parsing fails
|
|
19
|
-
* @default false
|
|
20
|
-
*/
|
|
21
|
-
silenceParseErrors?: boolean;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Generates CSS rules from a JavaScript file. Each slot in `makeStyles` and each `makeResetStyles` will be one line in the output.
|
|
26
|
-
* It returns a PostCSS AST that parses the generated CSS rules. For each node in AST, attaches information about its related slot location from the original js file.
|
|
27
|
-
*/
|
|
28
|
-
export const parse = (css: string | { toString(): string }, opts?: ParserOptions) => {
|
|
29
|
-
const { from: filename = 'postcss-syntax.styles.ts', silenceParseErrors = false } = opts ?? {};
|
|
30
|
-
const griffelPluginOptions = extractGriffelBabelPluginOptions(opts);
|
|
31
|
-
const code = css.toString();
|
|
32
|
-
|
|
33
|
-
const cssRuleSlotNames: string[] = [];
|
|
34
|
-
const cssRules: string[] = [];
|
|
35
|
-
|
|
36
|
-
const parseResult = parseGriffelStyles(code, filename, griffelPluginOptions, silenceParseErrors);
|
|
37
|
-
if (!parseResult) {
|
|
38
|
-
const root = postcss.parse(`/* Failed to parse griffel styles: ${filename} */`, { from: filename });
|
|
39
|
-
root.raws[GRIFFEL_SRC_RAW] = code;
|
|
40
|
-
return root;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const {
|
|
44
|
-
cssEntries,
|
|
45
|
-
cssResetEntries,
|
|
46
|
-
callExpressionLocations,
|
|
47
|
-
resetLocations,
|
|
48
|
-
locations,
|
|
49
|
-
commentDirectives,
|
|
50
|
-
resetCommentDirectives,
|
|
51
|
-
} = parseResult;
|
|
52
|
-
|
|
53
|
-
Object.entries(cssEntries).forEach(([declarator, slots]) => {
|
|
54
|
-
Object.entries(slots).forEach(([slot, rules]) => {
|
|
55
|
-
cssRuleSlotNames.push(`${declarator} ${slot}`);
|
|
56
|
-
let cssRule = rules.join('').replace(new RegExp(os.EOL, 'g'), ' ');
|
|
57
|
-
|
|
58
|
-
const ignoredRules = getIgnoredRulesFromDirectives(commentDirectives[declarator]?.[slot] ?? []);
|
|
59
|
-
if (ignoredRules.length) {
|
|
60
|
-
const stylelintIgnore = `/* stylelint-disable-line ${ignoredRules.join(',')} */`;
|
|
61
|
-
cssRule = `${cssRule} ${stylelintIgnore}`;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
cssRules.push(cssRule);
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
Object.entries(cssResetEntries).forEach(([declarator, resetRules]) => {
|
|
69
|
-
cssRuleSlotNames.push(`${declarator}`);
|
|
70
|
-
const ignoredRules = getIgnoredRulesFromDirectives(resetCommentDirectives[declarator] ?? []);
|
|
71
|
-
let cssRule = resetRules.join('').replace(new RegExp(os.EOL, 'g'), ' ');
|
|
72
|
-
|
|
73
|
-
if (ignoredRules.length) {
|
|
74
|
-
const stylelintIgnore = `/* stylelint-disable-line ${ignoredRules.join(',')} */`;
|
|
75
|
-
cssRule = `${cssRule} ${stylelintIgnore}`;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
cssRules.push(cssRule);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
const root = postcss.parse(cssRules.join('\n'), { from: filename });
|
|
82
|
-
root.walk(node => {
|
|
83
|
-
if (!node.source || node.source.start === undefined) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const [declarator, slot] = cssRuleSlotNames[node.source.start.line - 1].split(' ');
|
|
87
|
-
node.raws[GRIFFEL_DECLARATOR_RAW] = declarator;
|
|
88
|
-
if (slot) {
|
|
89
|
-
node.raws[GRIFFEL_SLOT_RAW] = slot;
|
|
90
|
-
node.raws[GRIFFEL_SLOT_LOCATION_RAW] = locations[declarator]?.[slot] ?? callExpressionLocations[declarator];
|
|
91
|
-
} else {
|
|
92
|
-
node.raws[GRIFFEL_DECLARATOR_LOCATION_RAW] = resetLocations[declarator] ?? callExpressionLocations[declarator];
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
root.raws[GRIFFEL_SRC_RAW] = css;
|
|
97
|
-
return root;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
function parseGriffelStyles(
|
|
101
|
-
code: string,
|
|
102
|
-
filename: string,
|
|
103
|
-
pluginOpts: BabelPluginOptions,
|
|
104
|
-
silenceParseErrors: boolean,
|
|
105
|
-
) {
|
|
106
|
-
try {
|
|
107
|
-
const { metadata } = transformSync(code, {
|
|
108
|
-
filename,
|
|
109
|
-
pluginOptions: {
|
|
110
|
-
...pluginOpts,
|
|
111
|
-
generateMetadata: true,
|
|
112
|
-
},
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
return metadata;
|
|
116
|
-
} catch (error) {
|
|
117
|
-
// eslint-disable-next-line no-console
|
|
118
|
-
console.warn('Failed to parse Griffel styles');
|
|
119
|
-
// eslint-disable-next-line no-console
|
|
120
|
-
console.warn(error);
|
|
121
|
-
if (silenceParseErrors) {
|
|
122
|
-
return null;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
throw error;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function getIgnoredRulesFromDirectives(commentDirectives: CommentDirective[]) {
|
|
130
|
-
return commentDirectives
|
|
131
|
-
.filter(([directive]) => directive === 'griffel-csslint-disable')
|
|
132
|
-
.map(([_, rulename]) => rulename);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const extractGriffelBabelPluginOptions = (opts: ParserOptions = {}) => {
|
|
136
|
-
const { babelOptions, evaluationRules, generateMetadata, modules } = opts;
|
|
137
|
-
const babelPluginOptions: BabelPluginOptions = {};
|
|
138
|
-
if (babelOptions) {
|
|
139
|
-
babelPluginOptions.babelOptions = babelOptions;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (evaluationRules) {
|
|
143
|
-
babelPluginOptions.evaluationRules = evaluationRules;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (generateMetadata) {
|
|
147
|
-
babelPluginOptions.generateMetadata = generateMetadata;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
if (modules) {
|
|
151
|
-
babelPluginOptions.modules = modules;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
return babelPluginOptions;
|
|
155
|
-
};
|
package/src/stringify.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type * as postcss from 'postcss';
|
|
2
|
-
import { GRIFFEL_SRC_RAW } from './constants';
|
|
3
|
-
|
|
4
|
-
export const stringify: postcss.Stringifier = root => {
|
|
5
|
-
const originalSource = root.raw(GRIFFEL_SRC_RAW);
|
|
6
|
-
if (originalSource) {
|
|
7
|
-
return originalSource;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
// TODO maybe we could generate some default Griffel file with all styles in one slot
|
|
11
|
-
throw new Error('Griffel syntax stringifier can only stringify AST parsed with the custom syntax');
|
|
12
|
-
};
|