From de5b7f90a03c4974b4424b4fe2a6826ee624ce32 Mon Sep 17 00:00:00 2001 From: Aleks Hudochenkov Date: Fri, 29 Dec 2023 12:24:58 +0100 Subject: [PATCH] Catch more JS parsing errors. Fixes #25 --- lib/__tests__/parseJs.test.js | 2 +- lib/parseJs.js | 61 ++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 31 deletions(-) diff --git a/lib/__tests__/parseJs.test.js b/lib/__tests__/parseJs.test.js index 0ef1bd8..65105bb 100644 --- a/lib/__tests__/parseJs.test.js +++ b/lib/__tests__/parseJs.test.js @@ -587,7 +587,7 @@ test('component after a component with interpolation', () => { }); test('does not crash for invalid JavaScript syntax', () => { - let document = parseJs('const Component = styled.div`position: sticky'); + let document = parseJs('const Component = styled.div`position: sticky', { from: 'test.js' }); expect(document).toMatchSnapshot(); }); diff --git a/lib/parseJs.js b/lib/parseJs.js index 8629c19..c3f11d9 100644 --- a/lib/parseJs.js +++ b/lib/parseJs.js @@ -13,41 +13,42 @@ module.exports.parseJs = function parseJs(inputCode, opts) { /** @type {NodeData[]} */ let foundNodes = []; - let sourceFile = ts.createSourceFile( - opts?.from || 'unnamed.ts', - inputCode, - ts.ScriptTarget.Latest, - // true, - // ts.ScriptKind.TSX, - ); - - /** - * Recursively visits the nodes in the AST - * - * @param {ts.Node} node - The current node in the AST. - */ - function visit(node) { - // Check if the node is a TaggedTemplateExpression - if (ts.isTaggedTemplateExpression(node) && isStyledComponent(node)) { - let nodeCssData = getNodeCssData(node, inputCode, sourceFile); - - foundNodes.push(nodeCssData); + try { + let sourceFile = ts.createSourceFile( + opts?.from || 'unnamed.ts', + inputCode, + ts.ScriptTarget.Latest, + // true, + // ts.ScriptKind.TSX, + ); + + /** + * Recursively visits the nodes in the AST + * + * @param {ts.Node} node - The current node in the AST. + */ + // eslint-disable-next-line no-inner-declarations + function visit(node) { + // Check if the node is a TaggedTemplateExpression + if (ts.isTaggedTemplateExpression(node) && isStyledComponent(node)) { + let nodeCssData = getNodeCssData(node, inputCode, sourceFile); + + foundNodes.push(nodeCssData); + } + + // Continue recursion down the tree + ts.forEachChild(node, visit); } - // Continue recursion down the tree - ts.forEachChild(node, visit); - } - - // @ts-expect-error -- parseDiagnostics is not public API. However, TS is crashing or very-very slow if using official way - // https://github.com/microsoft/TypeScript/issues/21940 - let hasParseErrors = sourceFile.parseDiagnostics?.length > 0; + // @ts-expect-error -- parseDiagnostics is not public API. However, TS is crashing or very-very slow if using official way + // https://github.com/microsoft/TypeScript/issues/21940 + let hasParseErrors = sourceFile.parseDiagnostics?.length > 0; - if (!hasParseErrors) { - try { + if (!hasParseErrors) { visit(sourceFile); - } catch (error) { - // Don't show parsing errors for JavaScript/TypeScript, because they are not relevant to CSS. And these errors most likely caught for user by JavaScript tools already } + } catch (error) { + // Don't show parsing errors for JavaScript/TypeScript, because they are not relevant to CSS. And these errors most likely caught for user by JavaScript tools already } return foundNodes;