Skip to content

Commit 846120b

Browse files
authored
fix: Types for node generate() function (#56)
1 parent 2f793c9 commit 846120b

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/index.d.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,37 @@ export type GenerateFunction = (ast: AnyCssNode, options?: GenerateOptions) => s
17001700
*/
17011701
export const generate: GenerateFunction;
17021702

1703+
/**
1704+
* Represents the context object passed to a Node's `generate` function.
1705+
* @see https://github.com/csstree/csstree/blob/56afb6dd761149099cd3cdfb0a38e15e8cc0a71a/lib/generator/create.js#L86-L91
1706+
*/
1707+
export interface GeneratorContext {
1708+
/**
1709+
* Tokenizes a string or node.
1710+
* @param value - The value to tokenize.
1711+
*/
1712+
tokenize(value: string): void;
1713+
1714+
/**
1715+
* Processes a child node.
1716+
* @param node - The child node to process.
1717+
*/
1718+
node(node: CssNode): void;
1719+
1720+
/**
1721+
* Processes all children of a node.
1722+
* @param node - The parent node whose children are processed.
1723+
*/
1724+
children(node: CssNode): void;
1725+
1726+
/**
1727+
* Emits a token with a specific type and value.
1728+
* @param type - The type of the token.
1729+
* @param value - The value of the token.
1730+
*/
1731+
token(type: number, value: string): void;
1732+
}
1733+
17031734
// ----------------------------------------------------------
17041735
// Walker
17051736
// ----------------------------------------------------------
@@ -2720,7 +2751,7 @@ interface NodeSyntaxConfig<T extends CssNodeCommon = CssNodeCommon> {
27202751
name: string;
27212752
structure: StructureDefinition;
27222753
parse(this: ParserContext, ...args:Array<unknown>): T;
2723-
generate(this: ParserContext, node: T): void;
2754+
generate(this: GeneratorContext, node: T): void;
27242755
walkContext?: string;
27252756
}
27262757

tests/types/types.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ const customSyntax = csstree.fork({
282282
value: 'hello'
283283
};
284284
},
285-
generate: (node) => {
285+
generate(node: csstree.CssNode) {
286+
this.node(node);
287+
this.children(node);
288+
this.token(1, 'foo');
289+
this.tokenize('foo');
286290
return `custom3: ${node.type}`;
287291
}
288292
},

0 commit comments

Comments
 (0)