Skip to content

Commit

Permalink
fix(postcss-syntax): Silenced parse result should be stringifiable (#643
Browse files Browse the repository at this point in the history
)

* fix(postcss-syntax): Silenced parse result should be stringifiable

If a result is silenced it should still be usable by the syntax
`stringify`. This PR adds the `GRIFFEL_SRC_RAW` property to the root
make that possible.

* Change files

* fix lint
  • Loading branch information
ling1726 authored Feb 10, 2025
1 parent 15af085 commit b93f281
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix(postcss-syntax): Silenced parse result should be stringifiable",
"packageName": "@griffel/postcss-syntax",
"email": "[email protected]",
"dependentChangeType": "patch"
}
3 changes: 3 additions & 0 deletions packages/postcss-syntax/src/parse.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type postcss from 'postcss';
import {
GRIFFEL_DECLARATOR_LOCATION_RAW,
GRIFFEL_DECLARATOR_RAW,
Expand All @@ -6,6 +7,7 @@ import {
GRIFFEL_SRC_RAW,
} from './constants';
import { parse } from './parse';
import { stringify } from './stringify';

describe('parse', () => {
describe('makeStyles', () => {
Expand Down Expand Up @@ -287,6 +289,7 @@ export const useStyles = makeStyles({
`;
const root = parse(fixture, { from: 'fixture.styles.ts', silenceParseErrors: true });
expect(root.toString()).toMatchInlineSnapshot(`"/* Failed to parse griffel styles: fixture.styles.ts */"`);
expect(() => stringify(root, {} as postcss.Builder)).not.toThrow();
});

it('should throw on failure to parse JS code', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/postcss-syntax/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export const parse = (css: string | { toString(): string }, opts?: ParserOptions

const parseResult = parseGriffelStyles(code, filename, griffelPluginOptions, silenceParseErrors);
if (!parseResult) {
return postcss.parse(`/* Failed to parse griffel styles: ${filename} */`, { from: filename });
const root = postcss.parse(`/* Failed to parse griffel styles: ${filename} */`, { from: filename });
root.raws[GRIFFEL_SRC_RAW] = code;
return root;
}

const {
Expand Down

0 comments on commit b93f281

Please sign in to comment.