|
| 1 | +// Copyright 2024 Google Inc. Use of this source code is governed by an |
| 2 | +// MIT-style license that can be found in the LICENSE file or at |
| 3 | +// https://opensource.org/licenses/MIT. |
| 4 | + |
| 5 | +// Used in TypeDoc |
| 6 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 7 | +import type * as postcss from 'postcss'; |
| 8 | + |
| 9 | +// Used in TypeDoc |
| 10 | +// eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 11 | +import type {Interpolation} from './interpolation'; |
| 12 | + |
| 13 | +/** |
| 14 | + * A Sass AST container. While this tries to maintain the general shape of the |
| 15 | + * {@link postcss.Container} interface, it's more broadly used to contain |
| 16 | + * other node types (and even strings in the case of {@link Interpolation}. |
| 17 | + * |
| 18 | + * @typeParam Child - The type of child nodes that this container can contain. |
| 19 | + * @typeParam NewChild - The type of values that can be passed in to create one |
| 20 | + * or more new child nodes for this container. |
| 21 | + */ |
| 22 | +export interface Container<Child, NewChild> { |
| 23 | + /** |
| 24 | + * The nodes in this container. |
| 25 | + * |
| 26 | + * This shouldn't be modified directly; instead, the various methods defined |
| 27 | + * in {@link Container} should be used to modify it. |
| 28 | + */ |
| 29 | + get nodes(): ReadonlyArray<Child>; |
| 30 | + |
| 31 | + /** Inserts new nodes at the end of this interpolation. */ |
| 32 | + append(...nodes: NewChild[]): this; |
| 33 | + |
| 34 | + /** |
| 35 | + * Iterates through {@link nodes}, calling `callback` for each child. |
| 36 | + * |
| 37 | + * Returning `false` in the callback will break iteration. |
| 38 | + * |
| 39 | + * Unlike a `for` loop or `Array#forEach`, this iterator is safe to use while |
| 40 | + * modifying the interpolation's children. |
| 41 | + * |
| 42 | + * @param callback The iterator callback, which is passed each child |
| 43 | + * @return Returns `false` if any call to `callback` returned false |
| 44 | + */ |
| 45 | + each( |
| 46 | + callback: (node: Child, index: number) => false | void, |
| 47 | + ): false | undefined; |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns `true` if {@link condition} returns `true` for all of the |
| 51 | + * container’s children. |
| 52 | + */ |
| 53 | + every( |
| 54 | + condition: ( |
| 55 | + node: Child, |
| 56 | + index: number, |
| 57 | + nodes: ReadonlyArray<Child>, |
| 58 | + ) => boolean, |
| 59 | + ): boolean; |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the first index of {@link child} in {@link nodes}. |
| 63 | + * |
| 64 | + * If {@link child} is a number, returns it as-is. |
| 65 | + */ |
| 66 | + index(child: Child | number): number; |
| 67 | + |
| 68 | + /** |
| 69 | + * Inserts {@link newNode} immediately after the first occurance of |
| 70 | + * {@link oldNode} in {@link nodes}. |
| 71 | + * |
| 72 | + * If {@link oldNode} is a number, inserts {@link newNode} immediately after |
| 73 | + * that index instead. |
| 74 | + */ |
| 75 | + insertAfter(oldNode: Child | number, newNode: NewChild): this; |
| 76 | + |
| 77 | + /** |
| 78 | + * Inserts {@link newNode} immediately before the first occurance of |
| 79 | + * {@link oldNode} in {@link nodes}. |
| 80 | + * |
| 81 | + * If {@link oldNode} is a number, inserts {@link newNode} at that index |
| 82 | + * instead. |
| 83 | + */ |
| 84 | + insertBefore(oldNode: Child | number, newNode: NewChild): this; |
| 85 | + |
| 86 | + /** Inserts {@link nodes} at the beginning of the container. */ |
| 87 | + prepend(...nodes: NewChild[]): this; |
| 88 | + |
| 89 | + /** Adds {@link child} to the end of this interpolation. */ |
| 90 | + push(child: Child): this; |
| 91 | + |
| 92 | + /** |
| 93 | + * Removes all {@link nodes} from this container and cleans their {@link |
| 94 | + * Node.parent} properties. |
| 95 | + */ |
| 96 | + removeAll(): this; |
| 97 | + |
| 98 | + /** |
| 99 | + * Removes the first occurance of {@link child} from the container and cleans |
| 100 | + * the parent properties from the node and its children. |
| 101 | + * |
| 102 | + * If {@link child} is a number, removes the child at that index. |
| 103 | + */ |
| 104 | + removeChild(child: Child | number): this; |
| 105 | + |
| 106 | + /** |
| 107 | + * Returns `true` if {@link condition} returns `true` for (at least) one of |
| 108 | + * the container’s children. |
| 109 | + */ |
| 110 | + some( |
| 111 | + condition: ( |
| 112 | + node: Child, |
| 113 | + index: number, |
| 114 | + nodes: ReadonlyArray<Child>, |
| 115 | + ) => boolean, |
| 116 | + ): boolean; |
| 117 | + |
| 118 | + /** The first node in {@link nodes}. */ |
| 119 | + get first(): Child | undefined; |
| 120 | + |
| 121 | + /** |
| 122 | + * The container’s last child. |
| 123 | + * |
| 124 | + * ```js |
| 125 | + * rule.last === rule.nodes[rule.nodes.length - 1] |
| 126 | + * ``` |
| 127 | + */ |
| 128 | + get last(): Child | undefined; |
| 129 | +} |
0 commit comments