Skip to content

Commit

Permalink
fix: refine script parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ony3000 committed Dec 4, 2024
1 parent 9c33ade commit 04fa728
Showing 1 changed file with 69 additions and 25 deletions.
94 changes: 69 additions & 25 deletions src/packages/core-parts/finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1002,40 +1002,84 @@ export function findTargetClassNameNodesForHtml(
value: z.string(),
}),
),
attrs: z.array(
z.object({
name: z.string(),
value: z.unknown(),
}),
),
}),
) &&
node.name === 'script'
) {
// Note: In fact, the script element is not a `keywordStartingNode`, but it is considered a kind of safe list to maintain the `classNameNode`s obtained from the code inside the element.
keywordStartingNodes.push(currentASTNode);

const textNodeInScript = node.children.at(0);

if (addon.parseTypescript && textNodeInScript) {
const openingTagEndingLineIndex = node.startSourceSpan.end.line;
const openingTagEndingOffset = node.startSourceSpan.end.offset;
if (node.attrs.find((attr) => attr.name === 'lang' && attr.value === 'ts')) {
// Note: In fact, the script element is not a `keywordStartingNode`, but it is considered a kind of safe list to maintain the `classNameNode`s obtained from the code inside the element.
keywordStartingNodes.push(currentASTNode);

const typescriptAst = addon.parseTypescript(textNodeInScript.value, {
...options,
parser: 'typescript',
});
const targetClassNameNodesInScript = findTargetClassNameNodes(
typescriptAst,
options,
).map<ClassNameNode>((classNameNode) => {
const [classNameNodeRangeStart, classNameNodeRangeEnd] = classNameNode.range;
if (addon.parseTypescript && textNodeInScript) {
const openingTagEndingLineIndex = node.startSourceSpan.end.line;
const openingTagEndingOffset = node.startSourceSpan.end.offset;

return {
...classNameNode,
range: [
classNameNodeRangeStart + openingTagEndingOffset,
classNameNodeRangeEnd + openingTagEndingOffset,
],
startLineIndex: classNameNode.startLineIndex + openingTagEndingLineIndex,
};
});
const typescriptAst = addon.parseTypescript(textNodeInScript.value, {
...options,
parser: 'typescript',
});
const targetClassNameNodesInScript = findTargetClassNameNodes(
typescriptAst,
options,
).map<ClassNameNode>((classNameNode) => {
const [classNameNodeRangeStart, classNameNodeRangeEnd] = classNameNode.range;

classNameNodes.push(...targetClassNameNodesInScript);
return {
...classNameNode,
range: [
classNameNodeRangeStart + openingTagEndingOffset,
classNameNodeRangeEnd + openingTagEndingOffset,
],
startLineIndex: classNameNode.startLineIndex + openingTagEndingLineIndex,
};
});

classNameNodes.push(...targetClassNameNodesInScript);
}
} else if (
node.attrs.find(
(attr) =>
attr.name === 'type' && (attr.value === '' || attr.value === 'text/javascript'),
) ||
!node.attrs.find((attr) => attr.name === 'type')
) {
// Note: In fact, the script element is not a `keywordStartingNode`, but it is considered a kind of safe list to maintain the `classNameNode`s obtained from the code inside the element.
keywordStartingNodes.push(currentASTNode);

if (addon.parseBabel && textNodeInScript) {
const openingTagEndingLineIndex = node.startSourceSpan.end.line;
const openingTagEndingOffset = node.startSourceSpan.end.offset;

const babelAst = addon.parseBabel(textNodeInScript.value, {
...options,
parser: 'babel',
});
const targetClassNameNodesInScript = findTargetClassNameNodes(
babelAst,
options,
).map<ClassNameNode>((classNameNode) => {
const [classNameNodeRangeStart, classNameNodeRangeEnd] = classNameNode.range;

return {
...classNameNode,
range: [
classNameNodeRangeStart + openingTagEndingOffset,
classNameNodeRangeEnd + openingTagEndingOffset,
],
startLineIndex: classNameNode.startLineIndex + openingTagEndingLineIndex,
};
});

classNameNodes.push(...targetClassNameNodesInScript);
}
}
}
break;
Expand Down

0 comments on commit 04fa728

Please sign in to comment.