Skip to content

Commit b42fc52

Browse files
committed
docs: update docs for parser
1 parent 8a74d77 commit b42fc52

4 files changed

Lines changed: 18 additions & 5 deletions

File tree

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ export const transform: Transform = (fileInfo, api) => {
7272
// Implement the transform.
7373
// See below for more details on the API.
7474
};
75+
76+
// Optionally defined a named `parser` export to configure the PostCSS parser.
77+
// Docs: https://postcss.org/api/#parser
78+
// export const parser = ...;
7579
```
7680

7781
## API
@@ -107,6 +111,7 @@ It's an object with helpers provided by `css-codemod` to perform transformations
107111
// transform.ts
108112

109113
import { Transform } from 'css-codemod';
114+
import { parse, stringify } from 'postcss-scss';
110115

111116
export const transform: Transform = (fileInfo, api) => {
112117
// Convert the file source into an AST using the provided helper.
@@ -129,8 +134,16 @@ export const transform: Transform = (fileInfo, api) => {
129134

130135
// Convert the mutated AST back into a string.
131136
// Since a string is returned this will be written back to the file.
132-
return root.toString();
137+
// Note: in this example the `postcss-scss` package is used to add
138+
// SCSS syntax support. The stringifier is passed when we call `toString` to
139+
// re-output valid SCSS syntax.
140+
return root.toString(stringify);
133141
};
142+
143+
// Note: in this example the `postcss-scss` package is used to add SCSS syntax support.
144+
// This configures PostCSS to correctly parse SCSS syntax.
145+
// Docs: https://postcss.org/api/#parser
146+
export const parser = parse;
134147
```
135148

136149
### PostCSS

recipes/rename-scss-variable/input/a.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717

1818
ul {
1919
@include horizontal-list;
20-
}
20+
}

recipes/rename-scss-variable/output/a.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717

1818
ul {
1919
@include horizontal-list;
20-
}
20+
}

src/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const createAPIParse = ({
1313
}): TransformAPI['parse'] => {
1414
const parse: TransformAPI['parse'] = source => {
1515
const result = postcss().process(source, {
16-
// Silence a warning about sourcemaps. Not relevant to this use case.
16+
// Silence warning about sourcemaps. Not relevant to this use case.
1717
from: undefined,
1818
parser,
1919
});
@@ -22,7 +22,7 @@ const createAPIParse = ({
2222
// if one occurs. The error field can then be checked.
2323
const { root } = result;
2424

25-
// Re-surface an PostCSS parsing errors.
25+
// Re-surface any PostCSS parsing errors.
2626
// https://github.com/postcss/postcss/issues/1708
2727
if ((result as any).error) {
2828
throw (result as any).error;

0 commit comments

Comments
 (0)