Skip to content

Commit 20d7528

Browse files
authored
fix: handle empty file (#7)
1 parent 0d4c146 commit 20d7528

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/typstToTextlintAst.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ export const convertRawTypstAstStringToObject = (rawTypstAstString: string) => {
6666
);
6767

6868
const parsed = parse(escapedRawTypstAstYamlString);
69+
70+
// Handle empty file case
71+
if (parsed.ast.c === null) {
72+
parsed.ast.c = [];
73+
}
74+
6975
return parsed.ast as TypstAstNode;
7076
};
7177

@@ -164,6 +170,14 @@ export const convertRawTypstAstObjectToTextlintAstObject = (
164170
if (!match) {
165171
if (c !== undefined) {
166172
// If root node
173+
174+
if (c.length === 0) {
175+
return {
176+
start: { line: 1, column: 0 },
177+
end: { line: 1, column: 0 },
178+
};
179+
}
180+
167181
const rootChildrenStartLocation = extractLocation(c[0].s, c[0].c);
168182
const rootChildrenEndLocation = extractLocation(
169183
c[c.length - 1].s,
@@ -193,7 +207,7 @@ export const convertRawTypstAstObjectToTextlintAstObject = (
193207
const nodeRawText = extractRawSourceByLocation(typstSource, location);
194208
const nodeLength = nodeRawText.length;
195209

196-
if (node.c && node.c.length > 0) {
210+
if (node.c) {
197211
// If TxtParentNode
198212
let childOffset = startOffset;
199213
const whitespaceNodes: TxtTextNode[] = [];
@@ -372,7 +386,7 @@ export const convertRawTypstAstObjectToTextlintAstObject = (
372386
};
373387

374388
// If the source code starts with a single newline, add a Break node before the first node.
375-
if (textlintAstObject.c) {
389+
if (textlintAstObject.c && textlintAstObject.c.length > 0) {
376390
const rootChildrenStartLocation = extractLocation(
377391
textlintAstObject.c[0].s,
378392
textlintAstObject.c[0].c,

test/fixtures/empty-file/input.typ

Whitespace-only changes.

test/fixtures/empty-file/output.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"children": [],
3+
"raw": "",
4+
"range": [0, 0],
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 1,
12+
"column": 0
13+
}
14+
},
15+
"type": "Document"
16+
}

0 commit comments

Comments
 (0)