diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index 7a39b2f9f..8db6ad45a 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -1,48 +1,53 @@ import Container, { ContainerProps } from './container.js' -interface AtRuleRaws extends Record { - /** - * The space symbols before the node. It also stores `*` - * and `_` symbols before the declaration (IE hack). - */ - before?: string +declare namespace AtRule { + export interface AtRuleRaws extends Record { + /** + * The space symbols before the node. It also stores `*` + * and `_` symbols before the declaration (IE hack). + */ + before?: string - /** - * The space symbols after the last child of the node to the end of the node. - */ - after?: string + /** + * The space symbols after the last child of the node to the end of the node. + */ + after?: string - /** - * The space between the at-rule name and its parameters. - */ - afterName?: string + /** + * The space between the at-rule name and its parameters. + */ + afterName?: string - /** - * The symbols between the last parameter and `{` for rules. - */ - between?: string + /** + * The symbols between the last parameter and `{` for rules. + */ + between?: string - /** - * Contains `true` if the last child has an (optional) semicolon. - */ - semicolon?: boolean + /** + * Contains `true` if the last child has an (optional) semicolon. + */ + semicolon?: boolean - /** - * The rule’s selector with comments. - */ - params?: { - value: string - raw: string + /** + * The rule’s selector with comments. + */ + params?: { + value: string + raw: string + } } -} -export interface AtRuleProps extends ContainerProps { - /** Name of the at-rule. */ - name: string - /** Parameters following the name of the at-rule. */ - params?: string | number - /** Information used to generate byte-to-byte equal node string as it was in the origin input. */ - raws?: AtRuleRaws + export interface AtRuleProps extends ContainerProps { + /** Name of the at-rule. */ + name: string + /** Parameters following the name of the at-rule. */ + params?: string | number + /** Information used to generate byte-to-byte equal node string as it was in the origin input. */ + raws?: AtRuleRaws + } + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { AtRule_ as default } } /** @@ -70,10 +75,10 @@ export interface AtRuleProps extends ContainerProps { * media.nodes //=> [] * ``` */ -export default class AtRule extends Container { +declare class AtRule_ extends Container { type: 'atrule' parent: Container | undefined - raws: AtRuleRaws + raws: AtRule.AtRuleRaws /** * The at-rule’s name immediately follows the `@`. @@ -98,9 +103,13 @@ export default class AtRule extends Container { */ params: string - constructor(defaults?: AtRuleProps) - assign(overrides: object | AtRuleProps): this - clone(overrides?: Partial): this - cloneBefore(overrides?: Partial): this - cloneAfter(overrides?: Partial): this + constructor(defaults?: AtRule.AtRuleProps) + assign(overrides: object | AtRule.AtRuleProps): this + clone(overrides?: Partial): this + cloneBefore(overrides?: Partial): this + cloneAfter(overrides?: Partial): this } + +declare class AtRule extends AtRule_ {} + +export = AtRule diff --git a/lib/comment.d.ts b/lib/comment.d.ts index 8eaf3a230..2f00ff7be 100644 --- a/lib/comment.d.ts +++ b/lib/comment.d.ts @@ -1,28 +1,33 @@ import Container from './container.js' import Node, { NodeProps } from './node.js' -interface CommentRaws extends Record { - /** - * The space symbols before the node. - */ - before?: string +declare namespace Comment { + export interface CommentRaws extends Record { + /** + * The space symbols before the node. + */ + before?: string - /** - * The space symbols between `/*` and the comment’s text. - */ - left?: string + /** + * The space symbols between `/*` and the comment’s text. + */ + left?: string - /** - * The space symbols between the comment’s text. - */ - right?: string -} + /** + * The space symbols between the comment’s text. + */ + right?: string + } -export interface CommentProps extends NodeProps { - /** Content of the comment. */ - text: string - /** Information used to generate byte-to-byte equal node string as it was in the origin input. */ - raws?: CommentRaws + export interface CommentProps extends NodeProps { + /** Content of the comment. */ + text: string + /** Information used to generate byte-to-byte equal node string as it was in the origin input. */ + raws?: CommentRaws + } + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Comment_ as default } } /** @@ -38,19 +43,23 @@ export interface CommentProps extends NodeProps { * Comments inside selectors, at-rule parameters, or declaration values * will be stored in the `raws` properties explained above. */ -export default class Comment extends Node { +declare class Comment_ extends Node { type: 'comment' parent: Container | undefined - raws: CommentRaws + raws: Comment.CommentRaws /** * The comment's text. */ text: string - constructor(defaults?: CommentProps) - assign(overrides: object | CommentProps): this - clone(overrides?: Partial): this - cloneBefore(overrides?: Partial): this - cloneAfter(overrides?: Partial): this + constructor(defaults?: Comment.CommentProps) + assign(overrides: object | Comment.CommentProps): this + clone(overrides?: Partial): this + cloneBefore(overrides?: Partial): this + cloneAfter(overrides?: Partial): this } + +declare class Comment extends Comment_ {} + +export = Comment diff --git a/lib/container.d.ts b/lib/container.d.ts index 2b75e372f..32953fe3d 100644 --- a/lib/container.d.ts +++ b/lib/container.d.ts @@ -4,20 +4,25 @@ import Comment from './comment.js' import AtRule from './at-rule.js' import Rule from './rule.js' -interface ValueOptions { - /** - * An array of property names. - */ - props?: string[] +declare namespace Container { + export interface ValueOptions { + /** + * An array of property names. + */ + props?: string[] - /** - * String that’s used to narrow down values and speed up the regexp search. - */ - fast?: string -} + /** + * String that’s used to narrow down values and speed up the regexp search. + */ + fast?: string + } + + export interface ContainerProps extends NodeProps { + nodes?: (ChildNode | ChildProps)[] + } -export interface ContainerProps extends NodeProps { - nodes?: (ChildNode | ChildProps)[] + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Container_ as default } } /** @@ -27,7 +32,7 @@ export interface ContainerProps extends NodeProps { * Note that all containers can store any content. If you write a rule inside * a rule, PostCSS will parse it. */ -export default abstract class Container< +declare abstract class Container_< Child extends Node = ChildNode > extends Node { /** @@ -390,7 +395,7 @@ export default abstract class Container< */ replaceValues( pattern: string | RegExp, - options: ValueOptions, + options: Container.ValueOptions, replaced: string | { (substring: string, ...args: any[]): string } ): this replaceValues( @@ -440,3 +445,7 @@ export default abstract class Container< */ index(child: Child | number): number } + +declare class Container extends Container_ {} + +export = Container diff --git a/lib/css-syntax-error.d.ts b/lib/css-syntax-error.d.ts index 3fd055528..f89b48479 100644 --- a/lib/css-syntax-error.d.ts +++ b/lib/css-syntax-error.d.ts @@ -1,18 +1,23 @@ import { FilePosition } from './input.js' -/** - * A position that is part of a range. - */ -export interface RangePosition { +declare namespace CssSyntaxError { /** - * The line number in the input. + * A position that is part of a range. */ - line: number + export interface RangePosition { + /** + * The line number in the input. + */ + line: number - /** - * The column number in the input. - */ - column: number + /** + * The column number in the input. + */ + column: number + } + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { CssSyntaxError_ as default } } /** @@ -44,7 +49,7 @@ export interface RangePosition { * } * ``` */ -export default class CssSyntaxError { +declare class CssSyntaxError_ { /** * Instantiates a CSS syntax error. Can be instantiated for a single position * or for a range. @@ -59,8 +64,8 @@ export default class CssSyntaxError { */ constructor( message: string, - lineOrStartPos?: number | RangePosition, - columnOrEndPos?: number | RangePosition, + lineOrStartPos?: number | CssSyntaxError.RangePosition, + columnOrEndPos?: number | CssSyntaxError.RangePosition, source?: string, file?: string, plugin?: string @@ -237,3 +242,7 @@ export default class CssSyntaxError { */ showSourceCode(color?: boolean): string } + +declare class CssSyntaxError extends CssSyntaxError_ {} + +export = CssSyntaxError diff --git a/lib/declaration.d.ts b/lib/declaration.d.ts index 4a1fb1ec9..c9984a47a 100644 --- a/lib/declaration.d.ts +++ b/lib/declaration.d.ts @@ -1,41 +1,46 @@ import Container from './container.js' import Node from './node.js' -interface DeclarationRaws extends Record { - /** - * The space symbols before the node. It also stores `*` - * and `_` symbols before the declaration (IE hack). - */ - before?: string +declare namespace Declaration { + export interface DeclarationRaws extends Record { + /** + * The space symbols before the node. It also stores `*` + * and `_` symbols before the declaration (IE hack). + */ + before?: string - /** - * The symbols between the property and value for declarations. - */ - between?: string + /** + * The symbols between the property and value for declarations. + */ + between?: string - /** - * The content of the important statement, if it is not just `!important`. - */ - important?: string + /** + * The content of the important statement, if it is not just `!important`. + */ + important?: string - /** - * Declaration value with comments. - */ - value?: { + /** + * Declaration value with comments. + */ + value?: { + value: string + raw: string + } + } + + export interface DeclarationProps { + /** Name of the declaration. */ + prop: string + /** Value of the declaration. */ value: string - raw: string + /** Whether the declaration has an `!important` annotation. */ + important?: boolean + /** Information used to generate byte-to-byte equal node string as it was in the origin input. */ + raws?: DeclarationRaws } -} -export interface DeclarationProps { - /** Name of the declaration. */ - prop: string - /** Value of the declaration. */ - value: string - /** Whether the declaration has an `!important` annotation. */ - important?: boolean - /** Information used to generate byte-to-byte equal node string as it was in the origin input. */ - raws?: DeclarationRaws + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Declaration_ as default } } /** @@ -55,10 +60,10 @@ export interface DeclarationProps { * decl.toString() //=> ' color: black' * ``` */ -export default class Declaration extends Node { +declare class Declaration_ extends Node { type: 'decl' parent: Container | undefined - raws: DeclarationRaws + raws: Declaration.DeclarationRaws /** * The declaration's property name. @@ -116,9 +121,13 @@ export default class Declaration extends Node { */ variable: boolean - constructor(defaults?: DeclarationProps) - assign(overrides: object | DeclarationProps): this - clone(overrides?: Partial): this - cloneBefore(overrides?: Partial): this - cloneAfter(overrides?: Partial): this + constructor(defaults?: Declaration.DeclarationProps) + assign(overrides: object | Declaration.DeclarationProps): this + clone(overrides?: Partial): this + cloneBefore(overrides?: Partial): this + cloneAfter(overrides?: Partial): this } + +declare class Declaration extends Declaration_ {} + +export = Declaration diff --git a/lib/document.d.ts b/lib/document.d.ts index 28a599ca8..7afb37472 100644 --- a/lib/document.d.ts +++ b/lib/document.d.ts @@ -1,22 +1,24 @@ import Container, { ContainerProps } from './container.js' import { ProcessOptions } from './postcss.js' import Result from './result.js' -import Root, { RootProps } from './root.js' +import Root from './root.js' -export interface DocumentProps extends ContainerProps { - nodes?: Root[] +declare namespace Document { + export interface DocumentProps extends ContainerProps { + nodes?: Root[] - /** - * Information to generate byte-to-byte equal node string as it was - * in the origin input. - * - * Every parser saves its own properties. - */ - raws?: Record -} + /** + * Information to generate byte-to-byte equal node string as it was + * in the origin input. + * + * Every parser saves its own properties. + */ + raws?: Record + } -type ChildNode = Root -type ChildProps = RootProps + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Document_ as default } +} /** * Represents a file and contains all its parsed nodes. @@ -32,11 +34,11 @@ type ChildProps = RootProps * document.nodes.length //=> 2 * ``` */ -export default class Document extends Container { +declare class Document_ extends Container { type: 'document' parent: undefined - constructor(defaults?: DocumentProps) + constructor(defaults?: Document.DocumentProps) /** * Returns a `Result` instance representing the document’s CSS roots. @@ -55,3 +57,7 @@ export default class Document extends Container { */ toResult(options?: ProcessOptions): Result } + +declare class Document extends Document_ {} + +export = Document diff --git a/lib/fromJSON.d.ts b/lib/fromJSON.d.ts index 13b169c66..e1deedbd3 100644 --- a/lib/fromJSON.d.ts +++ b/lib/fromJSON.d.ts @@ -1,5 +1,9 @@ import { JSONHydrator } from './postcss.js' -declare const fromJSON: JSONHydrator +interface FromJSON extends JSONHydrator { + default: FromJSON +} -export default fromJSON +declare const fromJSON: FromJSON + +export = fromJSON diff --git a/lib/input.d.ts b/lib/input.d.ts index 48c904b22..737694338 100644 --- a/lib/input.d.ts +++ b/lib/input.d.ts @@ -1,41 +1,46 @@ import { CssSyntaxError, ProcessOptions } from './postcss.js' import PreviousMap from './previous-map.js' -export interface FilePosition { - /** - * URL for the source file. - */ - url: string - - /** - * Absolute path to the source file. - */ - file?: string - - /** - * Line of inclusive start position in source file. - */ - line: number - - /** - * Column of inclusive start position in source file. - */ - column: number +declare namespace Input { + export interface FilePosition { + /** + * URL for the source file. + */ + url: string + + /** + * Absolute path to the source file. + */ + file?: string + + /** + * Line of inclusive start position in source file. + */ + line: number + + /** + * Column of inclusive start position in source file. + */ + column: number + + /** + * Line of exclusive end position in source file. + */ + endLine?: number + + /** + * Column of exclusive end position in source file. + */ + endColumn?: number - /** - * Line of exclusive end position in source file. - */ - endLine?: number + /** + * Source code. + */ + source?: string + } - /** - * Column of exclusive end position in source file. - */ - endColumn?: number - - /** - * Source code. - */ - source?: string + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Input_ as default } } /** @@ -46,7 +51,7 @@ export interface FilePosition { * const input = root.source.input * ``` */ -export default class Input { +declare class Input_ { /** * Input CSS source. * @@ -139,7 +144,7 @@ export default class Input { column: number, endLine?: number, endColumn?: number - ): FilePosition | false + ): Input.FilePosition | false /** * Converts source offset to line and column. @@ -183,3 +188,7 @@ export default class Input { opts?: { plugin?: CssSyntaxError['plugin'] } ): CssSyntaxError } + +declare class Input extends Input_ {} + +export = Input diff --git a/lib/lazy-result.d.ts b/lib/lazy-result.d.ts index a5142345e..089495c0a 100644 --- a/lib/lazy-result.d.ts +++ b/lib/lazy-result.d.ts @@ -4,6 +4,11 @@ import Processor from './processor.js' import Warning from './warning.js' import Root from './root.js' +declare namespace LazyResult { + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { LazyResult_ as default } +} + /** * A Promise proxy for the result of PostCSS transformations. * @@ -13,7 +18,7 @@ import Root from './root.js' * const lazy = postcss([autoprefixer]).process(css) * ``` */ -export default class LazyResult implements PromiseLike { +declare class LazyResult_ implements PromiseLike { /** * Processes input CSS through synchronous and asynchronous plugins * and calls `onFulfilled` with a Result instance. If a plugin throws @@ -174,3 +179,7 @@ export default class LazyResult implements PromiseLike { */ async(): Promise } + +declare class LazyResult extends LazyResult_ {} + +export = LazyResult diff --git a/lib/list.d.ts b/lib/list.d.ts index 79841edeb..6a56cad62 100644 --- a/lib/list.d.ts +++ b/lib/list.d.ts @@ -1,51 +1,57 @@ -export type List = { - /** - * Safely splits values. - * - * ```js - * Once (root, { list }) { - * list.split('1px calc(10% + 1px)', [' ', '\n', '\t']) //=> ['1px', 'calc(10% + 1px)'] - * } - * ``` - * - * @param string separated values. - * @param separators array of separators. - * @param last boolean indicator. - * @return Split values. - */ - split(string: string, separators: string[], last: boolean): string[] - /** - * Safely splits space-separated values (such as those for `background`, - * `border-radius`, and other shorthand properties). - * - * ```js - * Once (root, { list }) { - * list.space('1px calc(10% + 1px)') //=> ['1px', 'calc(10% + 1px)'] - * } - * ``` - * - * @param str Space-separated values. - * @return Split values. - */ - space(str: string): string[] +declare namespace list { + type List = { + default: List - /** - * Safely splits comma-separated values (such as those for `transition-*` - * and `background` properties). - * - * ```js - * Once (root, { list }) { - * list.comma('black, linear-gradient(white, black)') - * //=> ['black', 'linear-gradient(white, black)'] - * } - * ``` - * - * @param str Comma-separated values. - * @return Split values. - */ - comma(str: string): string[] + /** + * Safely splits values. + * + * ```js + * Once (root, { list }) { + * list.split('1px calc(10% + 1px)', [' ', '\n', '\t']) //=> ['1px', 'calc(10% + 1px)'] + * } + * ``` + * + * @param string separated values. + * @param separators array of separators. + * @param last boolean indicator. + * @return Split values. + */ + split(string: string, separators: string[], last: boolean): string[] + + /** + * Safely splits space-separated values (such as those for `background`, + * `border-radius`, and other shorthand properties). + * + * ```js + * Once (root, { list }) { + * list.space('1px calc(10% + 1px)') //=> ['1px', 'calc(10% + 1px)'] + * } + * ``` + * + * @param str Space-separated values. + * @return Split values. + */ + space(str: string): string[] + + /** + * Safely splits comma-separated values (such as those for `transition-*` + * and `background` properties). + * + * ```js + * Once (root, { list }) { + * list.comma('black, linear-gradient(white, black)') + * //=> ['black', 'linear-gradient(white, black)'] + * } + * ``` + * + * @param str Comma-separated values. + * @return Split values. + */ + comma(str: string): string[] + } } -declare const list: List +// eslint-disable-next-line @typescript-eslint/no-redeclare +declare const list: list.List -export default list +export = list diff --git a/lib/no-work-result.d.ts b/lib/no-work-result.d.ts index 7ba1462c5..dd150c647 100644 --- a/lib/no-work-result.d.ts +++ b/lib/no-work-result.d.ts @@ -5,6 +5,11 @@ import Warning from './warning.js' import Root from './root.js' import LazyResult from './lazy-result.js' +declare namespace NoWorkResult { + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { NoWorkResult_ as default } +} + /** * A Promise proxy for the result of PostCSS transformations. * This lazy result instance doesn't parse css unless `NoWorkResult#root` or `Result#root` @@ -17,7 +22,7 @@ import LazyResult from './lazy-result.js' * let root = noWorkResult.root // now css is parsed because we accessed the root * ``` */ -export default class NoWorkResult implements LazyResult { +declare class NoWorkResult_ implements LazyResult { then: Promise['then'] catch: Promise['catch'] finally: Promise['finally'] @@ -35,3 +40,7 @@ export default class NoWorkResult implements LazyResult { sync(): Result async(): Promise } + +declare class NoWorkResult extends NoWorkResult_ {} + +export = NoWorkResult diff --git a/lib/node.d.ts b/lib/node.d.ts index a97d43ae4..00f62b77a 100644 --- a/lib/node.d.ts +++ b/lib/node.d.ts @@ -1,7 +1,8 @@ import Declaration, { DeclarationProps } from './declaration.js' import Comment, { CommentProps } from './comment.js' import { Stringifier, Syntax } from './postcss.js' -import AtRule, { AtRuleProps } from './at-rule.js' +import AtRule = require('./at-rule.js') +import { AtRuleProps } from './at-rule.js' import Rule, { RuleProps } from './rule.js' import Warning, { WarningOptions } from './warning.js' import CssSyntaxError from './css-syntax-error.js' @@ -11,84 +12,90 @@ import Root from './root.js' import Document from './document.js' import Container from './container.js' -export type ChildNode = AtRule | Rule | Declaration | Comment - -export type AnyNode = AtRule | Rule | Declaration | Comment | Root | Document - -export type ChildProps = - | AtRuleProps - | RuleProps - | DeclarationProps - | CommentProps - -export interface Position { - /** - * Source offset in file. It starts from 0. - */ - offset: number - - /** - * Source line in file. In contrast to `offset` it starts from 1. - */ - column: number - - /** - * Source column in file. - */ - line: number -} - -export interface Range { - /** - * Start position, inclusive. - */ - start: Position - - /** - * End position, exclusive. - */ - end: Position -} - -export interface Source { - /** - * The file source of the node. - */ - input: Input - /** - * The inclusive starting position of the node’s source. - */ - start?: Position - /** - * The inclusive ending position of the node's source. - */ - end?: Position -} - -export interface NodeProps { - source?: Source -} - -interface NodeErrorOptions { - /** - * Plugin name that created this error. PostCSS will set it automatically. - */ - plugin?: string - /** - * A word inside a node's string, that should be highlighted as source - * of error. - */ - word?: string - /** - * An index inside a node's string that should be highlighted as source - * of error. - */ - index?: number - /** - * An ending index inside a node's string that should be highlighted as - * source of error. - */ - endIndex?: number +declare namespace Node { + export type ChildNode = AtRule.default | Rule | Declaration | Comment + + export type AnyNode = AtRule.default | Rule | Declaration | Comment | Root | Document + + export type ChildProps = + | AtRuleProps + | RuleProps + | DeclarationProps + | CommentProps + + export interface Position { + /** + * Source offset in file. It starts from 0. + */ + offset: number + + /** + * Source line in file. In contrast to `offset` it starts from 1. + */ + column: number + + /** + * Source column in file. + */ + line: number + } + + export interface Range { + /** + * Start position, inclusive. + */ + start: Position + + /** + * End position, exclusive. + */ + end: Position + } + + export interface Source { + /** + * The file source of the node. + */ + input: Input + /** + * The inclusive starting position of the node’s source. + */ + start?: Position + /** + * The inclusive ending position of the node's source. + */ + end?: Position + } + + export interface NodeProps { + source?: Source + } + + export interface NodeErrorOptions { + /** + * Plugin name that created this error. PostCSS will set it automatically. + */ + plugin?: string + /** + * A word inside a node's string, that should be highlighted as source + * of error. + */ + word?: string + /** + * An index inside a node's string that should be highlighted as source + * of error. + */ + index?: number + /** + * An ending index inside a node's string that should be highlighted as + * source of error. + */ + endIndex?: number + } + + // eslint-disable-next-line @typescript-eslint/no-shadow + class Node extends Node_ {} + export { Node as default } } /** @@ -97,7 +104,7 @@ interface NodeErrorOptions { * You should not extend this classes to create AST for selector or value * parser. */ -export default abstract class Node { +declare abstract class Node_ { /** * tring representing the node’s type. Possible values are `root`, `atrule`, * `rule`, `decl`, or `comment`. @@ -153,7 +160,7 @@ export default abstract class Node { * } * ``` */ - source?: Source + source?: Node.Source /** * Information to generate byte-to-byte equal node string as it was @@ -222,7 +229,7 @@ export default abstract class Node { * * @return Error object to throw it. */ - error(message: string, options?: NodeErrorOptions): CssSyntaxError + error(message: string, options?: Node.NodeErrorOptions): CssSyntaxError /** * This method is provided as a convenience wrapper for `Result#warn`. @@ -337,7 +344,7 @@ export default abstract class Node { * @return Current node to methods chain. */ replaceWith( - ...nodes: (ChildNode | ChildProps | ChildNode[] | ChildProps[])[] + ...nodes: (Node.ChildNode | Node.ChildProps | Node.ChildNode[] | Node.ChildProps[])[] ): this /** @@ -355,7 +362,7 @@ export default abstract class Node { * * @return Next node. */ - next(): ChildNode | undefined + next(): Node.ChildNode | undefined /** * Returns the previous child of the node’s parent. @@ -370,7 +377,7 @@ export default abstract class Node { * * @return Previous node. */ - prev(): ChildNode | undefined + prev(): Node.ChildNode | undefined /** * Insert new node before current node to current node’s parent. @@ -384,7 +391,7 @@ export default abstract class Node { * @param newNode New node. * @return This node for methods chain. */ - before(newNode: Node | ChildProps | string | Node[]): this + before(newNode: Node | Node.ChildProps | string | Node[]): this /** * Insert new node after current node to current node’s parent. @@ -398,7 +405,7 @@ export default abstract class Node { * @param newNode New node. * @return This node for methods chain. */ - after(newNode: Node | ChildProps | string | Node[]): this + after(newNode: Node | Node.ChildProps | string | Node[]): this /** * Finds the Root instance of the node’s tree. @@ -457,7 +464,7 @@ export default abstract class Node { * @param index The symbol number in the node’s string. * @return Symbol position in file. */ - positionInside(index: number): Position + positionInside(index: number): Node.Position /** * Get the position for a word or an index inside the node. @@ -465,7 +472,7 @@ export default abstract class Node { * @param opts Options. * @return Position. */ - positionBy(opts?: Pick): Position + positionBy(opts?: Pick): Node.Position /** * Get the range for a word or start and end index inside the node. @@ -474,5 +481,9 @@ export default abstract class Node { * @param opts Options. * @return Range. */ - rangeBy(opts?: Pick): Range + rangeBy(opts?: Pick): Node.Range } + +declare class Node extends Node_ {} + +export = Node diff --git a/lib/parse.d.ts b/lib/parse.d.ts index d6bdee299..4c943a4d6 100644 --- a/lib/parse.d.ts +++ b/lib/parse.d.ts @@ -1,5 +1,9 @@ import { Parser } from './postcss.js' -declare const parse: Parser +interface Parse extends Parser { + default: Parse +} -export default parse +declare const parse: Parse + +export = parse diff --git a/lib/postcss.d.mts b/lib/postcss.d.mts new file mode 100644 index 000000000..a8ca8c7a1 --- /dev/null +++ b/lib/postcss.d.mts @@ -0,0 +1,72 @@ +export { + // postcss function / namespace + default, + + // Value exports from postcss.mjs + stringify, + fromJSON, + // @ts-expect-error This value exists, but it’s untyped. + plugin, + parse, + list, + + document, + comment, + atRule, + rule, + decl, + root, + + CssSyntaxError, + Declaration, + Container, + Processor, + Document, + Comment, + Warning, + AtRule, + Result, + Input, + Rule, + Root, + Node, + + // Type-only exports + AcceptedPlugin, + AnyNode, + AtRuleProps, + Builder, + ChildNode, + ChildProps, + CommentProps, + ContainerProps, + DeclarationProps, + DocumentProps, + FilePosition, + Helpers, + JSONHydrator, + Message, + NodeErrorOptions, + NodeProps, + OldPlugin, + Parser, + Plugin, + PluginCreator, + Position, + Postcss, + ProcessOptions, + RootProps, + RuleProps, + Source, + SourceMap, + SourceMapOptions, + Stringifier, + Syntax, + TransformCallback, + Transformer, + WarningOptions, + + // This is a class, but it’s not re-exported. That’s why it’s exported as type-only here. + type LazyResult, + +} from './postcss.js' diff --git a/lib/postcss.d.ts b/lib/postcss.d.ts index 30a0d0883..5394ca7f4 100644 --- a/lib/postcss.d.ts +++ b/lib/postcss.d.ts @@ -20,65 +20,24 @@ import Result, { Message } from './result.js' import Root, { RootProps } from './root.js' import Rule, { RuleProps } from './rule.js' import CssSyntaxError from './css-syntax-error.js' -import list, { List } from './list.js' +import list from './list.js' import LazyResult from './lazy-result.js' import Processor from './processor.js' -export { - NodeErrorOptions, - DeclarationProps, - CssSyntaxError, - ContainerProps, - WarningOptions, - DocumentProps, - FilePosition, - CommentProps, - AtRuleProps, - Declaration, - ChildProps, - LazyResult, - ChildNode, - NodeProps, - Processor, - RuleProps, - RootProps, - Container, - Position, - Document, - AnyNode, - Warning, - Message, - Comment, - Source, - AtRule, - Result, - Input, - Node, - list, - Rule, - Root -} - -export type SourceMap = SourceMapGenerator & { - toJSON(): RawSourceMap -} - -export type Helpers = { result: Result; postcss: Postcss } & Postcss - type DocumentProcessor = ( document: Document, - helper: Helpers + helper: postcss.Helpers ) => Promise | void -type RootProcessor = (root: Root, helper: Helpers) => Promise | void +type RootProcessor = (root: Root, helper: postcss.Helpers) => Promise | void type DeclarationProcessor = ( decl: Declaration, - helper: Helpers + helper: postcss.Helpers ) => Promise | void -type RuleProcessor = (rule: Rule, helper: Helpers) => Promise | void -type AtRuleProcessor = (atRule: AtRule, helper: Helpers) => Promise | void +type RuleProcessor = (rule: Rule, helper: postcss.Helpers) => Promise | void +type AtRuleProcessor = (atRule: AtRule, helper: postcss.Helpers) => Promise | void type CommentProcessor = ( comment: Comment, - helper: Helpers + helper: postcss.Helpers ) => Promise | void interface Processors { @@ -188,185 +147,210 @@ interface Processors { Exit?: RootProcessor } -export interface Plugin extends Processors { - postcssPlugin: string - prepare?: (result: Result) => Processors -} - -export interface PluginCreator { - (opts?: PluginOptions): Plugin | Processor - postcss: true -} - -export interface Transformer extends TransformCallback { - postcssPlugin: string - postcssVersion: string -} - -export interface TransformCallback { - (root: Root, result: Result): Promise | void -} - -export interface OldPlugin extends Transformer { - (opts?: T): Transformer - postcss: Transformer -} - -export type AcceptedPlugin = - | Plugin - | PluginCreator - | OldPlugin - | TransformCallback - | { - postcss: TransformCallback | Processor - } - | Processor - -export interface Parser { - ( - css: string | { toString(): string }, - opts?: Pick - ): RootNode -} - -export interface Builder { - (part: string, node?: AnyNode, type?: 'start' | 'end'): void -} - -export interface Stringifier { - (node: AnyNode, builder: Builder): void -} - -export interface JSONHydrator { - (data: object[]): Node[] - (data: object): Node -} - -export interface Syntax { - /** - * Function to generate AST by string. - */ - parse?: Parser - - /** - * Class to generate string by AST. - */ - stringify?: Stringifier -} - -export interface SourceMapOptions { - /** - * Indicates that the source map should be embedded in the output CSS - * as a Base64-encoded comment. By default, it is `true`. - * But if all previous maps are external, not inline, PostCSS will not embed - * the map even if you do not set this option. - * - * If you have an inline source map, the result.map property will be empty, - * as the source map will be contained within the text of `result.css`. - */ - inline?: boolean - - /** - * Source map content from a previous processing step (e.g., Sass). - * - * PostCSS will try to read the previous source map - * automatically (based on comments within the source CSS), but you can use - * this option to identify it manually. - * - * If desired, you can omit the previous map with prev: `false`. - */ - prev?: string | boolean | object | ((file: string) => string) - - /** - * Indicates that PostCSS should set the origin content (e.g., Sass source) - * of the source map. By default, it is true. But if all previous maps do not - * contain sources content, PostCSS will also leave it out even if you - * do not set this option. - */ - sourcesContent?: boolean - - /** - * Indicates that PostCSS should add annotation comments to the CSS. - * By default, PostCSS will always add a comment with a path - * to the source map. PostCSS will not add annotations to CSS files - * that do not contain any comments. - * - * By default, PostCSS presumes that you want to save the source map as - * `opts.to + '.map'` and will use this path in the annotation comment. - * A different path can be set by providing a string value for annotation. - * - * If you have set `inline: true`, annotation cannot be disabled. - */ - annotation?: string | boolean | ((file: string, root: Root) => string) - - /** - * Override `from` in map’s sources. - */ - from?: string - - /** - * Use absolute path in generated source map. - */ - absolute?: boolean -} - -export interface ProcessOptions { - /** - * The path of the CSS source file. You should always set `from`, - * because it is used in source map generation and syntax error messages. - */ - from?: string - - /** - * The path where you'll put the output CSS file. You should always set `to` - * to generate correct source maps. - */ - to?: string - - /** - * Function to generate AST by string. - */ - parser?: Syntax | Parser - - /** - * Class to generate string by AST. - */ - stringifier?: Syntax | Stringifier - - /** - * Object with parse and stringify. - */ - syntax?: Syntax - - /** - * Source map options - */ - map?: SourceMapOptions | boolean -} - -export interface Postcss { - /** - * Create a new `Processor` instance that will apply `plugins` - * as CSS processors. - * - * ```js - * let postcss = require('postcss') - * - * postcss(plugins).process(css, { from, to }).then(result => { - * console.log(result.css) - * }) - * ``` - * - * @param plugins PostCSS plugins. - * @return Processor to process multiple CSS. - */ - (plugins?: AcceptedPlugin[]): Processor - (...plugins: AcceptedPlugin[]): Processor +declare namespace postcss { + export { + NodeErrorOptions, + DeclarationProps, + CssSyntaxError, + ContainerProps, + WarningOptions, + DocumentProps, + FilePosition, + CommentProps, + AtRuleProps, + Declaration, + ChildProps, + LazyResult, + ChildNode, + NodeProps, + Processor, + RuleProps, + RootProps, + Container, + Position, + Document, + AnyNode, + Warning, + Message, + Comment, + Source, + AtRule, + Result, + Input, + Node, + list, + Rule, + Root + } + + export type SourceMap = SourceMapGenerator & { + toJSON(): RawSourceMap + } + + export type Helpers = { result: Result; postcss: Postcss } & Postcss + + export interface Plugin extends Processors { + postcssPlugin: string + prepare?: (result: Result) => Processors + } + + export interface PluginCreator { + (opts?: PluginOptions): Plugin | Processor + postcss: true + } + + export interface Transformer extends TransformCallback { + postcssPlugin: string + postcssVersion: string + } + + export interface TransformCallback { + (root: Root, result: Result): Promise | void + } + + export interface OldPlugin extends Transformer { + (opts?: T): Transformer + postcss: Transformer + } + + export type AcceptedPlugin = + | Plugin + | PluginCreator + | OldPlugin + | TransformCallback + | { + postcss: TransformCallback | Processor + } + | Processor + + export interface Parser { + ( + css: string | { toString(): string }, + opts?: Pick + ): RootNode + } + + export interface Builder { + (part: string, node?: AnyNode, type?: 'start' | 'end'): void + } + + export interface Stringifier { + (node: AnyNode, builder: Builder): void + } + + export interface JSONHydrator { + (data: object[]): Node[] + (data: object): Node + } + + export interface Syntax { + /** + * Function to generate AST by string. + */ + parse?: Parser + + /** + * Class to generate string by AST. + */ + stringify?: Stringifier + } + + export interface SourceMapOptions { + /** + * Indicates that the source map should be embedded in the output CSS + * as a Base64-encoded comment. By default, it is `true`. + * But if all previous maps are external, not inline, PostCSS will not embed + * the map even if you do not set this option. + * + * If you have an inline source map, the result.map property will be empty, + * as the source map will be contained within the text of `result.css`. + */ + inline?: boolean + + /** + * Source map content from a previous processing step (e.g., Sass). + * + * PostCSS will try to read the previous source map + * automatically (based on comments within the source CSS), but you can use + * this option to identify it manually. + * + * If desired, you can omit the previous map with prev: `false`. + */ + prev?: string | boolean | object | ((file: string) => string) + + /** + * Indicates that PostCSS should set the origin content (e.g., Sass source) + * of the source map. By default, it is true. But if all previous maps do not + * contain sources content, PostCSS will also leave it out even if you + * do not set this option. + */ + sourcesContent?: boolean + + /** + * Indicates that PostCSS should add annotation comments to the CSS. + * By default, PostCSS will always add a comment with a path + * to the source map. PostCSS will not add annotations to CSS files + * that do not contain any comments. + * + * By default, PostCSS presumes that you want to save the source map as + * `opts.to + '.map'` and will use this path in the annotation comment. + * A different path can be set by providing a string value for annotation. + * + * If you have set `inline: true`, annotation cannot be disabled. + */ + annotation?: string | boolean | ((file: string, root: Root) => string) + + /** + * Override `from` in map’s sources. + */ + from?: string + + /** + * Use absolute path in generated source map. + */ + absolute?: boolean + } + + export interface ProcessOptions { + /** + * The path of the CSS source file. You should always set `from`, + * because it is used in source map generation and syntax error messages. + */ + from?: string + + /** + * The path where you'll put the output CSS file. You should always set `to` + * to generate correct source maps. + */ + to?: string + + /** + * Function to generate AST by string. + */ + parser?: Syntax | Parser + + /** + * Class to generate string by AST. + */ + stringifier?: Syntax | Stringifier + + /** + * Object with parse and stringify. + */ + syntax?: Syntax + + /** + * Source map options + */ + map?: SourceMapOptions | boolean + } + + export type Postcss = typeof postcss /** * Default function to convert a node tree into a CSS string. */ - stringify: Stringifier + export let stringify: Stringifier /** * Parses source css and returns a new `Root` or `Document` node, @@ -379,7 +363,7 @@ export interface Postcss { * root1.append(root2).toResult().css * ``` */ - parse: Parser + export let parse: Parser /** * Rehydrate a JSON AST (from `Node#toJSON`) back into the AST classes. @@ -390,12 +374,7 @@ export interface Postcss { * const root2 = postcss.fromJSON(json) * ``` */ - fromJSON: JSONHydrator - - /** - * Contains the `list` module. - */ - list: List + export let fromJSON: JSONHydrator /** * Creates a new `Comment` node. @@ -403,7 +382,7 @@ export interface Postcss { * @param defaults Properties for the new node. * @return New comment node */ - comment(defaults?: CommentProps): Comment + export function comment(defaults?: CommentProps): Comment /** * Creates a new `AtRule` node. @@ -411,7 +390,7 @@ export interface Postcss { * @param defaults Properties for the new node. * @return New at-rule node. */ - atRule(defaults?: AtRuleProps): AtRule + export function atRule(defaults?: AtRuleProps): AtRule /** * Creates a new `Declaration` node. @@ -419,7 +398,7 @@ export interface Postcss { * @param defaults Properties for the new node. * @return New declaration node. */ - decl(defaults?: DeclarationProps): Declaration + export function decl(defaults?: DeclarationProps): Declaration /** * Creates a new `Rule` node. @@ -427,7 +406,7 @@ export interface Postcss { * @param default Properties for the new node. * @return New rule node. */ - rule(defaults?: RuleProps): Rule + export function rule(defaults?: RuleProps): Rule /** * Creates a new `Root` node. @@ -435,7 +414,7 @@ export interface Postcss { * @param defaults Properties for the new node. * @return New root node. */ - root(defaults?: RootProps): Root + export function root(defaults?: RootProps): Root /** * Creates a new `Document` node. @@ -443,31 +422,27 @@ export interface Postcss { * @param defaults Properties for the new node. * @return New document node. */ - document(defaults?: DocumentProps): Document - - CssSyntaxError: typeof CssSyntaxError - Declaration: typeof Declaration - Container: typeof Container - Comment: typeof Comment - Warning: typeof Warning - AtRule: typeof AtRule - Result: typeof Result - Input: typeof Input - Rule: typeof Rule - Root: typeof Root - Node: typeof Node -} - -export const stringify: Stringifier -export const parse: Parser -export const fromJSON: JSONHydrator + export function document(defaults?: DocumentProps): Document -export const comment: Postcss['comment'] -export const atRule: Postcss['atRule'] -export const decl: Postcss['decl'] -export const rule: Postcss['rule'] -export const root: Postcss['root'] - -declare const postcss: Postcss + export { postcss as default } +} -export default postcss +/** + * Create a new `Processor` instance that will apply `plugins` + * as CSS processors. + * + * ```js + * let postcss = require('postcss') + * + * postcss(plugins).process(css, { from, to }).then(result => { + * console.log(result.css) + * }) + * ``` + * + * @param plugins PostCSS plugins. + * @return Processor to process multiple CSS. + */ +declare function postcss(plugins?: postcss.AcceptedPlugin[]): Processor +declare function postcss(...plugins: postcss.AcceptedPlugin[]): Processor + +export = postcss diff --git a/lib/previous-map.d.ts b/lib/previous-map.d.ts index 490d885fa..ce0ddc24e 100644 --- a/lib/previous-map.d.ts +++ b/lib/previous-map.d.ts @@ -2,6 +2,11 @@ import { SourceMapConsumer } from 'source-map-js' import { ProcessOptions } from './postcss.js' +declare namespace PreviousMap { + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { PreviousMap_ as default } +} + /** * Source map information from input CSS. * For example, source map after Sass compiler. @@ -14,7 +19,7 @@ import { ProcessOptions } from './postcss.js' * root.input.map //=> PreviousMap * ``` */ -export default class PreviousMap { +declare class PreviousMap_ { /** * Was source map inlined by data-uri to input CSS. */ @@ -70,3 +75,7 @@ export default class PreviousMap { */ withContent(): boolean } + +declare class PreviousMap extends PreviousMap_ {} + +export = PreviousMap diff --git a/lib/processor.d.ts b/lib/processor.d.ts index 19a365384..ff053c65f 100644 --- a/lib/processor.d.ts +++ b/lib/processor.d.ts @@ -10,6 +10,11 @@ import Result from './result.js' import Root from './root.js' import NoWorkResult from './no-work-result.js' +declare namespace Processor { + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Processor_ as default } +} + /** * Contains plugins to process CSS. Create one `Processor` instance, * initialize its plugins, and then use that instance on numerous CSS files. @@ -20,7 +25,7 @@ import NoWorkResult from './no-work-result.js' * processor.process(css2).then(result => console.log(result.css)) * ``` */ -export default class Processor { +declare class Processor_ { /** * Current PostCSS version. * @@ -100,3 +105,7 @@ export default class Processor { options?: ProcessOptions ): LazyResult | NoWorkResult } + +declare class Processor extends Processor_ {} + +export = Processor diff --git a/lib/result.d.ts b/lib/result.d.ts index 8a582b69e..e5d279670 100644 --- a/lib/result.d.ts +++ b/lib/result.d.ts @@ -11,31 +11,37 @@ import { } from './postcss.js' import Processor from './processor.js' -export interface Message { - /** - * Message type. - */ - type: string - - /** - * Source PostCSS plugin name. - */ - plugin?: string - - [others: string]: any -} - -export interface ResultOptions extends ProcessOptions { - /** - * The CSS node that was the source of the warning. - */ - node?: Node - - /** - * Name of plugin that created this warning. `Result#warn` will fill it - * automatically with `Plugin#postcssPlugin` value. - */ - plugin?: string +declare namespace Result { + export interface Message { + /** + * Message type. + */ + type: string + + /** + * Source PostCSS plugin name. + */ + plugin?: string + + [others: string]: any + } + + export interface ResultOptions extends ProcessOptions { + /** + * The CSS node that was the source of the warning. + */ + node?: Node + + /** + * Name of plugin that created this warning. `Result#warn` will fill it + * automatically with `Plugin#postcssPlugin` value. + */ + plugin?: string + } + + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Result_ as default } } /** @@ -54,7 +60,7 @@ export interface ResultOptions extends ProcessOptions { * const result2 = postcss.parse(css).toResult() * ``` */ -export default class Result { +declare class Result_ { /** * The Processor instance used for this transformation. * @@ -86,7 +92,7 @@ export default class Result { * } * ``` */ - messages: Message[] + messages: Result.Message[] /** * Root node after all transformations. @@ -105,7 +111,7 @@ export default class Result { * root.toResult(opts).opts === opts * ``` */ - opts: ResultOptions + opts: Result.ResultOptions /** * A CSS string representing of `Result#root`. @@ -142,7 +148,7 @@ export default class Result { * @param root Root node after all transformations. * @param opts Options from the `Processor#process` or `Root#toResult`. */ - constructor(processor: Processor, root: Root | Document, opts: ResultOptions) + constructor(processor: Processor, root: Root | Document, opts: Result.ResultOptions) /** * An alias for the `Result#css` property. @@ -194,3 +200,7 @@ export default class Result { */ warnings(): Warning[] } + +declare class Result extends Result_ {} + +export = Result diff --git a/lib/root.d.ts b/lib/root.d.ts index 251b92b42..d00cfbc52 100644 --- a/lib/root.d.ts +++ b/lib/root.d.ts @@ -3,40 +3,45 @@ import Document from './document.js' import { ProcessOptions } from './postcss.js' import Result from './result.js' -interface RootRaws extends Record { - /** - * The space symbols after the last child to the end of file. - */ - after?: string +declare namespace Root { + export interface RootRaws extends Record { + /** + * The space symbols after the last child to the end of file. + */ + after?: string - /** - * Non-CSS code before `Root`, when `Root` is inside `Document`. - * - * **Experimental:** some aspects of this node could change within minor - * or patch version releases. - */ - codeBefore?: string + /** + * Non-CSS code before `Root`, when `Root` is inside `Document`. + * + * **Experimental:** some aspects of this node could change within minor + * or patch version releases. + */ + codeBefore?: string - /** - * Non-CSS code after `Root`, when `Root` is inside `Document`. - * - * **Experimental:** some aspects of this node could change within minor - * or patch version releases. - */ - codeAfter?: string + /** + * Non-CSS code after `Root`, when `Root` is inside `Document`. + * + * **Experimental:** some aspects of this node could change within minor + * or patch version releases. + */ + codeAfter?: string - /** - * Is the last child has an (optional) semicolon. - */ - semicolon?: boolean -} + /** + * Is the last child has an (optional) semicolon. + */ + semicolon?: boolean + } -export interface RootProps extends ContainerProps { - /** - * Information used to generate byte-to-byte equal node string - * as it was in the origin input. - * */ - raws?: RootRaws + export interface RootProps extends ContainerProps { + /** + * Information used to generate byte-to-byte equal node string + * as it was in the origin input. + * */ + raws?: RootRaws + } + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Root_ as default } } /** @@ -48,10 +53,10 @@ export interface RootProps extends ContainerProps { * root.nodes.length //=> 2 * ``` */ -export default class Root extends Container { +declare class Root_ extends Container { type: 'root' parent: Document | undefined - raws: RootRaws + raws: Root.RootRaws /** * Returns a `Result` instance representing the root’s CSS. @@ -66,8 +71,12 @@ export default class Root extends Container { * @param opts Options. * @return Result with current root’s CSS. */ - toResult(options?: ProcessOptions): Result + toResult(options?: ProcessOptions): Result - constructor(defaults?: RootProps) - assign(overrides: object | RootProps): this + constructor(defaults?: Root.RootProps) + assign(overrides: object | Root.RootProps): this } + +declare class Root extends Root_ {} + +export = Root diff --git a/lib/rule.d.ts b/lib/rule.d.ts index ad6abb5ff..37fbb2510 100644 --- a/lib/rule.d.ts +++ b/lib/rule.d.ts @@ -1,48 +1,53 @@ import Container, { ContainerProps } from './container.js' -interface RuleRaws extends Record { - /** - * The space symbols before the node. It also stores `*` - * and `_` symbols before the declaration (IE hack). - */ - before?: string +declare namespace Rule { + export interface RuleRaws extends Record { + /** + * The space symbols before the node. It also stores `*` + * and `_` symbols before the declaration (IE hack). + */ + before?: string - /** - * The space symbols after the last child of the node to the end of the node. - */ - after?: string + /** + * The space symbols after the last child of the node to the end of the node. + */ + after?: string - /** - * The symbols between the selector and `{` for rules. - */ - between?: string + /** + * The symbols between the selector and `{` for rules. + */ + between?: string - /** - * Contains `true` if the last child has an (optional) semicolon. - */ - semicolon?: boolean + /** + * Contains `true` if the last child has an (optional) semicolon. + */ + semicolon?: boolean - /** - * Contains `true` if there is semicolon after rule. - */ - ownSemicolon?: string + /** + * Contains `true` if there is semicolon after rule. + */ + ownSemicolon?: string - /** - * The rule’s selector with comments. - */ - selector?: { - value: string - raw: string + /** + * The rule’s selector with comments. + */ + selector?: { + value: string + raw: string + } } -} -export interface RuleProps extends ContainerProps { - /** Selector or selectors of the rule. */ - selector?: string - /** Selectors of the rule represented as an array of strings. */ - selectors?: string[] - /** Information used to generate byte-to-byte equal node string as it was in the origin input. */ - raws?: RuleRaws + export interface RuleProps extends ContainerProps { + /** Selector or selectors of the rule. */ + selector?: string + /** Selectors of the rule represented as an array of strings. */ + selectors?: string[] + /** Information used to generate byte-to-byte equal node string as it was in the origin input. */ + raws?: RuleRaws + } + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Rule_ as default } } /** @@ -63,10 +68,10 @@ export interface RuleProps extends ContainerProps { * rule.toString() //=> 'a{}' * ``` */ -export default class Rule extends Container { +declare class Rule_ extends Container { type: 'rule' parent: Container | undefined - raws: RuleRaws + raws: Rule.RuleRaws /** * The rule’s full selector represented as a string. @@ -96,9 +101,13 @@ export default class Rule extends Container { */ selectors: string[] - constructor(defaults?: RuleProps) - assign(overrides: object | RuleProps): this - clone(overrides?: Partial): this - cloneBefore(overrides?: Partial): this - cloneAfter(overrides?: Partial): this + constructor(defaults?: Rule.RuleProps) + assign(overrides: object | Rule.RuleProps): this + clone(overrides?: Partial): this + cloneBefore(overrides?: Partial): this + cloneAfter(overrides?: Partial): this } + +declare class Rule extends Rule_ {} + +export = Rule diff --git a/lib/stringifier.d.ts b/lib/stringifier.d.ts index 23289dfb5..249acf4a0 100644 --- a/lib/stringifier.d.ts +++ b/lib/stringifier.d.ts @@ -10,7 +10,12 @@ import { Container } from './postcss.js' -export default class Stringifier { +declare namespace Stringifier { + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Stringifier_ as default } +} + +declare class Stringifier_ { builder: Builder constructor(builder: Builder) stringify(node: AnyNode, semicolon?: boolean): void @@ -35,3 +40,7 @@ export default class Stringifier { beforeAfter(node: AnyNode, detect: 'before' | 'after'): string rawValue(node: AnyNode, prop: string): string } + +declare class Stringifier extends Stringifier_ {} + +export = Stringifier diff --git a/lib/stringify.d.ts b/lib/stringify.d.ts index 363682ff3..06ad0b4de 100644 --- a/lib/stringify.d.ts +++ b/lib/stringify.d.ts @@ -1,5 +1,9 @@ import { Stringifier } from './postcss.js' -declare const stringify: Stringifier +interface Stringify extends Stringifier { + default: Stringify +} -export default stringify +declare const stringify: Stringify + +export = stringify diff --git a/lib/warning.d.ts b/lib/warning.d.ts index 838bef185..20ec0656b 100644 --- a/lib/warning.d.ts +++ b/lib/warning.d.ts @@ -1,42 +1,47 @@ import { RangePosition } from './css-syntax-error.js' import Node from './node.js' -export interface WarningOptions { - /** - * CSS node that caused the warning. - */ - node?: Node - - /** - * Word in CSS source that caused the warning. - */ - word?: string - - /** - * Start index, inclusive, in CSS node string that caused the warning. - */ - index?: number - - /** - * End index, exclusive, in CSS node string that caused the warning. - */ - endIndex?: number - - /** - * Start position, inclusive, in CSS node string that caused the warning. - */ - start?: RangePosition - - /** - * End position, exclusive, in CSS node string that caused the warning. - */ - end?: RangePosition - - /** - * Name of the plugin that created this warning. `Result#warn` fills - * this property automatically. - */ - plugin?: string +declare namespace Warning { + export interface WarningOptions { + /** + * CSS node that caused the warning. + */ + node?: Node + + /** + * Word in CSS source that caused the warning. + */ + word?: string + + /** + * Start index, inclusive, in CSS node string that caused the warning. + */ + index?: number + + /** + * End index, exclusive, in CSS node string that caused the warning. + */ + endIndex?: number + + /** + * Start position, inclusive, in CSS node string that caused the warning. + */ + start?: RangePosition + + /** + * End position, exclusive, in CSS node string that caused the warning. + */ + end?: RangePosition + + /** + * Name of the plugin that created this warning. `Result#warn` fills + * this property automatically. + */ + plugin?: string + } + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Warning_ as default } } /** @@ -48,7 +53,7 @@ export interface WarningOptions { * } * ``` */ -export default class Warning { +declare class Warning_ { /** * Type to filter warnings from `Result#messages`. * Always equal to `"warning"`. @@ -123,7 +128,7 @@ export default class Warning { * @param text Warning message. * @param opts Warning options. */ - constructor(text: string, opts?: WarningOptions) + constructor(text: string, opts?: Warning.WarningOptions) /** * Returns a warning position and message. @@ -136,3 +141,7 @@ export default class Warning { */ toString(): string } + +declare class Warning extends Warning_ {} + +export = Warning diff --git a/package.json b/package.json index f2e37e342..ae431c7dd 100755 --- a/package.json +++ b/package.json @@ -8,8 +8,7 @@ "exports": { ".": { "require": "./lib/postcss.js", - "import": "./lib/postcss.mjs", - "types": "./lib/postcss.d.ts" + "import": "./lib/postcss.mjs" }, "./lib/at-rule": "./lib/at-rule.js", "./lib/comment": "./lib/comment.js", @@ -151,6 +150,14 @@ "global-require": "off" }, "overrides": [ + { + "files": [ + "*.d.ts" + ], + "rules": { + "@typescript-eslint/no-redeclare": "off" + } + }, { "files": [ "*.test.*" diff --git a/test/css-syntax-error.test.ts b/test/css-syntax-error.test.ts index d2238ccd8..051bf1e8e 100755 --- a/test/css-syntax-error.test.ts +++ b/test/css-syntax-error.test.ts @@ -1,7 +1,7 @@ -import pico from 'picocolors' +import * as pico from 'picocolors' import { join, resolve as pathResolve } from 'path' import { pathToFileURL } from 'url' -import stripAnsi from 'strip-ansi' +import stripAnsi = require('strip-ansi') import Concat from 'concat-with-sourcemaps' import { test } from 'uvu' import { is, equal, match, type } from 'uvu/assert' diff --git a/test/errors.ts b/test/errors.ts index be471f04e..e5c07b7ae 100644 --- a/test/errors.ts +++ b/test/errors.ts @@ -9,9 +9,9 @@ const plugin: PluginCreator<{ a: number }> = opts => { postcssPlugin: 'remover', // THROWS Property 'Decl' does not exist on type 'Helpers'. Comment(decl, { Decl }) { - // THROWS Property 'prop' does not exist on type 'Comment' + // THROWS Property 'prop' does not exist on type 'Comment_' console.log(decl.prop) - // THROWS Property 'removeChild' does not exist on type 'Comment' + // THROWS Property 'removeChild' does not exist on type 'Comment_' decl.removeChild(1) } } diff --git a/test/fromJSON.test.ts b/test/fromJSON.test.ts index 2cbf82b6f..92e2cc081 100755 --- a/test/fromJSON.test.ts +++ b/test/fromJSON.test.ts @@ -1,4 +1,4 @@ -import v8 from 'v8' +import * as v8 from 'v8' import { test } from 'uvu' import { is, instance, throws } from 'uvu/assert' diff --git a/test/lazy-result.test.ts b/test/lazy-result.test.ts index c3a5e7281..0cace6445 100755 --- a/test/lazy-result.test.ts +++ b/test/lazy-result.test.ts @@ -1,6 +1,6 @@ import { is, equal, type } from 'uvu/assert' import { test } from 'uvu' -import mozilla from 'source-map-js' +import { SourceMapGenerator } from 'source-map-js' import LazyResult from '../lib/lazy-result.js' import Processor from '../lib/processor.js' @@ -35,7 +35,7 @@ test('has map only if necessary', () => { type(result2.map, 'undefined') let result3 = new LazyResult(processor, '', { map: { inline: false } }) - is(result3.map instanceof mozilla.SourceMapGenerator, true) + is(result3.map instanceof SourceMapGenerator, true) }) test('contains options', () => { diff --git a/test/no-work-result.test.ts b/test/no-work-result.test.ts index bffcda294..f8deeecdf 100644 --- a/test/no-work-result.test.ts +++ b/test/no-work-result.test.ts @@ -1,6 +1,6 @@ import { is, type, equal, throws, not, instance } from 'uvu/assert' import { test } from 'uvu' -import mozilla from 'source-map-js' +import { SourceMapGenerator } from 'source-map-js' import { spy } from 'nanospy' import NoWorkResult from '../lib/no-work-result.js' @@ -70,7 +70,7 @@ test('has map only if necessary', () => { from: '/a.css', map: { inline: false } }) - is(result3.map instanceof mozilla.SourceMapGenerator, true) + is(result3.map instanceof SourceMapGenerator, true) }) test('contains simple properties', () => { diff --git a/test/postcss.test.ts b/test/postcss.test.ts index 7c61c0123..713b4e877 100755 --- a/test/postcss.test.ts +++ b/test/postcss.test.ts @@ -2,13 +2,18 @@ import { is, type, equal, match, throws } from 'uvu/assert' import { spyOn, restoreAll } from 'nanospy' import { test } from 'uvu' -import postcss, { Root, PluginCreator } from '../lib/postcss.js' +import postcss = require('../lib/postcss.js') +import postcssDefault, { Root, PluginCreator } from '../lib/postcss.js' import Processor from '../lib/processor.js' test.after.each(() => { restoreAll() }) +test('default matches module.exports', () => { + is(postcss, postcssDefault) +}) + test('creates plugins list', () => { let processor = postcss() is(processor instanceof Processor, true) diff --git a/test/processor.test.ts b/test/processor.test.ts index 09a579c2d..d232ab2c2 100755 --- a/test/processor.test.ts +++ b/test/processor.test.ts @@ -1,6 +1,7 @@ import { is, type, equal, match, throws, not, instance } from 'uvu/assert' import { resolve as pathResolve } from 'path' import { spyOn, restoreAll } from 'nanospy' +// @ts-ignore type definitions for nanodelay@1 are wrong. import { delay } from 'nanodelay' import { test } from 'uvu' diff --git a/test/visitor.test.ts b/test/visitor.test.ts index 3190a627a..7d71a615d 100755 --- a/test/visitor.test.ts +++ b/test/visitor.test.ts @@ -1,4 +1,5 @@ import { resolve, basename } from 'path' +// @ts-ignore type definitions for nanodelay@1 are wrong. import { delay } from 'nanodelay' import { test } from 'uvu' import { is, type, equal, throws } from 'uvu/assert' diff --git a/tsconfig.json b/tsconfig.json index 200ac7802..37dc54f8b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,10 @@ { "compilerOptions": { + "lib": ["es2018"], "target": "es2018", "module": "commonjs", - "allowJs": true, + "moduleResolution": "node16", "strict": true, - "noEmit": true, - "skipLibCheck": true, - "esModuleInterop": true + "noEmit": true } }