Skip to content

Commit

Permalink
fix(postcss-syntax): parse styles with newlines (#629)
Browse files Browse the repository at this point in the history
* wip

* Change files

* match windows newline
  • Loading branch information
YuanboXue-Amber authored Dec 19, 2024
1 parent 72813e4 commit 0379b14
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: parse styles with new lines",
"packageName": "@griffel/postcss-syntax",
"email": "[email protected]",
"dependentChangeType": "patch"
}
74 changes: 74 additions & 0 deletions packages/postcss-syntax/src/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,47 @@ export const useStyles = makeStyles({
});
});

it('should map style locations to slots when styles contains new lines', () => {
const fixture = `
import { makeStyles } from "@griffel/react";
export const useStyles = makeStyles({
slot1: {
background: \`linear-gradient(#e66465,
#9198e5)\`,
},
slot2: {
color: "blue",
},
});
`;
const root = parse(fixture, { from: 'fixture.styles.ts' });

expect(root.toString()).toMatchInlineSnapshot(`
".f1qmhkic{background:linear-gradient(#e66465, #9198e5);}
.f163i14w{color:blue;}"
`);

root.walk(node => {
const slot = node.raw(GRIFFEL_SLOT_RAW);
expect(['slot1', 'slot2']).toContain(slot);

if (slot === 'slot1') {
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
start: { line: 5, column: 2, index: 87 },
end: { line: 8, column: 3, index: 159 },
});
}
if (slot === 'slot2') {
expect(node.raw(GRIFFEL_SLOT_LOCATION_RAW)).toEqual({
start: { line: 10, column: 2, index: 164 },
end: { line: 12, column: 3, index: 195 },
});
}
});
});

it('should hold original source in document raw field', () => {
const fixture = `
import { makeStyles } from '@griffel/react';
Expand Down Expand Up @@ -260,6 +301,39 @@ export const useResetStyles = makeResetStyles({
});
});

it('should map style locations to reset styles when styles contains new lines', () => {
const fixture = `
import { makeResetStyles } from "@griffel/react";
export const useResetStyles1 = makeResetStyles({
color: "red",
background: \`linear-gradient(#e66465,
#9198e5)\`,
});
export const useResetStyles2 = makeResetStyles({
color: "red",
});
`;
const root = parse(fixture, { from: 'fixture.styles.ts' });

root.walk(node => {
const declarator = node.raw(GRIFFEL_DECLARATOR_RAW);
if (declarator === 'useResetStyles1') {
expect(node.raw(GRIFFEL_DECLARATOR_LOCATION_RAW)).toEqual({
start: { line: 4, column: 47, index: 99 },
end: { line: 8, column: 1, index: 174 },
});
}

if (declarator === 'useResetStyles2') {
expect(node.raw(GRIFFEL_DECLARATOR_LOCATION_RAW)).toEqual({
start: { line: 9, column: 47, index: 224 },
end: { line: 11, column: 1, index: 243 },
});
}
});
});

it('should hold original source in document raw field', () => {
const fixture = `
import { makeResetStyles } from '@griffel/react';
Expand Down
5 changes: 3 additions & 2 deletions packages/postcss-syntax/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from './constants';
import type { BabelPluginOptions } from '@griffel/babel-preset';
import type { CommentDirective } from './location-preset';
import * as os from 'os';

export type PostCSSParserOptions = Pick<postcss.ProcessOptions<postcss.Document | postcss.Root>, 'from' | 'map'>;

Expand Down Expand Up @@ -39,7 +40,7 @@ export const parse = (css: string | { toString(): string }, opts?: ParserOptions
Object.entries(cssEntries).forEach(([declarator, slots]) => {
Object.entries(slots).forEach(([slot, rules]) => {
cssRuleSlotNames.push(`${declarator} ${slot}`);
let cssRule = rules.join('');
let cssRule = rules.join('').replace(new RegExp(os.EOL, 'g'), ' ');

const ignoredRules = getIgnoredRulesFromDirectives(commentDirectives[declarator]?.[slot] ?? []);
if (ignoredRules.length) {
Expand All @@ -54,7 +55,7 @@ export const parse = (css: string | { toString(): string }, opts?: ParserOptions
Object.entries(cssResetEntries).forEach(([declarator, resetRules]) => {
cssRuleSlotNames.push(`${declarator}`);
const ignoredRules = getIgnoredRulesFromDirectives(resetCommentDirectives[declarator] ?? []);
let cssRule = resetRules.join('');
let cssRule = resetRules.join('').replace(new RegExp(os.EOL, 'g'), ' ');

if (ignoredRules.length) {
const stylelintIgnore = `/* stylelint-disable-line ${ignoredRules.join(',')} */`;
Expand Down

0 comments on commit 0379b14

Please sign in to comment.