Skip to content

Commit

Permalink
Catch more JS parsing errors. Fixes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
hudochenkov committed Dec 29, 2023
1 parent d0505e7 commit de5b7f9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion lib/__tests__/parseJs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
61 changes: 31 additions & 30 deletions lib/parseJs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit de5b7f9

Please sign in to comment.