Skip to content

Commit 97e7d9a

Browse files
Copilotjogibear9988
andcommitted
refactor: simplify identityCompile and remove double type assertion
Address code review feedback: - Simplify identityCompile to directly return originalSource instead of unnecessary offset-based reconstruction - Remove `as unknown as` double assertion in filterEmptyRules Co-authored-by: jogibear9988 <364896+jogibear9988@users.noreply.github.com>
1 parent e26f125 commit 97e7d9a

File tree

1 file changed

+4
-33
lines changed

1 file changed

+4
-33
lines changed

src/stringify/compiler.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -221,43 +221,15 @@ class Compiler {
221221
}
222222

223223
/**
224-
* Identity mode: reconstruct the original CSS using stored source offsets.
225-
* Falls back to beautified output when offsets are not available.
224+
* Identity mode: reconstruct the original CSS using stored source.
225+
* Falls back to beautified output when original source is not available.
226226
*/
227227
private identityCompile(node: CssStylesheetAST): string {
228228
const source = node.stylesheet.originalSource;
229229
if (!source) {
230230
return this.stylesheet(node);
231231
}
232-
233-
const allRules = node.stylesheet.rules;
234-
if (allRules.length === 0) {
235-
return source;
236-
}
237-
238-
// Collect all nodes with valid offsets
239-
const nodesWithOffsets: Array<{
240-
startOffset: number;
241-
endOffset: number;
242-
}> = [];
243-
for (const rule of allRules) {
244-
const pos = (rule as CssCommonPositionAST).position;
245-
if (pos?.start?.offset != null && pos?.end?.offset != null) {
246-
nodesWithOffsets.push({
247-
startOffset: pos.start.offset,
248-
endOffset: pos.end.offset,
249-
});
250-
}
251-
}
252-
253-
if (nodesWithOffsets.length === 0) {
254-
return this.stylesheet(node);
255-
}
256-
257-
// Reconstruct: output everything from start to end of last node,
258-
// then any trailing text.
259-
const lastEnd = nodesWithOffsets[nodesWithOffsets.length - 1].endOffset;
260-
return source.slice(0, lastEnd) + source.slice(lastEnd);
232+
return source;
261233
}
262234

263235
/**
@@ -278,8 +250,7 @@ class Compiler {
278250
}
279251
return rules.filter((rule) => {
280252
if (rule.type === CssTypes.rule) {
281-
const r = rule as unknown as CssRuleAST;
282-
return r.declarations.length > 0;
253+
return (rule as CssRuleAST).declarations.length > 0;
283254
}
284255
return true;
285256
});

0 commit comments

Comments
 (0)