Skip to content

Commit

Permalink
fix(stylelint): only lint when file can be parsed by babel (reduces n…
Browse files Browse the repository at this point in the history
…oisey errors as developer types) (#1076)

* avoid constant erroring while developer types

* remove console.log

* add changeset

Co-authored-by: Tim Kutnick <tim.kutnick@airbnb.com>
  • Loading branch information
kutnickclose and Tim Kutnick committed Oct 13, 2022
1 parent ded33ab commit 4c2efaa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-swans-unite.md
@@ -0,0 +1,5 @@
---
'@linaria/postcss-linaria': patch
---

Only lint when file can be parsed by babel, reduce noisey errors during dev
7 changes: 7 additions & 0 deletions packages/postcss-linaria/__tests__/parse.test.ts
Expand Up @@ -382,4 +382,11 @@ describe('parse', () => {
expect(ast.source!.input.css).toEqual(source);
});
});

it('should return empty document if babel cannot parse file', () => {
const { ast } = createTestAst(`this is not valid source code for a file`);
expect(ast.type).toEqual('document');
expect(ast.raws).toEqual({});
expect(ast.nodes.length).toEqual(0);
});
});
18 changes: 13 additions & 5 deletions packages/postcss-linaria/src/parse.ts
Expand Up @@ -107,11 +107,19 @@ export const parse: Parser<Root | Document> = (
): Root | Document => {
const doc = new Document();
const sourceAsString = source.toString();
const ast = babelParse(sourceAsString, {
sourceType: 'unambiguous',
plugins: ['typescript', 'jsx'],
ranges: true,
});

// avoid error spam (and vscode error toasts) if babel can't parse doc
// allows user to type new code without constant warnings
let ast;
try {
ast = babelParse(sourceAsString, {
sourceType: 'unambiguous',
plugins: ['typescript', 'jsx'],
ranges: true,
});
} catch {
return doc;
}
const extractedStyles = new Set<TaggedTemplateExpression>();

traverse(ast, {
Expand Down

0 comments on commit 4c2efaa

Please sign in to comment.