Skip to content

Commit 832cbdc

Browse files
committed
Fixed Root.parent type
1 parent dc08b51 commit 832cbdc

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/ast/style.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { Node, ChildProps, Container } from "postcss";
1+
import type { Node, ChildProps, Container, Root } from "postcss";
22
import type { Locations } from "./common";
3+
import type { SvelteStyleElement } from "./html";
34

45
type ESLintCompatiblePostCSSContainer<
56
PostCSSNode extends Node,
@@ -64,10 +65,12 @@ type ESLintCompatiblePostCSSContainer<
6465
};
6566

6667
export type ESLintCompatiblePostCSSNode<PostCSSNode extends Node> =
68+
// The following hack makes the `type` property work for type narrowing, see microsoft/TypeScript#53887.
6769
PostCSSNode extends any
6870
? Locations &
6971
Omit<
7072
PostCSSNode,
73+
| "parent"
7174
| "type"
7275
| "each"
7376
| "every"
@@ -85,5 +88,12 @@ export type ESLintCompatiblePostCSSNode<PostCSSNode extends Node> =
8588
type: `SvelteStyle-${PostCSSNode["type"]}`;
8689
} & (PostCSSNode extends Container<infer Child>
8790
? ESLintCompatiblePostCSSContainer<PostCSSNode, Child>
88-
: unknown)
91+
: unknown) &
92+
(PostCSSNode extends Root
93+
? {
94+
parent: SvelteStyleElement;
95+
}
96+
: {
97+
parent: PostCSSNode["parent"];
98+
})
8999
: never;

src/parser/converts/root.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { Context } from "../../context";
1111
import { convertChildren, extractElementTags } from "./element";
1212
import { convertAttributeTokens } from "./attr";
1313
import type { Scope } from "eslint-scope";
14-
import type { Document, Node, Parser, Root } from "postcss";
14+
import type { Node, Parser, Root } from "postcss";
1515
import postcss from "postcss";
1616
import { parse as SCSSparse } from "postcss-scss";
1717
import type { ESLintCompatiblePostCSSNode } from "../../ast/style";
@@ -149,15 +149,19 @@ export function convertSvelteRoot(
149149
style.body = parseFn(styleCode, {
150150
from: ctx.parserOptions.filePath,
151151
}) as unknown as ESLintCompatiblePostCSSNode<Root>;
152-
convertPostCSSNodeToESLintNode(style.body, style.loc, contentRange);
152+
convertPostCSSNodeToESLintNode<Root>(
153+
style.body,
154+
style.loc,
155+
contentRange
156+
);
153157
// Fix Root loc
154158
style.body.loc.start.column += style.startTag.loc.end.column;
155159
style.body.loc.end.column -=
156160
style.endTag.loc.end.column - style.endTag.loc.start.column;
157161
style.body?.walk((node) =>
158162
convertPostCSSNodeToESLintNode(node, style.loc, contentRange)
159163
);
160-
style.body.parent = style as unknown as Document;
164+
style.body.parent = style;
161165
delete style.body.source?.input.file;
162166
}
163167
ctx.addToken("HTMLText", contentRange);

0 commit comments

Comments
 (0)