From 3c195408152089419411d6fa6633debc7e912e7c Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Thu, 16 Feb 2023 15:28:45 +0100 Subject: [PATCH 01/16] Fix type definitions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Many files from postcss are exported. Each of this file is written using the syntax: ```js module.exports = Thing ``` This should be typed as: ```ts export = Thing ``` If such a file needs to export additional types, a namespace should be used. However, it was typed as: ```ts export default thing ``` This was solved at the time in #1459 (by me) by adding the following code to each module: ```js Thing.default = Thing ``` Whilst affective at the time, this was not the correct solution. The introduction of the `"module": "node16"` option in TypeScript made this issue more apparent. I recommend to remove this in a next semver major release. Usage has changed as follows: **cts** ```jsonc // tsconfig.json { "compilerOptions": { "module": "commonjs", "moduleResolution": "node16" } } ``` ```ts // Old import postcss = require('postcss') postcss.default(/* … */) ``` ```ts // New import postcss = require('postcss') postcss(/* … */) ``` **cjs** ```jsonc // tsconfig.json { "compilerOptions": { "checkJs": true, "module": "commonjs", "moduleResolution": "node16" } } ``` ```ts // Old const postcss = require('postcss') postcss.default(/* … */) ``` ```ts // New const postcss = require('postcss') postcss(/* … */) ``` **mts** / **mjs** ```jsonc // tsconfig.json { "compilerOptions": { "module": "node16" } } ``` ```ts // Old import postcss from 'postcss' postcss.default(/* … */) ``` ```ts // New import postcss from 'postcss' postcss(/* … */) ``` **bundler** ```jsonc // tsconfig.json { "compilerOptions": { "allowSyntheticDefaultImports": true, "module": "esnext", "moduleResolution": "node16" } } ``` ```ts // Unchanged import postcss from 'postcss' postcss(/* … */) ``` **ES module interop** (Not recommended for library code!) ```jsonc // tsconfig.json { "compilerOptions": { "esModuleInterop": true, "module": "commonjs", "moduleResolution": "node16" } } ``` ```ts // Unchanged import postcss from 'postcss' postcss(/* … */) ``` When compiling to CJS, it is still possible to write ESM-like syntax, for example: ```jsonc // Same as import postcss = require('postcss') import * as postcss from 'postcss' // Named imports from namespaces are allowed import { root, Root } from 'postcss' ``` The following changes were made to `tsconfig.json`: - `lib` was specified explicitly. By default, TypeScript includes the `dom` types. This is not desirable, it even causes some conflicts with types used by Postcss. - `allowJs` was removed. When this is enabled, TypeScript treats `.d.ts` files matching `.js` files as output. This caused some unexpected issues where TypeScript simply ignored type errors. - `skipLibCheck` was removed. Ideally all types just work together. This can be added again later if necessary. - `esModuleInterop` was removed. Enabling this option causes TypeScript to allow code that is otherwise invalid. This may be useful for CLIs or website, but is dangerous to use for libraries. Correcting these options also highlighted some issues in the tests. These have been resolved. `nanodelay@1` has incorrect type definitions, so it has been marked using `@ts-expect-error` comments. In `test/errors.ts` were some type errors. These were marked as `THROWS`, which signals `check-dts` a type error is expected. This was changed to `@ts-expect-error`, which works too, but it also works with other TypeScript tooling. The `Postcss` type was missing some properties. As a side effect of the corrected types, these are now available. This also affects the `Helper` type. Last, but not least, type definitions have been added for `postcss.mjs`. Closes #1814 --- lib/at-rule.d.ts | 32 +- lib/comment.d.ts | 30 +- lib/container.d.ts | 32 +- lib/css-syntax-error.d.ts | 30 +- lib/declaration.d.ts | 38 +-- lib/document.d.ts | 35 +-- lib/fromJSON.d.ts | 2 +- lib/input.d.ts | 80 ++--- lib/lazy-result.d.ts | 18 +- lib/list.d.ts | 97 +++--- lib/no-work-result.d.ts | 20 +- lib/node.d.ts | 208 ++++++------- lib/parse.d.ts | 2 +- lib/postcss.d.mts | 72 +++++ lib/postcss.d.ts | 534 +++++++++++++++++----------------- lib/previous-map.d.ts | 4 +- lib/processor.d.ts | 12 +- lib/result.d.ts | 66 +++-- lib/root.d.ts | 28 +- lib/rule.d.ts | 32 +- lib/stringifier.d.ts | 4 +- lib/stringify.d.ts | 2 +- lib/warning.d.ts | 84 +++--- test/css-syntax-error.test.ts | 7 +- test/document.test.ts | 2 +- test/errors.ts | 10 +- test/fromJSON.test.ts | 5 +- test/lazy-result.test.ts | 8 +- test/map.test.ts | 7 +- test/no-work-result.test.ts | 8 +- test/node.test.ts | 3 +- test/postcss.test.ts | 5 +- test/processor.test.ts | 14 +- test/result.test.ts | 5 +- test/types.ts | 3 +- test/visitor.test.ts | 4 +- tsconfig.json | 7 +- 37 files changed, 837 insertions(+), 713 deletions(-) create mode 100644 lib/postcss.d.mts diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index 7a39b2f9f..113240e1a 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -1,4 +1,4 @@ -import Container, { ContainerProps } from './container.js' +import Container = require('./container.js') interface AtRuleRaws extends Record { /** @@ -36,13 +36,15 @@ interface AtRuleRaws extends Record { } } -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 +declare namespace AtRule { + interface AtRuleProps extends Container.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 + } } /** @@ -70,7 +72,7 @@ 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 @@ -98,9 +100,11 @@ 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 } + +export = AtRule diff --git a/lib/comment.d.ts b/lib/comment.d.ts index 8eaf3a230..f6abe1e68 100644 --- a/lib/comment.d.ts +++ b/lib/comment.d.ts @@ -1,5 +1,5 @@ -import Container from './container.js' -import Node, { NodeProps } from './node.js' +import Container = require('./container.js') +import Node = require('./node.js') interface CommentRaws extends Record { /** @@ -18,11 +18,13 @@ interface CommentRaws extends Record { 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 +declare namespace Comment { + interface CommentProps extends Node.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 + } } /** @@ -38,7 +40,7 @@ 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 @@ -48,9 +50,11 @@ export default class Comment extends Node { */ 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 } + +export = Comment diff --git a/lib/container.d.ts b/lib/container.d.ts index 2b75e372f..a60310f21 100644 --- a/lib/container.d.ts +++ b/lib/container.d.ts @@ -1,8 +1,8 @@ -import Node, { ChildNode, NodeProps, ChildProps } from './node.js' -import Declaration from './declaration.js' -import Comment from './comment.js' -import AtRule from './at-rule.js' -import Rule from './rule.js' +import Node = require('./node.js') +import Declaration = require('./declaration.js') +import Comment = require('./comment.js') +import AtRule = require('./at-rule.js') +import Rule = require('./rule.js') interface ValueOptions { /** @@ -16,8 +16,10 @@ interface ValueOptions { fast?: string } -export interface ContainerProps extends NodeProps { - nodes?: (ChildNode | ChildProps)[] +declare namespace Container { + interface ContainerProps extends Node.NodeProps { + nodes?: (Node.ChildNode | Node.ChildProps)[] + } } /** @@ -27,8 +29,8 @@ 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< - Child extends Node = ChildNode +declare abstract class Container< + Child extends Node = Node.ChildNode > extends Node { /** * An array containing the container’s children. @@ -117,7 +119,7 @@ export default abstract class Container< * @return Returns `false` if iteration was broke. */ walk( - callback: (node: ChildNode, index: number) => false | void + callback: (node: Node.ChildNode, index: number) => false | void ): false | undefined /** @@ -269,7 +271,7 @@ export default abstract class Container< * @return This node for methods chain. */ append( - ...nodes: (Node | Node[] | ChildProps | ChildProps[] | string | string[])[] + ...nodes: (Node | Node[] | Node.ChildProps | Node.ChildProps[] | string | string[])[] ): this /** @@ -293,7 +295,7 @@ export default abstract class Container< * @return This node for methods chain. */ prepend( - ...nodes: (Node | Node[] | ChildProps | ChildProps[] | string | string[])[] + ...nodes: (Node | Node[] | Node.ChildProps | Node.ChildProps[] | string | string[])[] ): this /** @@ -321,7 +323,7 @@ export default abstract class Container< */ insertBefore( oldNode: Child | number, - newNode: Child | ChildProps | string | Child[] | ChildProps[] | string[] + newNode: Child | Node.ChildProps | string | Child[] | Node.ChildProps[] | string[] ): this /** @@ -333,7 +335,7 @@ export default abstract class Container< */ insertAfter( oldNode: Child | number, - newNode: Child | ChildProps | string | Child[] | ChildProps[] | string[] + newNode: Child | Node.ChildProps | string | Child[] | Node.ChildProps[] | string[] ): this /** @@ -440,3 +442,5 @@ export default abstract class Container< */ index(child: Child | number): number } + +export = Container diff --git a/lib/css-syntax-error.d.ts b/lib/css-syntax-error.d.ts index 3fd055528..ea531d883 100644 --- a/lib/css-syntax-error.d.ts +++ b/lib/css-syntax-error.d.ts @@ -1,18 +1,20 @@ 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 + 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 + } } /** @@ -44,7 +46,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 +61,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 +239,5 @@ export default class CssSyntaxError { */ showSourceCode(color?: boolean): string } + +export = CssSyntaxError diff --git a/lib/declaration.d.ts b/lib/declaration.d.ts index 4a1fb1ec9..9e3b815ab 100644 --- a/lib/declaration.d.ts +++ b/lib/declaration.d.ts @@ -1,5 +1,5 @@ -import Container from './container.js' -import Node from './node.js' +import Container = require('./container.js') +import Node = require('./node.js') interface DeclarationRaws extends Record { /** @@ -27,15 +27,17 @@ interface DeclarationRaws extends Record { } } -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 +declare namespace Declaration { + 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 + } } /** @@ -55,7 +57,7 @@ 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 @@ -116,9 +118,11 @@ 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 } + +export = Declaration diff --git a/lib/document.d.ts b/lib/document.d.ts index 28a599ca8..a11762071 100644 --- a/lib/document.d.ts +++ b/lib/document.d.ts @@ -1,23 +1,22 @@ -import Container, { ContainerProps } from './container.js' +import Container = require('./container.js') import { ProcessOptions } from './postcss.js' -import Result from './result.js' -import Root, { RootProps } from './root.js' +import Result = require('./result.js') +import Root = require('./root.js') -export interface DocumentProps extends ContainerProps { - nodes?: Root[] +declare namespace Document { + interface DocumentProps extends Container.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 - /** * Represents a file and contains all its parsed nodes. * @@ -32,11 +31,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 +54,5 @@ export default class Document extends Container { */ toResult(options?: ProcessOptions): Result } + +export = Document diff --git a/lib/fromJSON.d.ts b/lib/fromJSON.d.ts index 13b169c66..3a3ec7a1f 100644 --- a/lib/fromJSON.d.ts +++ b/lib/fromJSON.d.ts @@ -2,4 +2,4 @@ import { JSONHydrator } from './postcss.js' declare const fromJSON: JSONHydrator -export default fromJSON +export = fromJSON diff --git a/lib/input.d.ts b/lib/input.d.ts index 48c904b22..0d18888b6 100644 --- a/lib/input.d.ts +++ b/lib/input.d.ts @@ -1,41 +1,43 @@ 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 - - /** - * Line of exclusive end position in source file. - */ - endLine?: number - - /** - * Column of exclusive end position in source file. - */ - endColumn?: number +import PreviousMap = require('./previous-map.js') + +declare namespace Input { + 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 - /** - * Source code. - */ - source?: string + /** + * Source code. + */ + source?: string + } } /** @@ -46,7 +48,7 @@ export interface FilePosition { * const input = root.source.input * ``` */ -export default class Input { +declare class Input { /** * Input CSS source. * @@ -139,7 +141,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 +185,5 @@ export default class Input { opts?: { plugin?: CssSyntaxError['plugin'] } ): CssSyntaxError } + +export = Input diff --git a/lib/lazy-result.d.ts b/lib/lazy-result.d.ts index a5142345e..5bab7e77e 100644 --- a/lib/lazy-result.d.ts +++ b/lib/lazy-result.d.ts @@ -1,8 +1,8 @@ -import Result, { Message, ResultOptions } from './result.js' +import Result = require('./result.js') import { SourceMap } from './postcss.js' -import Processor from './processor.js' -import Warning from './warning.js' -import Root from './root.js' +import Processor = require('./processor.js') +import Warning = require('./warning.js') +import Root = require('./root.js') /** * A Promise proxy for the result of PostCSS transformations. @@ -13,7 +13,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 @@ -64,7 +64,7 @@ export default class LazyResult implements PromiseLike { * @param css CSS to parse and transform. * @param opts Options from the `Processor#process` or `Root#toResult`. */ - constructor(processor: Processor, css: string, opts: ResultOptions) + constructor(processor: Processor, css: string, opts: Result.ResultOptions) /** * Returns the default string description of an object. @@ -81,7 +81,7 @@ export default class LazyResult implements PromiseLike { /** * Options from the `Processor#process` call. */ - get opts(): ResultOptions + get opts(): Result.ResultOptions /** * Processes input CSS through synchronous plugins, converts `Root` @@ -139,7 +139,7 @@ export default class LazyResult implements PromiseLike { * * PostCSS runners should always use `LazyResult#then`. */ - get messages(): Message[] + get messages(): Result.Message[] /** * Processes input CSS through synchronous plugins @@ -174,3 +174,5 @@ export default class LazyResult implements PromiseLike { */ async(): Promise } + +export = LazyResult diff --git a/lib/list.d.ts b/lib/list.d.ts index 79841edeb..957be16c9 100644 --- a/lib/list.d.ts +++ b/lib/list.d.ts @@ -1,51 +1,54 @@ -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 = { + /** + * 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[] + /** + * 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..3384de67c 100644 --- a/lib/no-work-result.d.ts +++ b/lib/no-work-result.d.ts @@ -1,9 +1,9 @@ -import Result, { Message, ResultOptions } from './result.js' +import Result = require('./result.js') import { SourceMap } from './postcss.js' -import Processor from './processor.js' -import Warning from './warning.js' -import Root from './root.js' -import LazyResult from './lazy-result.js' +import Processor = require('./processor.js') +import Warning = require('./warning.js') +import Root = require('./root.js') +import LazyResult = require('./lazy-result.js') /** * A Promise proxy for the result of PostCSS transformations. @@ -17,21 +17,23 @@ 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'] - constructor(processor: Processor, css: string, opts: ResultOptions) + constructor(processor: Processor, css: string, opts: Result.ResultOptions) get [Symbol.toStringTag](): string get processor(): Processor - get opts(): ResultOptions + get opts(): Result.ResultOptions get css(): string get content(): string get map(): SourceMap get root(): Root - get messages(): Message[] + get messages(): Result.Message[] warnings(): Warning[] toString(): string sync(): Result async(): Promise } + +export = NoWorkResult diff --git a/lib/node.d.ts b/lib/node.d.ts index a97d43ae4..83e871d03 100644 --- a/lib/node.d.ts +++ b/lib/node.d.ts @@ -1,94 +1,96 @@ -import Declaration, { DeclarationProps } from './declaration.js' -import Comment, { CommentProps } from './comment.js' +import Declaration = require('./declaration.js') +import Comment = require('./comment.js') import { Stringifier, Syntax } from './postcss.js' -import AtRule, { AtRuleProps } from './at-rule.js' -import Rule, { RuleProps } from './rule.js' -import Warning, { WarningOptions } from './warning.js' -import CssSyntaxError from './css-syntax-error.js' -import Result from './result.js' -import Input from './input.js' -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 +import AtRule = require('./at-rule.js') +import Rule = require('./rule.js') +import Warning = require('./warning.js') +import CssSyntaxError = require('./css-syntax-error.js') +import Result = require('./result.js') +import Input = require('./input.js') +import Root = require('./root.js') +import Document = require('./document.js') +import Container = require('./container.js') + +declare namespace Node { + type ChildNode = AtRule | Rule | Declaration | Comment + + type AnyNode = AtRule | Rule | Declaration | Comment | Root | Document + + type ChildProps = + | AtRule.AtRuleProps + | Rule.RuleProps + | Declaration.DeclarationProps + | Comment.CommentProps + + 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 + } + + interface Range { + /** + * Start position, inclusive. + */ + start: Position + + /** + * End position, exclusive. + */ + end: Position + } + + 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 + } + + 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 + } } /** @@ -97,7 +99,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 +155,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 +224,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`. @@ -241,7 +243,7 @@ export default abstract class Node { * * @return Created warning object. */ - warn(result: Result, text: string, opts?: WarningOptions): Warning + warn(result: Result, text: string, opts?: Warning.WarningOptions): Warning /** * Removes the node from its parent and cleans the parent properties @@ -337,7 +339,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 +357,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 +372,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 +386,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 +400,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 +459,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 +467,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 +476,7 @@ export default abstract class Node { * @param opts Options. * @return Range. */ - rangeBy(opts?: Pick): Range + rangeBy(opts?: Pick): Node.Range } + +export = Node diff --git a/lib/parse.d.ts b/lib/parse.d.ts index d6bdee299..b17a0d3ae 100644 --- a/lib/parse.d.ts +++ b/lib/parse.d.ts @@ -2,4 +2,4 @@ import { Parser } from './postcss.js' declare const parse: Parser -export default 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..fe400d71b 100644 --- a/lib/postcss.d.ts +++ b/lib/postcss.d.ts @@ -1,6 +1,7 @@ import { SourceMapGenerator, RawSourceMap } from 'source-map-js' -import Node, { +import Node = require('./node.js') +import { Position, Source, ChildNode, @@ -9,76 +10,45 @@ import Node, { ChildProps, AnyNode } from './node.js' -import Declaration, { DeclarationProps } from './declaration.js' -import Container, { ContainerProps } from './container.js' -import Document, { DocumentProps } from './document.js' -import Warning, { WarningOptions } from './warning.js' -import Comment, { CommentProps } from './comment.js' -import AtRule, { AtRuleProps } from './at-rule.js' -import Input, { FilePosition } from './input.js' -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 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 +import Declaration = require('./declaration.js') +import { DeclarationProps } from './declaration.js' +import Container = require('./container.js') +import { ContainerProps } from './container.js' +import Document = require('./document.js') +import { DocumentProps } from './document.js' +import Warning = require('./warning.js') +import { WarningOptions } from './warning.js' +import Comment = require('./comment.js') +import { CommentProps } from './comment.js' +import AtRule = require('./at-rule.js') +import { AtRuleProps } from './at-rule.js' +import Input = require('./input.js') +import { FilePosition } from './input.js' +import Result = require('./result.js') +import { Message } from './result.js' +import Root = require('./root.js') +import { RootProps } from './root.js' +import Rule = require('./rule.js') +import { RuleProps } from './rule.js' +import CssSyntaxError = require('./css-syntax-error.js') +import list = require('./list.js') +import LazyResult = require('./lazy-result.js') +import Processor = require('./processor.js') 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 +158,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 +374,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 +385,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 +393,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 +401,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 +409,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 +417,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 +425,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 +433,25 @@ 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 function document(defaults?: DocumentProps): Document } -export const stringify: Stringifier -export const parse: Parser -export const fromJSON: JSONHydrator - -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 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..eec3ebb9f 100644 --- a/lib/previous-map.d.ts +++ b/lib/previous-map.d.ts @@ -14,7 +14,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 +70,5 @@ export default class PreviousMap { */ withContent(): boolean } + +export = PreviousMap diff --git a/lib/processor.d.ts b/lib/processor.d.ts index 19a365384..49cede8e5 100644 --- a/lib/processor.d.ts +++ b/lib/processor.d.ts @@ -5,10 +5,10 @@ import { Transformer, TransformCallback } from './postcss.js' -import LazyResult from './lazy-result.js' -import Result from './result.js' -import Root from './root.js' -import NoWorkResult from './no-work-result.js' +import LazyResult = require('./lazy-result.js') +import Result = require('./result.js') +import Root = require('./root.js') +import NoWorkResult = require('./no-work-result.js') /** * Contains plugins to process CSS. Create one `Processor` instance, @@ -20,7 +20,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 +100,5 @@ export default class Processor { options?: ProcessOptions ): LazyResult | NoWorkResult } + +export = Processor diff --git a/lib/result.d.ts b/lib/result.d.ts index 8a582b69e..785d28288 100644 --- a/lib/result.d.ts +++ b/lib/result.d.ts @@ -9,33 +9,35 @@ import { Warning, WarningOptions } 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 +import Processor = require('./processor.js') + +declare namespace Result { + interface Message { + /** + * Message type. + */ + type: string + + /** + * Source PostCSS plugin name. + */ + plugin?: string + + [others: string]: any + } + + 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 + } } /** @@ -54,7 +56,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 +88,7 @@ export default class Result { * } * ``` */ - messages: Message[] + messages: Result.Message[] /** * Root node after all transformations. @@ -105,7 +107,7 @@ export default class Result { * root.toResult(opts).opts === opts * ``` */ - opts: ResultOptions + opts: Result.ResultOptions /** * A CSS string representing of `Result#root`. @@ -142,7 +144,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 +196,5 @@ export default class Result { */ warnings(): Warning[] } + +export = Result diff --git a/lib/root.d.ts b/lib/root.d.ts index 251b92b42..e258b83fd 100644 --- a/lib/root.d.ts +++ b/lib/root.d.ts @@ -1,7 +1,7 @@ -import Container, { ContainerProps } from './container.js' -import Document from './document.js' +import Container = require('./container.js') +import Document = require('./document.js') import { ProcessOptions } from './postcss.js' -import Result from './result.js' +import Result = require('./result.js') interface RootRaws extends Record { /** @@ -31,12 +31,14 @@ interface RootRaws extends Record { 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 +declare namespace Root { + interface RootProps extends Container.ContainerProps { + /** + * Information used to generate byte-to-byte equal node string + * as it was in the origin input. + * */ + raws?: RootRaws + } } /** @@ -48,7 +50,7 @@ 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 @@ -68,6 +70,8 @@ export default class Root extends Container { */ toResult(options?: ProcessOptions): Result - constructor(defaults?: RootProps) - assign(overrides: object | RootProps): this + constructor(defaults?: Root.RootProps) + assign(overrides: object | Root.RootProps): this } + +export = Root diff --git a/lib/rule.d.ts b/lib/rule.d.ts index ad6abb5ff..d0c7c13e4 100644 --- a/lib/rule.d.ts +++ b/lib/rule.d.ts @@ -1,4 +1,4 @@ -import Container, { ContainerProps } from './container.js' +import Container = require('./container.js') interface RuleRaws extends Record { /** @@ -36,13 +36,15 @@ interface RuleRaws extends Record { } } -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 +declare namespace Rule { + interface RuleProps extends Container.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 + } } /** @@ -63,7 +65,7 @@ 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 @@ -96,9 +98,11 @@ 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 } + +export = Rule diff --git a/lib/stringifier.d.ts b/lib/stringifier.d.ts index 23289dfb5..24a5affc3 100644 --- a/lib/stringifier.d.ts +++ b/lib/stringifier.d.ts @@ -10,7 +10,7 @@ import { Container } from './postcss.js' -export default class Stringifier { +declare class Stringifier { builder: Builder constructor(builder: Builder) stringify(node: AnyNode, semicolon?: boolean): void @@ -35,3 +35,5 @@ export default class Stringifier { beforeAfter(node: AnyNode, detect: 'before' | 'after'): string rawValue(node: AnyNode, prop: string): string } + +export = Stringifier diff --git a/lib/stringify.d.ts b/lib/stringify.d.ts index 363682ff3..b737e4ac4 100644 --- a/lib/stringify.d.ts +++ b/lib/stringify.d.ts @@ -2,4 +2,4 @@ import { Stringifier } from './postcss.js' declare const stringify: Stringifier -export default stringify +export = stringify diff --git a/lib/warning.d.ts b/lib/warning.d.ts index 838bef185..91bbec2ee 100644 --- a/lib/warning.d.ts +++ b/lib/warning.d.ts @@ -1,42 +1,44 @@ 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 +import Node = require('./node.js') + +declare namespace Warning { + 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 + } } /** @@ -48,7 +50,7 @@ export interface WarningOptions { * } * ``` */ -export default class Warning { +declare class Warning { /** * Type to filter warnings from `Result#messages`. * Always equal to `"warning"`. @@ -123,7 +125,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 +138,5 @@ export default class Warning { */ toString(): string } + +export = Warning diff --git a/test/css-syntax-error.test.ts b/test/css-syntax-error.test.ts index d2238ccd8..5eb294831 100755 --- a/test/css-syntax-error.test.ts +++ b/test/css-syntax-error.test.ts @@ -1,12 +1,13 @@ -import pico from 'picocolors' +import pico = require('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' -import postcss, { +import postcss = require('../lib/postcss.js') +import { ProcessOptions, CssSyntaxError, Plugin, diff --git a/test/document.test.ts b/test/document.test.ts index 7d81e3e9c..941beb222 100755 --- a/test/document.test.ts +++ b/test/document.test.ts @@ -2,7 +2,7 @@ import { test } from 'uvu' import { is, match } from 'uvu/assert' import { Result, parse } from '../lib/postcss.js' -import Document from '../lib/document.js' +import Document = require('../lib/document.js') test('generates result without map', () => { let root = parse('a {}') diff --git a/test/errors.ts b/test/errors.ts index be471f04e..77ea303aa 100644 --- a/test/errors.ts +++ b/test/errors.ts @@ -1,17 +1,17 @@ import { PluginCreator } from '../lib/postcss.js' const plugin: PluginCreator<{ a: number }> = opts => { - // THROWS 'opts' is possibly 'undefined' + // @ts-expect-error 'opts' is possibly 'undefined' console.log(opts.a) - // THROWS Property 'b' does not exist on type '{ a: number; }' + // @ts-expect-error Property 'b' does not exist on type '{ a: number; }' console.log(opts?.b) return { postcssPlugin: 'remover', - // THROWS Property 'Decl' does not exist on type 'Helpers'. + // @ts-expect-error Property 'Decl' does not exist on type 'Helpers'. Comment(decl, { Decl }) { - // THROWS Property 'prop' does not exist on type 'Comment' + // @ts-expect-error Property 'prop' does not exist on type 'Comment' console.log(decl.prop) - // THROWS Property 'removeChild' does not exist on type 'Comment' + // @ts-expect-error 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..0344ecf20 100755 --- a/test/fromJSON.test.ts +++ b/test/fromJSON.test.ts @@ -1,8 +1,9 @@ -import v8 from 'v8' +import * as v8 from 'v8' import { test } from 'uvu' import { is, instance, throws } from 'uvu/assert' -import postcss, { Root, Declaration, Input, Rule } from '../lib/postcss.js' +import postcss = require('../lib/postcss.js') +import { Root, Declaration, Input, Rule } from '../lib/postcss.js' test('rehydrates a JSON AST', () => { let cssWithMap = postcss().process( diff --git a/test/lazy-result.test.ts b/test/lazy-result.test.ts index c3a5e7281..341815c66 100755 --- a/test/lazy-result.test.ts +++ b/test/lazy-result.test.ts @@ -1,9 +1,9 @@ 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' +import LazyResult = require('../lib/lazy-result.js') +import Processor = require('../lib/processor.js') let processor = new Processor() @@ -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/map.test.ts b/test/map.test.ts index 08847464f..0e0f7da49 100755 --- a/test/map.test.ts +++ b/test/map.test.ts @@ -6,8 +6,9 @@ import { pathToFileURL } from 'url' import { existsSync } from 'fs' import { test } from 'uvu' -import postcss, { SourceMap, Rule, Root } from '../lib/postcss.js' -import PreviousMap from '../lib/previous-map.js' +import postcss = require('../lib/postcss.js') +import { FilePosition, SourceMap, Rule, Root } from '../lib/postcss.js' +import PreviousMap = require('../lib/previous-map.js') function consumer(map: SourceMap): any { return (SourceMapConsumer as any).fromSourceMap(map) @@ -735,7 +736,7 @@ test('supports previous inline map with empty processor', () => { to: '/c.css' }) let root3 = postcss.parse(result2.css, { from: '/c.css' }) - match((root3.source?.input.origin(1, 0) as any).file, 'a.css') + match((root3.source?.input!.origin(1, 0) as FilePosition).file!, 'a.css') }) test('absolute sourcemaps have source contents', () => { diff --git a/test/no-work-result.test.ts b/test/no-work-result.test.ts index bffcda294..ef3c9c40d 100644 --- a/test/no-work-result.test.ts +++ b/test/no-work-result.test.ts @@ -1,10 +1,10 @@ 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' -import Processor from '../lib/processor.js' +import NoWorkResult = require('../lib/no-work-result.js') +import Processor = require('../lib/processor.js') let processor = new Processor() @@ -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/node.test.ts b/test/node.test.ts index bbc211a7a..90652e36a 100755 --- a/test/node.test.ts +++ b/test/node.test.ts @@ -2,7 +2,8 @@ import { resolve } from 'path' import { test } from 'uvu' import { is, equal, type, not } from 'uvu/assert' -import postcss, { +import postcss = require('../lib/postcss.js') +import { AnyNode, AtRule, Root, diff --git a/test/postcss.test.ts b/test/postcss.test.ts index 7c61c0123..0d7dd7828 100755 --- a/test/postcss.test.ts +++ b/test/postcss.test.ts @@ -2,8 +2,9 @@ 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 Processor from '../lib/processor.js' +import postcss = require('../lib/postcss.js') +import { Root, PluginCreator } from '../lib/postcss.js' +import Processor = require('../lib/processor.js') test.after.each(() => { restoreAll() diff --git a/test/processor.test.ts b/test/processor.test.ts index 09a579c2d..eb3dc48c8 100755 --- a/test/processor.test.ts +++ b/test/processor.test.ts @@ -1,10 +1,12 @@ import { is, type, equal, match, throws, not, instance } from 'uvu/assert' import { resolve as pathResolve } from 'path' import { spyOn, restoreAll } from 'nanospy' +// @ts-expect-error type definitions for nanodelay@1 are wrong. import { delay } from 'nanodelay' import { test } from 'uvu' -import postcss, { +import postcss = require('../lib/postcss.js') +import { Plugin, Result, Node, @@ -15,11 +17,11 @@ import postcss, { Parser, Stringifier } from '../lib/postcss.js' -import CssSyntaxError from '../lib/css-syntax-error.js' -import LazyResult from '../lib/lazy-result.js' -import NoWorkResult from '../lib/no-work-result.js' -import Processor from '../lib/processor.js' -import Rule from '../lib/rule.js' +import CssSyntaxError = require('../lib/css-syntax-error.js') +import LazyResult = require('../lib/lazy-result.js') +import NoWorkResult = require('../lib/no-work-result.js') +import Processor = require('../lib/processor.js') +import Rule = require('../lib/rule.js') test.after.each(() => { restoreAll() diff --git a/test/result.test.ts b/test/result.test.ts index 6c0328bb8..ea411b568 100755 --- a/test/result.test.ts +++ b/test/result.test.ts @@ -1,8 +1,9 @@ import { test } from 'uvu' import { is, equal } from 'uvu/assert' -import postcss, { Warning, Result, Root, Plugin } from '../lib/postcss.js' -import Processor from '../lib/processor.js' +import postcss = require('../lib/postcss.js') +import { Warning, Result, Root, Plugin } from '../lib/postcss.js' +import Processor = require('../lib/processor.js') let processor = new Processor() let root = new Root() diff --git a/test/types.ts b/test/types.ts index 7ff9e8929..a18b9fcd7 100644 --- a/test/types.ts +++ b/test/types.ts @@ -1,4 +1,5 @@ -import postcss, { Result, PluginCreator } from '../lib/postcss.js' +import postcss = require('../lib/postcss.js') +import { Result, PluginCreator } from '../lib/postcss.js' const plugin: PluginCreator = prop => { return { diff --git a/test/visitor.test.ts b/test/visitor.test.ts index 3190a627a..d6235e322 100755 --- a/test/visitor.test.ts +++ b/test/visitor.test.ts @@ -1,9 +1,11 @@ import { resolve, basename } from 'path' +// @ts-expect-error type definitions for nanodelay@1 are wrong. import { delay } from 'nanodelay' import { test } from 'uvu' import { is, type, equal, throws } from 'uvu/assert' -import postcss, { +import postcss = require('../lib/postcss.js') +import { Container, Root, Rule, 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 } } From d75e3095d15ce662f7d4ab32c78b034af585fc7a Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Thu, 16 Feb 2023 16:25:59 +0100 Subject: [PATCH 02/16] Remove redundant types fields from package.json These are redundant if their file path matches the file they represent. --- package.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/package.json b/package.json index 69a46c38c..c12b629fb 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", @@ -41,7 +40,6 @@ "./package.json": "./package.json" }, "main": "./lib/postcss.js", - "types": "./lib/postcss.d.ts", "keywords": [ "css", "postcss", From 00b33a637fce9593b6b6f5735797669526ab572d Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Thu, 16 Feb 2023 17:48:48 +0100 Subject: [PATCH 03/16] Restore THROWS assertsions --- test/errors.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/errors.ts b/test/errors.ts index 77ea303aa..be471f04e 100644 --- a/test/errors.ts +++ b/test/errors.ts @@ -1,17 +1,17 @@ import { PluginCreator } from '../lib/postcss.js' const plugin: PluginCreator<{ a: number }> = opts => { - // @ts-expect-error 'opts' is possibly 'undefined' + // THROWS 'opts' is possibly 'undefined' console.log(opts.a) - // @ts-expect-error Property 'b' does not exist on type '{ a: number; }' + // THROWS Property 'b' does not exist on type '{ a: number; }' console.log(opts?.b) return { postcssPlugin: 'remover', - // @ts-expect-error Property 'Decl' does not exist on type 'Helpers'. + // THROWS Property 'Decl' does not exist on type 'Helpers'. Comment(decl, { Decl }) { - // @ts-expect-error Property 'prop' does not exist on type 'Comment' + // THROWS Property 'prop' does not exist on type 'Comment' console.log(decl.prop) - // @ts-expect-error Property 'removeChild' does not exist on type 'Comment' + // THROWS Property 'removeChild' does not exist on type 'Comment' decl.removeChild(1) } } From d18b24a7094ccb42049110552a046750ab7d4a88 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Thu, 16 Feb 2023 17:51:08 +0100 Subject: [PATCH 04/16] Restore types field in package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index c12b629fb..20259b4c0 100755 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "./package.json": "./package.json" }, "main": "./lib/postcss.js", + "types": "./lib/postcss.d.ts", "keywords": [ "css", "postcss", From afad941c93ca0f92ddc2b40b684a4ea3ebda035c Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Thu, 2 Mar 2023 12:35:05 +0100 Subject: [PATCH 05/16] Add back support for default in type definitions --- lib/at-rule.d.ts | 11 +- lib/comment.d.ts | 2 + lib/container.d.ts | 2 + lib/css-syntax-error.d.ts | 2 + lib/declaration.d.ts | 2 + lib/document.d.ts | 12 +- lib/fromJSON.d.ts | 6 +- lib/input.d.ts | 2 + lib/lazy-result.d.ts | 2 + lib/list.d.ts | 3 + lib/no-work-result.d.ts | 2 + lib/node.d.ts | 2 + lib/parse.d.ts | 6 +- lib/postcss.d.ts | 347 +++++++++++++++++++------------------- lib/previous-map.d.ts | 2 + lib/processor.d.ts | 2 + lib/result.d.ts | 2 + lib/root.d.ts | 11 +- lib/rule.d.ts | 11 +- lib/stringifier.d.ts | 2 + lib/stringify.d.ts | 6 +- lib/warning.d.ts | 2 + package.json | 8 + 23 files changed, 260 insertions(+), 187 deletions(-) diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index 113240e1a..4dc6d96c2 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -47,6 +47,12 @@ declare namespace AtRule { } } +interface AtRuleCtor { + default: AtRuleCtor + + new (defaults?: AtRule.AtRuleProps): AtRule +} + /** * Represents an at-rule. * @@ -72,7 +78,7 @@ declare namespace AtRule { * media.nodes //=> [] * ``` */ -declare class AtRule extends Container { +interface AtRule extends Container { type: 'atrule' parent: Container | undefined raws: AtRuleRaws @@ -100,11 +106,12 @@ declare class AtRule extends Container { */ params: string - constructor(defaults?: AtRule.AtRuleProps) assign(overrides: object | AtRule.AtRuleProps): this clone(overrides?: Partial): this cloneBefore(overrides?: Partial): this cloneAfter(overrides?: Partial): this } +declare const AtRule: AtRuleCtor + export = AtRule diff --git a/lib/comment.d.ts b/lib/comment.d.ts index f6abe1e68..062d3eddb 100644 --- a/lib/comment.d.ts +++ b/lib/comment.d.ts @@ -41,6 +41,8 @@ declare namespace Comment { * will be stored in the `raws` properties explained above. */ declare class Comment extends Node { + static default: typeof Comment + type: 'comment' parent: Container | undefined raws: CommentRaws diff --git a/lib/container.d.ts b/lib/container.d.ts index a60310f21..49d1d0347 100644 --- a/lib/container.d.ts +++ b/lib/container.d.ts @@ -32,6 +32,8 @@ declare namespace Container { declare abstract class Container< Child extends Node = Node.ChildNode > extends Node { + static default: typeof Container + /** * An array containing the container’s children. * diff --git a/lib/css-syntax-error.d.ts b/lib/css-syntax-error.d.ts index ea531d883..f89b2f236 100644 --- a/lib/css-syntax-error.d.ts +++ b/lib/css-syntax-error.d.ts @@ -47,6 +47,8 @@ declare namespace CssSyntaxError { * ``` */ declare class CssSyntaxError { + static default: typeof CssSyntaxError + /** * Instantiates a CSS syntax error. Can be instantiated for a single position * or for a range. diff --git a/lib/declaration.d.ts b/lib/declaration.d.ts index 9e3b815ab..c851ef39e 100644 --- a/lib/declaration.d.ts +++ b/lib/declaration.d.ts @@ -58,6 +58,8 @@ declare namespace Declaration { * ``` */ declare class Declaration extends Node { + static default: typeof Declaration + type: 'decl' parent: Container | undefined raws: DeclarationRaws diff --git a/lib/document.d.ts b/lib/document.d.ts index a11762071..1982eb9d1 100644 --- a/lib/document.d.ts +++ b/lib/document.d.ts @@ -17,6 +17,12 @@ declare namespace Document { } } +interface DocumentCtor { + default: DocumentCtor + + new (defaults?: Document.DocumentProps): Document +} + /** * Represents a file and contains all its parsed nodes. * @@ -31,12 +37,10 @@ declare namespace Document { * document.nodes.length //=> 2 * ``` */ -declare class Document extends Container { +interface Document extends Container { type: 'document' parent: undefined - constructor(defaults?: Document.DocumentProps) - /** * Returns a `Result` instance representing the document’s CSS roots. * @@ -55,4 +59,6 @@ declare class Document extends Container { toResult(options?: ProcessOptions): Result } +declare const Document: DocumentCtor + export = Document diff --git a/lib/fromJSON.d.ts b/lib/fromJSON.d.ts index 3a3ec7a1f..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 +} + +declare const fromJSON: FromJSON export = fromJSON diff --git a/lib/input.d.ts b/lib/input.d.ts index 0d18888b6..8af526f34 100644 --- a/lib/input.d.ts +++ b/lib/input.d.ts @@ -49,6 +49,8 @@ declare namespace Input { * ``` */ declare class Input { + static default: typeof Input + /** * Input CSS source. * diff --git a/lib/lazy-result.d.ts b/lib/lazy-result.d.ts index 5bab7e77e..7dadd32ef 100644 --- a/lib/lazy-result.d.ts +++ b/lib/lazy-result.d.ts @@ -14,6 +14,8 @@ import Root = require('./root.js') * ``` */ declare class LazyResult implements PromiseLike { + static LazyResult: typeof LazyResult + /** * Processes input CSS through synchronous and asynchronous plugins * and calls `onFulfilled` with a Result instance. If a plugin throws diff --git a/lib/list.d.ts b/lib/list.d.ts index 957be16c9..6a56cad62 100644 --- a/lib/list.d.ts +++ b/lib/list.d.ts @@ -1,5 +1,7 @@ declare namespace list { type List = { + default: List + /** * Safely splits values. * @@ -15,6 +17,7 @@ declare namespace list { * @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). diff --git a/lib/no-work-result.d.ts b/lib/no-work-result.d.ts index 3384de67c..eaed419d8 100644 --- a/lib/no-work-result.d.ts +++ b/lib/no-work-result.d.ts @@ -18,6 +18,8 @@ import LazyResult = require('./lazy-result.js') * ``` */ declare class NoWorkResult implements LazyResult { + static default: typeof NoWorkResult + then: Promise['then'] catch: Promise['catch'] finally: Promise['finally'] diff --git a/lib/node.d.ts b/lib/node.d.ts index 83e871d03..0eb22e5a3 100644 --- a/lib/node.d.ts +++ b/lib/node.d.ts @@ -100,6 +100,8 @@ declare namespace Node { * parser. */ declare abstract class Node { + static default: typeof Node + /** * tring representing the node’s type. Possible values are `root`, `atrule`, * `rule`, `decl`, or `comment`. diff --git a/lib/parse.d.ts b/lib/parse.d.ts index b17a0d3ae..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 +} + +declare const parse: Parse export = parse diff --git a/lib/postcss.d.ts b/lib/postcss.d.ts index fe400d71b..5ec661981 100644 --- a/lib/postcss.d.ts +++ b/lib/postcss.d.ts @@ -1,53 +1,34 @@ import { SourceMapGenerator, RawSourceMap } from 'source-map-js' -import Node = require('./node.js') -import { - Position, - Source, - ChildNode, - NodeErrorOptions, - NodeProps, - ChildProps, - AnyNode -} from './node.js' -import Declaration = require('./declaration.js') -import { DeclarationProps } from './declaration.js' -import Container = require('./container.js') -import { ContainerProps } from './container.js' -import Document = require('./document.js') -import { DocumentProps } from './document.js' -import Warning = require('./warning.js') -import { WarningOptions } from './warning.js' -import Comment = require('./comment.js') -import { CommentProps } from './comment.js' -import AtRule = require('./at-rule.js') -import { AtRuleProps } from './at-rule.js' -import Input = require('./input.js') -import { FilePosition } from './input.js' -import Result = require('./result.js') -import { Message } from './result.js' -import Root = require('./root.js') -import { RootProps } from './root.js' -import Rule = require('./rule.js') -import { RuleProps } from './rule.js' -import CssSyntaxError = require('./css-syntax-error.js') +import Node_ = require('./node.js') +import Declaration_ = require('./declaration.js') +import Container_ = require('./container.js') +import Document_ = require('./document.js') +import Warning_ = require('./warning.js') +import Comment_ = require('./comment.js') +import AtRule_ = require('./at-rule.js') +import Input_ = require('./input.js') +import Result_ = require('./result.js') +import Root_ = require('./root.js') +import Rule_ = require('./rule.js') +import CssSyntaxError_ = require('./css-syntax-error.js') import list = require('./list.js') -import LazyResult = require('./lazy-result.js') -import Processor = require('./processor.js') +import LazyResult_ = require('./lazy-result.js') +import Processor_ = require('./processor.js') type DocumentProcessor = ( - document: Document, + document: postcss.Document, helper: postcss.Helpers ) => Promise | void -type RootProcessor = (root: Root, helper: postcss.Helpers) => Promise | void +type RootProcessor = (root: postcss.Root, helper: postcss.Helpers) => Promise | void type DeclarationProcessor = ( - decl: Declaration, + decl: postcss.Declaration, helper: postcss.Helpers ) => Promise | void -type RuleProcessor = (rule: Rule, helper: postcss.Helpers) => Promise | void -type AtRuleProcessor = (atRule: AtRule, helper: postcss.Helpers) => Promise | void +type RuleProcessor = (rule: postcss.Rule, helper: postcss.Helpers) => Promise | void +type AtRuleProcessor = (atRule: postcss.AtRule, helper: postcss.Helpers) => Promise | void type CommentProcessor = ( - comment: Comment, + comment: postcss.Comment, helper: postcss.Helpers ) => Promise | void @@ -159,72 +140,69 @@ interface Processors { } 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 & { + type NodeErrorOptions = Node_.NodeErrorOptions + type DeclarationProps = Declaration_.DeclarationProps + type ContainerProps = Container_.ContainerProps + type WarningOptions = Warning_.WarningOptions + type DocumentProps = Document_.DocumentProps + type FilePosition = Input_.FilePosition + type CommentProps = Comment_.CommentProps + type AtRuleProps = AtRule_.AtRuleProps + type ChildProps = Node_.ChildProps + type LazyResult = LazyResult_ + type ChildNode = Node_.ChildNode + type NodeProps = Node_.NodeProps + type RuleProps = Rule_.RuleProps + type RootProps = Root_.RootProps + type Position = Node_.Position + type AnyNode = Node_.AnyNode + type Message = Result_.Message + type Source = Node_.Source + type CssSyntaxError = CssSyntaxError_ + type Declaration = Declaration_ + type Processor = Processor_ + type Container = Container_ + type Document = Document_ + type Warning = Warning_ + type Comment = Comment_ + type AtRule = AtRule_ + type Result = Result_ + type Input = Input_ + type Node = Node_ + type Rule = Rule_ + type Root = Root_ + + type SourceMap = SourceMapGenerator & { toJSON(): RawSourceMap } - export type Helpers = { result: Result; postcss: Postcss } & Postcss + type Helpers = { result: Result; postcss: Postcss } & Postcss - export interface Plugin extends Processors { + interface Plugin extends Processors { postcssPlugin: string prepare?: (result: Result) => Processors } - export interface PluginCreator { + interface PluginCreator { (opts?: PluginOptions): Plugin | Processor postcss: true } - export interface Transformer extends TransformCallback { + interface Transformer extends TransformCallback { postcssPlugin: string postcssVersion: string } - export interface TransformCallback { + interface TransformCallback { (root: Root, result: Result): Promise | void } - export interface OldPlugin extends Transformer { + interface OldPlugin extends Transformer { (opts?: T): Transformer postcss: Transformer } - export type AcceptedPlugin = + type AcceptedPlugin = | Plugin | PluginCreator | OldPlugin @@ -234,27 +212,27 @@ declare namespace postcss { } | Processor - export interface Parser { + interface Parser { ( css: string | { toString(): string }, opts?: Pick ): RootNode } - export interface Builder { + interface Builder { (part: string, node?: AnyNode, type?: 'start' | 'end'): void } - export interface Stringifier { + interface Stringifier { (node: AnyNode, builder: Builder): void } - export interface JSONHydrator { + interface JSONHydrator { (data: object[]): Node[] (data: object): Node } - export interface Syntax { + interface Syntax { /** * Function to generate AST by string. */ @@ -266,7 +244,7 @@ declare namespace postcss { stringify?: Stringifier } - export interface SourceMapOptions { + interface SourceMapOptions { /** * Indicates that the source map should be embedded in the output CSS * as a Base64-encoded comment. By default, it is `true`. @@ -322,7 +300,7 @@ declare namespace postcss { absolute?: boolean } - export interface ProcessOptions { + 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. @@ -356,102 +334,121 @@ declare namespace postcss { map?: SourceMapOptions | boolean } - export type Postcss = typeof postcss + interface Postcss { + default: Postcss - /** - * Default function to convert a node tree into a CSS string. - */ - export let stringify: Stringifier + /** + * 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?: postcss.AcceptedPlugin[]): Processor + (...plugins: postcss.AcceptedPlugin[]): Processor - /** - * Parses source css and returns a new `Root` or `Document` node, - * which contains the source CSS nodes. - * - * ```js - * // Simple CSS concatenation with source map support - * const root1 = postcss.parse(css1, { from: file1 }) - * const root2 = postcss.parse(css2, { from: file2 }) - * root1.append(root2).toResult().css - * ``` - */ - export let parse: Parser + /** + * Default function to convert a node tree into a CSS string. + */ + stringify: Stringifier - /** - * Rehydrate a JSON AST (from `Node#toJSON`) back into the AST classes. - * - * ```js - * const json = root.toJSON() - * // save to file, send by network, etc - * const root2 = postcss.fromJSON(json) - * ``` - */ - export let fromJSON: JSONHydrator + /** + * Parses source css and returns a new `Root` or `Document` node, + * which contains the source CSS nodes. + * + * ```js + * // Simple CSS concatenation with source map support + * const root1 = postcss.parse(css1, { from: file1 }) + * const root2 = postcss.parse(css2, { from: file2 }) + * root1.append(root2).toResult().css + * ``` + */ + parse: Parser - /** - * Creates a new `Comment` node. - * - * @param defaults Properties for the new node. - * @return New comment node - */ - export function comment(defaults?: CommentProps): Comment + /** + * Rehydrate a JSON AST (from `Node#toJSON`) back into the AST classes. + * + * ```js + * const json = root.toJSON() + * // save to file, send by network, etc + * const root2 = postcss.fromJSON(json) + * ``` + */ + fromJSON: JSONHydrator - /** - * Creates a new `AtRule` node. - * - * @param defaults Properties for the new node. - * @return New at-rule node. - */ - export function atRule(defaults?: AtRuleProps): AtRule + /** + * Creates a new `Comment` node. + * + * @param defaults Properties for the new node. + * @return New comment node + */ + comment(defaults?: CommentProps): Comment - /** - * Creates a new `Declaration` node. - * - * @param defaults Properties for the new node. - * @return New declaration node. - */ - export function decl(defaults?: DeclarationProps): Declaration + /** + * Creates a new `AtRule` node. + * + * @param defaults Properties for the new node. + * @return New at-rule node. + */ + atRule(defaults?: AtRuleProps): AtRule - /** - * Creates a new `Rule` node. - * - * @param default Properties for the new node. - * @return New rule node. - */ - export function rule(defaults?: RuleProps): Rule + /** + * Creates a new `Declaration` node. + * + * @param defaults Properties for the new node. + * @return New declaration node. + */ + decl(defaults?: DeclarationProps): Declaration - /** - * Creates a new `Root` node. - * - * @param defaults Properties for the new node. - * @return New root node. - */ - export function root(defaults?: RootProps): Root + /** + * Creates a new `Rule` node. + * + * @param default Properties for the new node. + * @return New rule node. + */ + rule(defaults?: RuleProps): Rule - /** - * Creates a new `Document` node. - * - * @param defaults Properties for the new node. - * @return New document node. - */ - export function document(defaults?: DocumentProps): Document + /** + * Creates a new `Root` node. + * + * @param defaults Properties for the new node. + * @return New root node. + */ + root(defaults?: RootProps): Root + + /** + * Creates a new `Document` node. + * + * @param defaults Properties for the new node. + * @return New document node. + */ + document(defaults?: DocumentProps): Document + + CssSyntaxError: typeof CssSyntaxError_ + Declaration: typeof Declaration_ + Processor: typeof Processor_ + Container: typeof Container_ + Document: typeof Document_ + Warning: typeof Warning_ + Comment: typeof Comment_ + AtRule: typeof AtRule_ + Result: typeof Result_ + Input: typeof Input_ + Node: typeof Node_ + list: typeof list + Rule: typeof Rule_ + Root: typeof Root_ + } } -/** - * 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 +declare const postcss: postcss.Postcss export = postcss diff --git a/lib/previous-map.d.ts b/lib/previous-map.d.ts index eec3ebb9f..c972f05bd 100644 --- a/lib/previous-map.d.ts +++ b/lib/previous-map.d.ts @@ -15,6 +15,8 @@ import { ProcessOptions } from './postcss.js' * ``` */ declare class PreviousMap { + static default: typeof PreviousMap + /** * Was source map inlined by data-uri to input CSS. */ diff --git a/lib/processor.d.ts b/lib/processor.d.ts index 49cede8e5..5fdee063f 100644 --- a/lib/processor.d.ts +++ b/lib/processor.d.ts @@ -21,6 +21,8 @@ import NoWorkResult = require('./no-work-result.js') * ``` */ declare class Processor { + static default: typeof Processor + /** * Current PostCSS version. * diff --git a/lib/result.d.ts b/lib/result.d.ts index 785d28288..83335ed2f 100644 --- a/lib/result.d.ts +++ b/lib/result.d.ts @@ -57,6 +57,8 @@ declare namespace Result { * ``` */ declare class Result { + static default: typeof Result + /** * The Processor instance used for this transformation. * diff --git a/lib/root.d.ts b/lib/root.d.ts index e258b83fd..b48929da5 100644 --- a/lib/root.d.ts +++ b/lib/root.d.ts @@ -41,6 +41,12 @@ declare namespace Root { } } +interface RootCtor { + default: RootCtor + + new (defaults?: Root.RootProps): Root +} + /** * Represents a CSS file and contains all its parsed nodes. * @@ -50,7 +56,7 @@ declare namespace Root { * root.nodes.length //=> 2 * ``` */ -declare class Root extends Container { +interface Root extends Container { type: 'root' parent: Document | undefined raws: RootRaws @@ -70,8 +76,9 @@ declare class Root extends Container { */ toResult(options?: ProcessOptions): Result - constructor(defaults?: Root.RootProps) assign(overrides: object | Root.RootProps): this } +declare const Root: RootCtor + export = Root diff --git a/lib/rule.d.ts b/lib/rule.d.ts index d0c7c13e4..8d4263e5f 100644 --- a/lib/rule.d.ts +++ b/lib/rule.d.ts @@ -47,6 +47,12 @@ declare namespace Rule { } } +interface RuleCtor { + default: RuleCtor + + new (defaults?: Rule.RuleProps): Rule +} + /** * Represents a CSS rule: a selector followed by a declaration block. * @@ -65,7 +71,7 @@ declare namespace Rule { * rule.toString() //=> 'a{}' * ``` */ -declare class Rule extends Container { +interface Rule extends Container { type: 'rule' parent: Container | undefined raws: RuleRaws @@ -98,11 +104,12 @@ declare class Rule extends Container { */ selectors: string[] - constructor(defaults?: Rule.RuleProps) assign(overrides: object | Rule.RuleProps): this clone(overrides?: Partial): this cloneBefore(overrides?: Partial): this cloneAfter(overrides?: Partial): this } +declare const Rule: RuleCtor + export = Rule diff --git a/lib/stringifier.d.ts b/lib/stringifier.d.ts index 24a5affc3..52aca8d91 100644 --- a/lib/stringifier.d.ts +++ b/lib/stringifier.d.ts @@ -11,6 +11,8 @@ import { } from './postcss.js' declare class Stringifier { + static default: typeof Stringifier + builder: Builder constructor(builder: Builder) stringify(node: AnyNode, semicolon?: boolean): void diff --git a/lib/stringify.d.ts b/lib/stringify.d.ts index b737e4ac4..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 +} + +declare const stringify: Stringify export = stringify diff --git a/lib/warning.d.ts b/lib/warning.d.ts index 91bbec2ee..120c38a6c 100644 --- a/lib/warning.d.ts +++ b/lib/warning.d.ts @@ -51,6 +51,8 @@ declare namespace Warning { * ``` */ declare class Warning { + static default: Warning + /** * Type to filter warnings from `Result#messages`. * Always equal to `"warning"`. diff --git a/package.json b/package.json index 20259b4c0..74694c302 100755 --- a/package.json +++ b/package.json @@ -147,6 +147,14 @@ "global-require": "off" }, "overrides": [ + { + "files": [ + "*.d.ts" + ], + "rules": { + "@typescript-eslint/no-redeclare": "off" + } + }, { "files": [ "*.test.*" From 6f65c8f7a6e6ed7243dc60e1e5ad69a72261585b Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Thu, 2 Mar 2023 12:40:37 +0100 Subject: [PATCH 06/16] Add hacks needed to make check-dts work --- test/processor.test.ts | 2 +- test/visitor.test.ts | 2 +- tsconfig.json | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test/processor.test.ts b/test/processor.test.ts index eb3dc48c8..3e801eff3 100755 --- a/test/processor.test.ts +++ b/test/processor.test.ts @@ -1,7 +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-expect-error type definitions for nanodelay@1 are wrong. +// @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 d6235e322..71ac5d7c9 100755 --- a/test/visitor.test.ts +++ b/test/visitor.test.ts @@ -1,5 +1,5 @@ import { resolve, basename } from 'path' -// @ts-expect-error type definitions for nanodelay@1 are wrong. +// @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 37dc54f8b..9437d40cb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,6 +5,7 @@ "module": "commonjs", "moduleResolution": "node16", "strict": true, - "noEmit": true + "noEmit": true, + "skipLibCheck": true } } From 15bfd9d084b9e3a142a949f5142a3352b4900d39 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sun, 26 Mar 2023 13:34:48 +0200 Subject: [PATCH 07/16] Revert tests --- lib/lazy-result.d.ts | 2 +- test/css-syntax-error.test.ts | 5 ++--- test/document.test.ts | 2 +- test/fromJSON.test.ts | 3 +-- test/lazy-result.test.ts | 4 ++-- test/map.test.ts | 7 +++---- test/no-work-result.test.ts | 8 ++++---- test/node.test.ts | 3 +-- test/postcss.test.ts | 5 ++--- test/processor.test.ts | 13 ++++++------- test/result.test.ts | 5 ++--- test/types.ts | 3 +-- test/visitor.test.ts | 3 +-- 13 files changed, 27 insertions(+), 36 deletions(-) diff --git a/lib/lazy-result.d.ts b/lib/lazy-result.d.ts index 7dadd32ef..396535a90 100644 --- a/lib/lazy-result.d.ts +++ b/lib/lazy-result.d.ts @@ -14,7 +14,7 @@ import Root = require('./root.js') * ``` */ declare class LazyResult implements PromiseLike { - static LazyResult: typeof LazyResult + static default: typeof LazyResult /** * Processes input CSS through synchronous and asynchronous plugins diff --git a/test/css-syntax-error.test.ts b/test/css-syntax-error.test.ts index 5eb294831..051bf1e8e 100755 --- a/test/css-syntax-error.test.ts +++ b/test/css-syntax-error.test.ts @@ -1,4 +1,4 @@ -import pico = require('picocolors') +import * as pico from 'picocolors' import { join, resolve as pathResolve } from 'path' import { pathToFileURL } from 'url' import stripAnsi = require('strip-ansi') @@ -6,8 +6,7 @@ import Concat from 'concat-with-sourcemaps' import { test } from 'uvu' import { is, equal, match, type } from 'uvu/assert' -import postcss = require('../lib/postcss.js') -import { +import postcss, { ProcessOptions, CssSyntaxError, Plugin, diff --git a/test/document.test.ts b/test/document.test.ts index 941beb222..7d81e3e9c 100755 --- a/test/document.test.ts +++ b/test/document.test.ts @@ -2,7 +2,7 @@ import { test } from 'uvu' import { is, match } from 'uvu/assert' import { Result, parse } from '../lib/postcss.js' -import Document = require('../lib/document.js') +import Document from '../lib/document.js' test('generates result without map', () => { let root = parse('a {}') diff --git a/test/fromJSON.test.ts b/test/fromJSON.test.ts index 0344ecf20..92e2cc081 100755 --- a/test/fromJSON.test.ts +++ b/test/fromJSON.test.ts @@ -2,8 +2,7 @@ import * as v8 from 'v8' import { test } from 'uvu' import { is, instance, throws } from 'uvu/assert' -import postcss = require('../lib/postcss.js') -import { Root, Declaration, Input, Rule } from '../lib/postcss.js' +import postcss, { Root, Declaration, Input, Rule } from '../lib/postcss.js' test('rehydrates a JSON AST', () => { let cssWithMap = postcss().process( diff --git a/test/lazy-result.test.ts b/test/lazy-result.test.ts index 341815c66..0cace6445 100755 --- a/test/lazy-result.test.ts +++ b/test/lazy-result.test.ts @@ -2,8 +2,8 @@ import { is, equal, type } from 'uvu/assert' import { test } from 'uvu' import { SourceMapGenerator } from 'source-map-js' -import LazyResult = require('../lib/lazy-result.js') -import Processor = require('../lib/processor.js') +import LazyResult from '../lib/lazy-result.js' +import Processor from '../lib/processor.js' let processor = new Processor() diff --git a/test/map.test.ts b/test/map.test.ts index 0e0f7da49..08847464f 100755 --- a/test/map.test.ts +++ b/test/map.test.ts @@ -6,9 +6,8 @@ import { pathToFileURL } from 'url' import { existsSync } from 'fs' import { test } from 'uvu' -import postcss = require('../lib/postcss.js') -import { FilePosition, SourceMap, Rule, Root } from '../lib/postcss.js' -import PreviousMap = require('../lib/previous-map.js') +import postcss, { SourceMap, Rule, Root } from '../lib/postcss.js' +import PreviousMap from '../lib/previous-map.js' function consumer(map: SourceMap): any { return (SourceMapConsumer as any).fromSourceMap(map) @@ -736,7 +735,7 @@ test('supports previous inline map with empty processor', () => { to: '/c.css' }) let root3 = postcss.parse(result2.css, { from: '/c.css' }) - match((root3.source?.input!.origin(1, 0) as FilePosition).file!, 'a.css') + match((root3.source?.input.origin(1, 0) as any).file, 'a.css') }) test('absolute sourcemaps have source contents', () => { diff --git a/test/no-work-result.test.ts b/test/no-work-result.test.ts index ef3c9c40d..bffcda294 100644 --- a/test/no-work-result.test.ts +++ b/test/no-work-result.test.ts @@ -1,10 +1,10 @@ import { is, type, equal, throws, not, instance } from 'uvu/assert' import { test } from 'uvu' -import { SourceMapGenerator } from 'source-map-js' +import mozilla from 'source-map-js' import { spy } from 'nanospy' -import NoWorkResult = require('../lib/no-work-result.js') -import Processor = require('../lib/processor.js') +import NoWorkResult from '../lib/no-work-result.js' +import Processor from '../lib/processor.js' let processor = new Processor() @@ -70,7 +70,7 @@ test('has map only if necessary', () => { from: '/a.css', map: { inline: false } }) - is(result3.map instanceof SourceMapGenerator, true) + is(result3.map instanceof mozilla.SourceMapGenerator, true) }) test('contains simple properties', () => { diff --git a/test/node.test.ts b/test/node.test.ts index 90652e36a..bbc211a7a 100755 --- a/test/node.test.ts +++ b/test/node.test.ts @@ -2,8 +2,7 @@ import { resolve } from 'path' import { test } from 'uvu' import { is, equal, type, not } from 'uvu/assert' -import postcss = require('../lib/postcss.js') -import { +import postcss, { AnyNode, AtRule, Root, diff --git a/test/postcss.test.ts b/test/postcss.test.ts index 0d7dd7828..7c61c0123 100755 --- a/test/postcss.test.ts +++ b/test/postcss.test.ts @@ -2,9 +2,8 @@ import { is, type, equal, match, throws } from 'uvu/assert' import { spyOn, restoreAll } from 'nanospy' import { test } from 'uvu' -import postcss = require('../lib/postcss.js') -import { Root, PluginCreator } from '../lib/postcss.js' -import Processor = require('../lib/processor.js') +import postcss, { Root, PluginCreator } from '../lib/postcss.js' +import Processor from '../lib/processor.js' test.after.each(() => { restoreAll() diff --git a/test/processor.test.ts b/test/processor.test.ts index 3e801eff3..d232ab2c2 100755 --- a/test/processor.test.ts +++ b/test/processor.test.ts @@ -5,8 +5,7 @@ import { spyOn, restoreAll } from 'nanospy' import { delay } from 'nanodelay' import { test } from 'uvu' -import postcss = require('../lib/postcss.js') -import { +import postcss, { Plugin, Result, Node, @@ -17,11 +16,11 @@ import { Parser, Stringifier } from '../lib/postcss.js' -import CssSyntaxError = require('../lib/css-syntax-error.js') -import LazyResult = require('../lib/lazy-result.js') -import NoWorkResult = require('../lib/no-work-result.js') -import Processor = require('../lib/processor.js') -import Rule = require('../lib/rule.js') +import CssSyntaxError from '../lib/css-syntax-error.js' +import LazyResult from '../lib/lazy-result.js' +import NoWorkResult from '../lib/no-work-result.js' +import Processor from '../lib/processor.js' +import Rule from '../lib/rule.js' test.after.each(() => { restoreAll() diff --git a/test/result.test.ts b/test/result.test.ts index ea411b568..6c0328bb8 100755 --- a/test/result.test.ts +++ b/test/result.test.ts @@ -1,9 +1,8 @@ import { test } from 'uvu' import { is, equal } from 'uvu/assert' -import postcss = require('../lib/postcss.js') -import { Warning, Result, Root, Plugin } from '../lib/postcss.js' -import Processor = require('../lib/processor.js') +import postcss, { Warning, Result, Root, Plugin } from '../lib/postcss.js' +import Processor from '../lib/processor.js' let processor = new Processor() let root = new Root() diff --git a/test/types.ts b/test/types.ts index a18b9fcd7..7ff9e8929 100644 --- a/test/types.ts +++ b/test/types.ts @@ -1,5 +1,4 @@ -import postcss = require('../lib/postcss.js') -import { Result, PluginCreator } from '../lib/postcss.js' +import postcss, { Result, PluginCreator } from '../lib/postcss.js' const plugin: PluginCreator = prop => { return { diff --git a/test/visitor.test.ts b/test/visitor.test.ts index 71ac5d7c9..7d71a615d 100755 --- a/test/visitor.test.ts +++ b/test/visitor.test.ts @@ -4,8 +4,7 @@ import { delay } from 'nanodelay' import { test } from 'uvu' import { is, type, equal, throws } from 'uvu/assert' -import postcss = require('../lib/postcss.js') -import { +import postcss, { Container, Root, Rule, From 18f5ce3b0919c5cafb33a2f9ae5f69f38b491f5c Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sun, 26 Mar 2023 16:08:31 +0200 Subject: [PATCH 08/16] Add test to assert postcss is postcss.default --- test/postcss.test.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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) From 206938a0999e86ade9390a2ec5a0603469a15189 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sun, 26 Mar 2023 16:12:49 +0200 Subject: [PATCH 09/16] Fix broken import --- test/no-work-result.test.ts | 4 ++-- tsconfig.json | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) 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/tsconfig.json b/tsconfig.json index 9437d40cb..37dc54f8b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,6 @@ "module": "commonjs", "moduleResolution": "node16", "strict": true, - "noEmit": true, - "skipLibCheck": true + "noEmit": true } } From 452ba619472a3c6703eca9d1753a5d5a779a03d8 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Mon, 27 Mar 2023 18:08:45 +0200 Subject: [PATCH 10/16] Fix default exports with inheritance --- lib/at-rule.d.ts | 14 ++++++-------- lib/comment.d.ts | 12 ++++++++---- lib/container.d.ts | 12 ++++++++---- lib/css-syntax-error.d.ts | 12 ++++++++---- lib/declaration.d.ts | 10 ++++++++-- lib/document.d.ts | 16 ++++++++-------- lib/input.d.ts | 12 ++++++++---- lib/lazy-result.d.ts | 12 +++++++++--- lib/no-work-result.d.ts | 12 +++++++++--- lib/node.d.ts | 26 +++++++++++++++----------- lib/previous-map.d.ts | 12 +++++++++--- lib/processor.d.ts | 12 +++++++++--- lib/result.d.ts | 15 ++++++++++----- lib/root.d.ts | 15 +++++++-------- lib/rule.d.ts | 15 +++++++-------- lib/stringifier.d.ts | 10 ++++++++-- lib/warning.d.ts | 12 ++++++++---- 17 files changed, 145 insertions(+), 84 deletions(-) diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index 4dc6d96c2..38c498744 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -37,7 +37,7 @@ interface AtRuleRaws extends Record { } declare namespace AtRule { - interface AtRuleProps extends Container.ContainerProps { + export interface AtRuleProps extends Container.ContainerProps { /** Name of the at-rule. */ name: string /** Parameters following the name of the at-rule. */ @@ -45,12 +45,10 @@ declare namespace AtRule { /** Information used to generate byte-to-byte equal node string as it was in the origin input. */ raws?: AtRuleRaws } -} - -interface AtRuleCtor { - default: AtRuleCtor - new (defaults?: AtRule.AtRuleProps): AtRule + // eslint-disable-next-line @typescript-eslint/no-shadow + class AtRule extends AtRule_ {} + export { AtRule as default } } /** @@ -78,7 +76,7 @@ interface AtRuleCtor { * media.nodes //=> [] * ``` */ -interface AtRule extends Container { +declare class AtRule_ extends Container.default { type: 'atrule' parent: Container | undefined raws: AtRuleRaws @@ -112,6 +110,6 @@ interface AtRule extends Container { cloneAfter(overrides?: Partial): this } -declare const AtRule: AtRuleCtor +declare class AtRule extends AtRule_ {} export = AtRule diff --git a/lib/comment.d.ts b/lib/comment.d.ts index 062d3eddb..3477234b9 100644 --- a/lib/comment.d.ts +++ b/lib/comment.d.ts @@ -19,12 +19,16 @@ interface CommentRaws extends Record { } declare namespace Comment { - interface CommentProps extends Node.NodeProps { + export interface CommentProps extends Node.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-shadow + class Comment extends Comment_ {} + export { Comment as default } } /** @@ -40,9 +44,7 @@ declare namespace Comment { * Comments inside selectors, at-rule parameters, or declaration values * will be stored in the `raws` properties explained above. */ -declare class Comment extends Node { - static default: typeof Comment - +declare class Comment_ extends Node { type: 'comment' parent: Container | undefined raws: CommentRaws @@ -59,4 +61,6 @@ declare class Comment extends Node { cloneAfter(overrides?: Partial): this } +declare class Comment extends Comment_ {} + export = Comment diff --git a/lib/container.d.ts b/lib/container.d.ts index 49d1d0347..49d054408 100644 --- a/lib/container.d.ts +++ b/lib/container.d.ts @@ -17,9 +17,13 @@ interface ValueOptions { } declare namespace Container { - interface ContainerProps extends Node.NodeProps { + export interface ContainerProps extends Node.NodeProps { nodes?: (Node.ChildNode | Node.ChildProps)[] } + + // eslint-disable-next-line @typescript-eslint/no-shadow + class Container extends Container_ {} + export { Container as default } } /** @@ -29,11 +33,9 @@ declare namespace Container { * Note that all containers can store any content. If you write a rule inside * a rule, PostCSS will parse it. */ -declare abstract class Container< +declare abstract class Container_< Child extends Node = Node.ChildNode > extends Node { - static default: typeof Container - /** * An array containing the container’s children. * @@ -445,4 +447,6 @@ declare 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 f89b2f236..2e7ea8f0b 100644 --- a/lib/css-syntax-error.d.ts +++ b/lib/css-syntax-error.d.ts @@ -4,7 +4,7 @@ declare namespace CssSyntaxError { /** * A position that is part of a range. */ - interface RangePosition { + export interface RangePosition { /** * The line number in the input. */ @@ -15,6 +15,10 @@ declare namespace CssSyntaxError { */ column: number } + + // eslint-disable-next-line @typescript-eslint/no-shadow + class CssSyntaxError extends CssSyntaxError_ {} + export { CssSyntaxError as default } } /** @@ -46,9 +50,7 @@ declare namespace CssSyntaxError { * } * ``` */ -declare class CssSyntaxError { - static default: typeof CssSyntaxError - +declare class CssSyntaxError_ { /** * Instantiates a CSS syntax error. Can be instantiated for a single position * or for a range. @@ -242,4 +244,6 @@ declare 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 c851ef39e..87c1cc4b8 100644 --- a/lib/declaration.d.ts +++ b/lib/declaration.d.ts @@ -28,7 +28,7 @@ interface DeclarationRaws extends Record { } declare namespace Declaration { - interface DeclarationProps { + export interface DeclarationProps { /** Name of the declaration. */ prop: string /** Value of the declaration. */ @@ -38,6 +38,10 @@ declare namespace Declaration { /** 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-shadow + class Declaration extends Declaration_ {} + export { Declaration as default } } /** @@ -57,7 +61,7 @@ declare namespace Declaration { * decl.toString() //=> ' color: black' * ``` */ -declare class Declaration extends Node { +declare class Declaration_ extends Node { static default: typeof Declaration type: 'decl' @@ -127,4 +131,6 @@ declare class Declaration extends Node { cloneAfter(overrides?: Partial): this } +declare class Declaration extends Declaration_ {} + export = Declaration diff --git a/lib/document.d.ts b/lib/document.d.ts index 1982eb9d1..941bea2aa 100644 --- a/lib/document.d.ts +++ b/lib/document.d.ts @@ -4,7 +4,7 @@ import Result = require('./result.js') import Root = require('./root.js') declare namespace Document { - interface DocumentProps extends Container.ContainerProps { + export interface DocumentProps extends Container.ContainerProps { nodes?: Root[] /** @@ -15,12 +15,10 @@ declare namespace Document { */ raws?: Record } -} - -interface DocumentCtor { - default: DocumentCtor - new (defaults?: Document.DocumentProps): Document + // eslint-disable-next-line @typescript-eslint/no-shadow + class Document extends Document_ {} + export { Document as default } } /** @@ -37,7 +35,9 @@ interface DocumentCtor { * document.nodes.length //=> 2 * ``` */ -interface Document extends Container { +declare class Document_ extends Container.default { + constructor(defaults?: Document.DocumentProps) + type: 'document' parent: undefined @@ -59,6 +59,6 @@ interface Document extends Container { toResult(options?: ProcessOptions): Result } -declare const Document: DocumentCtor +declare class Document extends Document_ {} export = Document diff --git a/lib/input.d.ts b/lib/input.d.ts index 8af526f34..b7e809545 100644 --- a/lib/input.d.ts +++ b/lib/input.d.ts @@ -2,7 +2,7 @@ import { CssSyntaxError, ProcessOptions } from './postcss.js' import PreviousMap = require('./previous-map.js') declare namespace Input { - interface FilePosition { + export interface FilePosition { /** * URL for the source file. */ @@ -38,6 +38,10 @@ declare namespace Input { */ source?: string } + + // eslint-disable-next-line @typescript-eslint/no-shadow + class Input extends Input_ {} + export { Input as default } } /** @@ -48,9 +52,7 @@ declare namespace Input { * const input = root.source.input * ``` */ -declare class Input { - static default: typeof Input - +declare class Input_ { /** * Input CSS source. * @@ -188,4 +190,6 @@ declare class Input { ): CssSyntaxError } +declare class Input extends Input_ {} + export = Input diff --git a/lib/lazy-result.d.ts b/lib/lazy-result.d.ts index 396535a90..6ef2f3d31 100644 --- a/lib/lazy-result.d.ts +++ b/lib/lazy-result.d.ts @@ -4,6 +4,12 @@ import Processor = require('./processor.js') import Warning = require('./warning.js') import Root = require('./root.js') +declare namespace LazyResult { + // eslint-disable-next-line @typescript-eslint/no-shadow + class LazyResult extends LazyResult_ {} + export { LazyResult as default } +} + /** * A Promise proxy for the result of PostCSS transformations. * @@ -13,9 +19,7 @@ import Root = require('./root.js') * const lazy = postcss([autoprefixer]).process(css) * ``` */ -declare class LazyResult implements PromiseLike { - static default: typeof LazyResult - +declare class LazyResult_ implements PromiseLike { /** * Processes input CSS through synchronous and asynchronous plugins * and calls `onFulfilled` with a Result instance. If a plugin throws @@ -177,4 +181,6 @@ declare class LazyResult implements PromiseLike { async(): Promise } +declare class LazyResult extends LazyResult_ {} + export = LazyResult diff --git a/lib/no-work-result.d.ts b/lib/no-work-result.d.ts index eaed419d8..cbcfd2a1d 100644 --- a/lib/no-work-result.d.ts +++ b/lib/no-work-result.d.ts @@ -5,6 +5,12 @@ import Warning = require('./warning.js') import Root = require('./root.js') import LazyResult = require('./lazy-result.js') +declare namespace NoWorkResult { + // eslint-disable-next-line @typescript-eslint/no-shadow + class NoWorkResult extends NoWorkResult_ {} + 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,9 +23,7 @@ import LazyResult = require('./lazy-result.js') * let root = noWorkResult.root // now css is parsed because we accessed the root * ``` */ -declare class NoWorkResult implements LazyResult { - static default: typeof NoWorkResult - +declare class NoWorkResult_ implements LazyResult { then: Promise['then'] catch: Promise['catch'] finally: Promise['finally'] @@ -38,4 +42,6 @@ declare class NoWorkResult implements LazyResult { async(): Promise } +declare class NoWorkResult extends NoWorkResult_ {} + export = NoWorkResult diff --git a/lib/node.d.ts b/lib/node.d.ts index 0eb22e5a3..6db899687 100644 --- a/lib/node.d.ts +++ b/lib/node.d.ts @@ -12,17 +12,17 @@ import Document = require('./document.js') import Container = require('./container.js') declare namespace Node { - type ChildNode = AtRule | Rule | Declaration | Comment + export type ChildNode = AtRule | Rule | Declaration | Comment - type AnyNode = AtRule | Rule | Declaration | Comment | Root | Document + export type AnyNode = AtRule | Rule | Declaration | Comment | Root | Document - type ChildProps = + export type ChildProps = | AtRule.AtRuleProps | Rule.RuleProps | Declaration.DeclarationProps | Comment.CommentProps - interface Position { + export interface Position { /** * Source offset in file. It starts from 0. */ @@ -39,7 +39,7 @@ declare namespace Node { line: number } - interface Range { + export interface Range { /** * Start position, inclusive. */ @@ -51,7 +51,7 @@ declare namespace Node { end: Position } - interface Source { + export interface Source { /** * The file source of the node. */ @@ -66,11 +66,11 @@ declare namespace Node { end?: Position } - interface NodeProps { + export interface NodeProps { source?: Source } - interface NodeErrorOptions { + export interface NodeErrorOptions { /** * Plugin name that created this error. PostCSS will set it automatically. */ @@ -91,6 +91,10 @@ declare namespace Node { */ endIndex?: number } + + // eslint-disable-next-line @typescript-eslint/no-shadow + class Node extends Node_ {} + export { Node as default } } /** @@ -99,9 +103,7 @@ declare namespace Node { * You should not extend this classes to create AST for selector or value * parser. */ -declare abstract class Node { - static default: typeof Node - +declare abstract class Node_ { /** * tring representing the node’s type. Possible values are `root`, `atrule`, * `rule`, `decl`, or `comment`. @@ -481,4 +483,6 @@ declare abstract class Node { rangeBy(opts?: Pick): Node.Range } +declare class Node extends Node_ {} + export = Node diff --git a/lib/previous-map.d.ts b/lib/previous-map.d.ts index c972f05bd..04ec275f6 100644 --- a/lib/previous-map.d.ts +++ b/lib/previous-map.d.ts @@ -2,6 +2,12 @@ import { SourceMapConsumer } from 'source-map-js' import { ProcessOptions } from './postcss.js' +declare namespace PreviousMap { + // eslint-disable-next-line @typescript-eslint/no-shadow + class PreviousMap extends PreviousMap_ {} + export { PreviousMap as default } +} + /** * Source map information from input CSS. * For example, source map after Sass compiler. @@ -14,9 +20,7 @@ import { ProcessOptions } from './postcss.js' * root.input.map //=> PreviousMap * ``` */ -declare class PreviousMap { - static default: typeof PreviousMap - +declare class PreviousMap_ { /** * Was source map inlined by data-uri to input CSS. */ @@ -73,4 +77,6 @@ declare class PreviousMap { withContent(): boolean } +declare class PreviousMap extends PreviousMap_ {} + export = PreviousMap diff --git a/lib/processor.d.ts b/lib/processor.d.ts index 5fdee063f..698803611 100644 --- a/lib/processor.d.ts +++ b/lib/processor.d.ts @@ -10,6 +10,12 @@ import Result = require('./result.js') import Root = require('./root.js') import NoWorkResult = require('./no-work-result.js') +declare namespace Processor { + // eslint-disable-next-line @typescript-eslint/no-shadow + class Processor extends Processor_ {} + 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,9 +26,7 @@ import NoWorkResult = require('./no-work-result.js') * processor.process(css2).then(result => console.log(result.css)) * ``` */ -declare class Processor { - static default: typeof Processor - +declare class Processor_ { /** * Current PostCSS version. * @@ -103,4 +107,6 @@ declare class Processor { ): LazyResult | NoWorkResult } +declare class Processor extends Processor_ {} + export = Processor diff --git a/lib/result.d.ts b/lib/result.d.ts index 83335ed2f..ebc5fb467 100644 --- a/lib/result.d.ts +++ b/lib/result.d.ts @@ -12,7 +12,7 @@ import { import Processor = require('./processor.js') declare namespace Result { - interface Message { + export interface Message { /** * Message type. */ @@ -26,7 +26,7 @@ declare namespace Result { [others: string]: any } - interface ResultOptions extends ProcessOptions { + export interface ResultOptions extends ProcessOptions { /** * The CSS node that was the source of the warning. */ @@ -38,6 +38,11 @@ declare namespace Result { */ plugin?: string } + + + // eslint-disable-next-line @typescript-eslint/no-shadow + class Result extends Result_ {} + export { Result as default } } /** @@ -56,9 +61,7 @@ declare namespace Result { * const result2 = postcss.parse(css).toResult() * ``` */ -declare class Result { - static default: typeof Result - +declare class Result_ { /** * The Processor instance used for this transformation. * @@ -199,4 +202,6 @@ declare class Result { warnings(): Warning[] } +declare class Result extends Result_ {} + export = Result diff --git a/lib/root.d.ts b/lib/root.d.ts index b48929da5..991a8cda0 100644 --- a/lib/root.d.ts +++ b/lib/root.d.ts @@ -32,19 +32,17 @@ interface RootRaws extends Record { } declare namespace Root { - interface RootProps extends Container.ContainerProps { + export interface RootProps extends Container.ContainerProps { /** * Information used to generate byte-to-byte equal node string * as it was in the origin input. * */ raws?: RootRaws } -} - -interface RootCtor { - default: RootCtor - new (defaults?: Root.RootProps): Root + // eslint-disable-next-line @typescript-eslint/no-shadow + class Root extends Root_ {} + export { Root as default } } /** @@ -56,7 +54,8 @@ interface RootCtor { * root.nodes.length //=> 2 * ``` */ -interface Root extends Container { +declare class Root_ extends Container.default { + constructor(defaults?: Root.RootProps) type: 'root' parent: Document | undefined raws: RootRaws @@ -79,6 +78,6 @@ interface Root extends Container { assign(overrides: object | Root.RootProps): this } -declare const Root: RootCtor +declare class Root extends Root_ {} export = Root diff --git a/lib/rule.d.ts b/lib/rule.d.ts index 8d4263e5f..bd5c57f38 100644 --- a/lib/rule.d.ts +++ b/lib/rule.d.ts @@ -37,7 +37,7 @@ interface RuleRaws extends Record { } declare namespace Rule { - interface RuleProps extends Container.ContainerProps { + export interface RuleProps extends Container.ContainerProps { /** Selector or selectors of the rule. */ selector?: string /** Selectors of the rule represented as an array of strings. */ @@ -45,12 +45,10 @@ declare namespace Rule { /** Information used to generate byte-to-byte equal node string as it was in the origin input. */ raws?: RuleRaws } -} - -interface RuleCtor { - default: RuleCtor - new (defaults?: Rule.RuleProps): Rule + // eslint-disable-next-line @typescript-eslint/no-shadow + class Rule extends Rule_ {} + export { Rule as default } } /** @@ -71,7 +69,8 @@ interface RuleCtor { * rule.toString() //=> 'a{}' * ``` */ -interface Rule extends Container { +declare class Rule_ extends Container.default { + constructor(defaults?: Rule.RuleProps) type: 'rule' parent: Container | undefined raws: RuleRaws @@ -110,6 +109,6 @@ interface Rule extends Container { cloneAfter(overrides?: Partial): this } -declare const Rule: RuleCtor +declare class Rule extends Rule_ {} export = Rule diff --git a/lib/stringifier.d.ts b/lib/stringifier.d.ts index 52aca8d91..957db53e5 100644 --- a/lib/stringifier.d.ts +++ b/lib/stringifier.d.ts @@ -10,9 +10,13 @@ import { Container } from './postcss.js' -declare class Stringifier { - static default: typeof Stringifier +declare namespace Stringifier { + // eslint-disable-next-line @typescript-eslint/no-shadow + class Stringifier extends Stringifier_ {} + export { Stringifier as default } +} +declare class Stringifier_ { builder: Builder constructor(builder: Builder) stringify(node: AnyNode, semicolon?: boolean): void @@ -38,4 +42,6 @@ declare class Stringifier { rawValue(node: AnyNode, prop: string): string } +declare class Stringifier extends Stringifier_ {} + export = Stringifier diff --git a/lib/warning.d.ts b/lib/warning.d.ts index 120c38a6c..6386e2fea 100644 --- a/lib/warning.d.ts +++ b/lib/warning.d.ts @@ -2,7 +2,7 @@ import { RangePosition } from './css-syntax-error.js' import Node = require('./node.js') declare namespace Warning { - interface WarningOptions { + export interface WarningOptions { /** * CSS node that caused the warning. */ @@ -39,6 +39,10 @@ declare namespace Warning { */ plugin?: string } + + // eslint-disable-next-line @typescript-eslint/no-shadow + class Warning extends Warning_ {} + export { Warning as default } } /** @@ -50,9 +54,7 @@ declare namespace Warning { * } * ``` */ -declare class Warning { - static default: Warning - +declare class Warning_ { /** * Type to filter warnings from `Result#messages`. * Always equal to `"warning"`. @@ -141,4 +143,6 @@ declare class Warning { toString(): string } +declare class Warning extends Warning_ {} + export = Warning From 31024785c654d5542579a8d6fccafa2c92c31338 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Mon, 27 Mar 2023 18:35:01 +0200 Subject: [PATCH 11/16] Use ESM imports in type definitions --- lib/at-rule.d.ts | 6 +- lib/comment.d.ts | 6 +- lib/container.d.ts | 30 ++-- lib/declaration.d.ts | 4 +- lib/document.d.ts | 10 +- lib/input.d.ts | 2 +- lib/lazy-result.d.ts | 14 +- lib/no-work-result.d.ts | 16 +- lib/node.d.ts | 39 ++--- lib/postcss.d.ts | 326 ++++++++++++++++++++-------------------- lib/processor.d.ts | 8 +- lib/result.d.ts | 2 +- lib/root.d.ts | 10 +- lib/rule.d.ts | 6 +- lib/warning.d.ts | 2 +- 15 files changed, 238 insertions(+), 243 deletions(-) diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index 38c498744..bb4ae3aba 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -1,4 +1,4 @@ -import Container = require('./container.js') +import Container, { ContainerProps } from './container.js' interface AtRuleRaws extends Record { /** @@ -37,7 +37,7 @@ interface AtRuleRaws extends Record { } declare namespace AtRule { - export interface AtRuleProps extends Container.ContainerProps { + export interface AtRuleProps extends ContainerProps { /** Name of the at-rule. */ name: string /** Parameters following the name of the at-rule. */ @@ -76,7 +76,7 @@ declare namespace AtRule { * media.nodes //=> [] * ``` */ -declare class AtRule_ extends Container.default { +declare class AtRule_ extends Container { type: 'atrule' parent: Container | undefined raws: AtRuleRaws diff --git a/lib/comment.d.ts b/lib/comment.d.ts index 3477234b9..dcdd75bcc 100644 --- a/lib/comment.d.ts +++ b/lib/comment.d.ts @@ -1,5 +1,5 @@ -import Container = require('./container.js') -import Node = require('./node.js') +import Container from './container.js' +import Node, { NodeProps } from './node.js' interface CommentRaws extends Record { /** @@ -19,7 +19,7 @@ interface CommentRaws extends Record { } declare namespace Comment { - export interface CommentProps extends Node.NodeProps { + 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. */ diff --git a/lib/container.d.ts b/lib/container.d.ts index 49d054408..5ef685f38 100644 --- a/lib/container.d.ts +++ b/lib/container.d.ts @@ -1,8 +1,8 @@ -import Node = require('./node.js') -import Declaration = require('./declaration.js') -import Comment = require('./comment.js') -import AtRule = require('./at-rule.js') -import Rule = require('./rule.js') +import Node, { ChildNode, NodeProps, ChildProps } from './node.js' +import Declaration from './declaration.js' +import Comment from './comment.js' +import AtRule from './at-rule.js' +import Rule from './rule.js' interface ValueOptions { /** @@ -17,12 +17,12 @@ interface ValueOptions { } declare namespace Container { - export interface ContainerProps extends Node.NodeProps { - nodes?: (Node.ChildNode | Node.ChildProps)[] + export interface ContainerProps extends NodeProps { + nodes?: (ChildNode | ChildProps)[] } // eslint-disable-next-line @typescript-eslint/no-shadow - class Container extends Container_ {} + class Container extends Container_ {} export { Container as default } } @@ -34,7 +34,7 @@ declare namespace Container { * a rule, PostCSS will parse it. */ declare abstract class Container_< - Child extends Node = Node.ChildNode + Child extends Node = ChildNode > extends Node { /** * An array containing the container’s children. @@ -123,7 +123,7 @@ declare abstract class Container_< * @return Returns `false` if iteration was broke. */ walk( - callback: (node: Node.ChildNode, index: number) => false | void + callback: (node: ChildNode, index: number) => false | void ): false | undefined /** @@ -275,7 +275,7 @@ declare abstract class Container_< * @return This node for methods chain. */ append( - ...nodes: (Node | Node[] | Node.ChildProps | Node.ChildProps[] | string | string[])[] + ...nodes: (Node | Node[] | ChildProps | ChildProps[] | string | string[])[] ): this /** @@ -299,7 +299,7 @@ declare abstract class Container_< * @return This node for methods chain. */ prepend( - ...nodes: (Node | Node[] | Node.ChildProps | Node.ChildProps[] | string | string[])[] + ...nodes: (Node | Node[] | ChildProps | ChildProps[] | string | string[])[] ): this /** @@ -327,7 +327,7 @@ declare abstract class Container_< */ insertBefore( oldNode: Child | number, - newNode: Child | Node.ChildProps | string | Child[] | Node.ChildProps[] | string[] + newNode: Child | ChildProps | string | Child[] | ChildProps[] | string[] ): this /** @@ -339,7 +339,7 @@ declare abstract class Container_< */ insertAfter( oldNode: Child | number, - newNode: Child | Node.ChildProps | string | Child[] | Node.ChildProps[] | string[] + newNode: Child | ChildProps | string | Child[] | ChildProps[] | string[] ): this /** @@ -447,6 +447,6 @@ declare abstract class Container_< index(child: Child | number): number } -declare class Container extends Container_ {} +declare class Container extends Container_ {} export = Container diff --git a/lib/declaration.d.ts b/lib/declaration.d.ts index 87c1cc4b8..14d2fe945 100644 --- a/lib/declaration.d.ts +++ b/lib/declaration.d.ts @@ -1,5 +1,5 @@ -import Container = require('./container.js') -import Node = require('./node.js') +import Container from './container.js' +import Node from './node.js' interface DeclarationRaws extends Record { /** diff --git a/lib/document.d.ts b/lib/document.d.ts index 941bea2aa..3fff07bdc 100644 --- a/lib/document.d.ts +++ b/lib/document.d.ts @@ -1,10 +1,10 @@ -import Container = require('./container.js') +import Container, { ContainerProps } from './container.js' import { ProcessOptions } from './postcss.js' -import Result = require('./result.js') -import Root = require('./root.js') +import Result from './result.js' +import Root from './root.js' declare namespace Document { - export interface DocumentProps extends Container.ContainerProps { + export interface DocumentProps extends ContainerProps { nodes?: Root[] /** @@ -35,7 +35,7 @@ declare namespace Document { * document.nodes.length //=> 2 * ``` */ -declare class Document_ extends Container.default { +declare class Document_ extends Container { constructor(defaults?: Document.DocumentProps) type: 'document' diff --git a/lib/input.d.ts b/lib/input.d.ts index b7e809545..03e63df8b 100644 --- a/lib/input.d.ts +++ b/lib/input.d.ts @@ -1,5 +1,5 @@ import { CssSyntaxError, ProcessOptions } from './postcss.js' -import PreviousMap = require('./previous-map.js') +import PreviousMap from './previous-map.js' declare namespace Input { export interface FilePosition { diff --git a/lib/lazy-result.d.ts b/lib/lazy-result.d.ts index 6ef2f3d31..5b98980fd 100644 --- a/lib/lazy-result.d.ts +++ b/lib/lazy-result.d.ts @@ -1,8 +1,8 @@ -import Result = require('./result.js') +import Result, { Message, ResultOptions } from './result.js' import { SourceMap } from './postcss.js' -import Processor = require('./processor.js') -import Warning = require('./warning.js') -import Root = require('./root.js') +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-shadow @@ -70,7 +70,7 @@ declare class LazyResult_ implements PromiseLike { * @param css CSS to parse and transform. * @param opts Options from the `Processor#process` or `Root#toResult`. */ - constructor(processor: Processor, css: string, opts: Result.ResultOptions) + constructor(processor: Processor, css: string, opts: ResultOptions) /** * Returns the default string description of an object. @@ -87,7 +87,7 @@ declare class LazyResult_ implements PromiseLike { /** * Options from the `Processor#process` call. */ - get opts(): Result.ResultOptions + get opts(): ResultOptions /** * Processes input CSS through synchronous plugins, converts `Root` @@ -145,7 +145,7 @@ declare class LazyResult_ implements PromiseLike { * * PostCSS runners should always use `LazyResult#then`. */ - get messages(): Result.Message[] + get messages(): Message[] /** * Processes input CSS through synchronous plugins diff --git a/lib/no-work-result.d.ts b/lib/no-work-result.d.ts index cbcfd2a1d..9e247b846 100644 --- a/lib/no-work-result.d.ts +++ b/lib/no-work-result.d.ts @@ -1,9 +1,9 @@ -import Result = require('./result.js') +import Result, { Message, ResultOptions } from './result.js' import { SourceMap } from './postcss.js' -import Processor = require('./processor.js') -import Warning = require('./warning.js') -import Root = require('./root.js') -import LazyResult = require('./lazy-result.js') +import Processor from './processor.js' +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-shadow @@ -27,15 +27,15 @@ declare class NoWorkResult_ implements LazyResult { then: Promise['then'] catch: Promise['catch'] finally: Promise['finally'] - constructor(processor: Processor, css: string, opts: Result.ResultOptions) + constructor(processor: Processor, css: string, opts: ResultOptions) get [Symbol.toStringTag](): string get processor(): Processor - get opts(): Result.ResultOptions + get opts(): ResultOptions get css(): string get content(): string get map(): SourceMap get root(): Root - get messages(): Result.Message[] + get messages(): Message[] warnings(): Warning[] toString(): string sync(): Result diff --git a/lib/node.d.ts b/lib/node.d.ts index 6db899687..00f62b77a 100644 --- a/lib/node.d.ts +++ b/lib/node.d.ts @@ -1,26 +1,27 @@ -import Declaration = require('./declaration.js') -import Comment = require('./comment.js') +import Declaration, { DeclarationProps } from './declaration.js' +import Comment, { CommentProps } from './comment.js' import { Stringifier, Syntax } from './postcss.js' import AtRule = require('./at-rule.js') -import Rule = require('./rule.js') -import Warning = require('./warning.js') -import CssSyntaxError = require('./css-syntax-error.js') -import Result = require('./result.js') -import Input = require('./input.js') -import Root = require('./root.js') -import Document = require('./document.js') -import Container = require('./container.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' +import Result from './result.js' +import Input from './input.js' +import Root from './root.js' +import Document from './document.js' +import Container from './container.js' declare namespace Node { - export type ChildNode = AtRule | Rule | Declaration | Comment + export type ChildNode = AtRule.default | Rule | Declaration | Comment - export type AnyNode = AtRule | Rule | Declaration | Comment | Root | Document + export type AnyNode = AtRule.default | Rule | Declaration | Comment | Root | Document export type ChildProps = - | AtRule.AtRuleProps - | Rule.RuleProps - | Declaration.DeclarationProps - | Comment.CommentProps + | AtRuleProps + | RuleProps + | DeclarationProps + | CommentProps export interface Position { /** @@ -247,7 +248,7 @@ declare abstract class Node_ { * * @return Created warning object. */ - warn(result: Result, text: string, opts?: Warning.WarningOptions): Warning + warn(result: Result, text: string, opts?: WarningOptions): Warning /** * Removes the node from its parent and cleans the parent properties @@ -471,7 +472,7 @@ declare abstract class Node_ { * @param opts Options. * @return Position. */ - positionBy(opts?: Pick): Node.Position + positionBy(opts?: Pick): Node.Position /** * Get the range for a word or start and end index inside the node. @@ -480,7 +481,7 @@ declare abstract class Node_ { * @param opts Options. * @return Range. */ - rangeBy(opts?: Pick): Node.Range + rangeBy(opts?: Pick): Node.Range } declare class Node extends Node_ {} diff --git a/lib/postcss.d.ts b/lib/postcss.d.ts index 5ec661981..e93db9aa9 100644 --- a/lib/postcss.d.ts +++ b/lib/postcss.d.ts @@ -1,20 +1,28 @@ import { SourceMapGenerator, RawSourceMap } from 'source-map-js' -import Node_ = require('./node.js') -import Declaration_ = require('./declaration.js') -import Container_ = require('./container.js') -import Document_ = require('./document.js') -import Warning_ = require('./warning.js') -import Comment_ = require('./comment.js') -import AtRule_ = require('./at-rule.js') -import Input_ = require('./input.js') -import Result_ = require('./result.js') -import Root_ = require('./root.js') -import Rule_ = require('./rule.js') -import CssSyntaxError_ = require('./css-syntax-error.js') -import list = require('./list.js') -import LazyResult_ = require('./lazy-result.js') -import Processor_ = require('./processor.js') +import Node, { + Position, + Source, + ChildNode, + NodeErrorOptions, + NodeProps, + ChildProps, + AnyNode +} from './node.js' +import Declaration, { DeclarationProps } from './declaration.js' +import Container, { ContainerProps } from './container.js' +import Document, { DocumentProps } from './document.js' +import Warning, { WarningOptions } from './warning.js' +import Comment, { CommentProps } from './comment.js' +import AtRule, { AtRuleProps } from './at-rule.js' +import Input, { FilePosition } from './input.js' +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 from './list.js' +import LazyResult from './lazy-result.js' +import Processor from './processor.js' type DocumentProcessor = ( document: postcss.Document, @@ -140,69 +148,72 @@ interface Processors { } declare namespace postcss { - type NodeErrorOptions = Node_.NodeErrorOptions - type DeclarationProps = Declaration_.DeclarationProps - type ContainerProps = Container_.ContainerProps - type WarningOptions = Warning_.WarningOptions - type DocumentProps = Document_.DocumentProps - type FilePosition = Input_.FilePosition - type CommentProps = Comment_.CommentProps - type AtRuleProps = AtRule_.AtRuleProps - type ChildProps = Node_.ChildProps - type LazyResult = LazyResult_ - type ChildNode = Node_.ChildNode - type NodeProps = Node_.NodeProps - type RuleProps = Rule_.RuleProps - type RootProps = Root_.RootProps - type Position = Node_.Position - type AnyNode = Node_.AnyNode - type Message = Result_.Message - type Source = Node_.Source - type CssSyntaxError = CssSyntaxError_ - type Declaration = Declaration_ - type Processor = Processor_ - type Container = Container_ - type Document = Document_ - type Warning = Warning_ - type Comment = Comment_ - type AtRule = AtRule_ - type Result = Result_ - type Input = Input_ - type Node = Node_ - type Rule = Rule_ - type Root = Root_ - - type SourceMap = SourceMapGenerator & { + 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 } - type Helpers = { result: Result; postcss: Postcss } & Postcss + export type Helpers = { result: Result; postcss: Postcss } & Postcss - interface Plugin extends Processors { + export interface Plugin extends Processors { postcssPlugin: string prepare?: (result: Result) => Processors } - interface PluginCreator { + export interface PluginCreator { (opts?: PluginOptions): Plugin | Processor postcss: true } - interface Transformer extends TransformCallback { + export interface Transformer extends TransformCallback { postcssPlugin: string postcssVersion: string } - interface TransformCallback { + export interface TransformCallback { (root: Root, result: Result): Promise | void } - interface OldPlugin extends Transformer { + export interface OldPlugin extends Transformer { (opts?: T): Transformer postcss: Transformer } - type AcceptedPlugin = + export type AcceptedPlugin = | Plugin | PluginCreator | OldPlugin @@ -212,27 +223,27 @@ declare namespace postcss { } | Processor - interface Parser { + export interface Parser { ( css: string | { toString(): string }, opts?: Pick ): RootNode } - interface Builder { + export interface Builder { (part: string, node?: AnyNode, type?: 'start' | 'end'): void } - interface Stringifier { + export interface Stringifier { (node: AnyNode, builder: Builder): void } - interface JSONHydrator { + export interface JSONHydrator { (data: object[]): Node[] (data: object): Node } - interface Syntax { + export interface Syntax { /** * Function to generate AST by string. */ @@ -244,7 +255,7 @@ declare namespace postcss { stringify?: Stringifier } - interface SourceMapOptions { + 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`. @@ -300,7 +311,7 @@ declare namespace postcss { absolute?: boolean } - interface ProcessOptions { + 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. @@ -334,121 +345,104 @@ declare namespace postcss { map?: SourceMapOptions | boolean } - interface Postcss { - default: Postcss + export type Postcss = typeof 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?: postcss.AcceptedPlugin[]): Processor - (...plugins: postcss.AcceptedPlugin[]): Processor + /** + * Default function to convert a node tree into a CSS string. + */ + export let stringify: Stringifier - /** - * Default function to convert a node tree into a CSS string. - */ - stringify: Stringifier + /** + * Parses source css and returns a new `Root` or `Document` node, + * which contains the source CSS nodes. + * + * ```js + * // Simple CSS concatenation with source map support + * const root1 = postcss.parse(css1, { from: file1 }) + * const root2 = postcss.parse(css2, { from: file2 }) + * root1.append(root2).toResult().css + * ``` + */ + export let parse: Parser - /** - * Parses source css and returns a new `Root` or `Document` node, - * which contains the source CSS nodes. - * - * ```js - * // Simple CSS concatenation with source map support - * const root1 = postcss.parse(css1, { from: file1 }) - * const root2 = postcss.parse(css2, { from: file2 }) - * root1.append(root2).toResult().css - * ``` - */ - parse: Parser + /** + * Rehydrate a JSON AST (from `Node#toJSON`) back into the AST classes. + * + * ```js + * const json = root.toJSON() + * // save to file, send by network, etc + * const root2 = postcss.fromJSON(json) + * ``` + */ + export let fromJSON: JSONHydrator - /** - * Rehydrate a JSON AST (from `Node#toJSON`) back into the AST classes. - * - * ```js - * const json = root.toJSON() - * // save to file, send by network, etc - * const root2 = postcss.fromJSON(json) - * ``` - */ - fromJSON: JSONHydrator + /** + * Creates a new `Comment` node. + * + * @param defaults Properties for the new node. + * @return New comment node + */ + export function comment(defaults?: CommentProps): Comment - /** - * Creates a new `Comment` node. - * - * @param defaults Properties for the new node. - * @return New comment node - */ - comment(defaults?: CommentProps): Comment + /** + * Creates a new `AtRule` node. + * + * @param defaults Properties for the new node. + * @return New at-rule node. + */ + export function atRule(defaults?: AtRuleProps): AtRule - /** - * Creates a new `AtRule` node. - * - * @param defaults Properties for the new node. - * @return New at-rule node. - */ - atRule(defaults?: AtRuleProps): AtRule + /** + * Creates a new `Declaration` node. + * + * @param defaults Properties for the new node. + * @return New declaration node. + */ + export function decl(defaults?: DeclarationProps): Declaration - /** - * Creates a new `Declaration` node. - * - * @param defaults Properties for the new node. - * @return New declaration node. - */ - decl(defaults?: DeclarationProps): Declaration + /** + * Creates a new `Rule` node. + * + * @param default Properties for the new node. + * @return New rule node. + */ + export function rule(defaults?: RuleProps): Rule - /** - * Creates a new `Rule` node. - * - * @param default Properties for the new node. - * @return New rule node. - */ - rule(defaults?: RuleProps): Rule + /** + * Creates a new `Root` node. + * + * @param defaults Properties for the new node. + * @return New root node. + */ + export function root(defaults?: RootProps): Root - /** - * Creates a new `Root` node. - * - * @param defaults Properties for the new node. - * @return New root node. - */ - root(defaults?: RootProps): Root + /** + * Creates a new `Document` node. + * + * @param defaults Properties for the new node. + * @return New document node. + */ + export function document(defaults?: DocumentProps): Document - /** - * Creates a new `Document` node. - * - * @param defaults Properties for the new node. - * @return New document node. - */ - document(defaults?: DocumentProps): Document - - CssSyntaxError: typeof CssSyntaxError_ - Declaration: typeof Declaration_ - Processor: typeof Processor_ - Container: typeof Container_ - Document: typeof Document_ - Warning: typeof Warning_ - Comment: typeof Comment_ - AtRule: typeof AtRule_ - Result: typeof Result_ - Input: typeof Input_ - Node: typeof Node_ - list: typeof list - Rule: typeof Rule_ - Root: typeof Root_ - } + export { postcss as default } } -declare const postcss: postcss.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/processor.d.ts b/lib/processor.d.ts index 698803611..c2bb0649c 100644 --- a/lib/processor.d.ts +++ b/lib/processor.d.ts @@ -5,10 +5,10 @@ import { Transformer, TransformCallback } from './postcss.js' -import LazyResult = require('./lazy-result.js') -import Result = require('./result.js') -import Root = require('./root.js') -import NoWorkResult = require('./no-work-result.js') +import LazyResult from './lazy-result.js' +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-shadow diff --git a/lib/result.d.ts b/lib/result.d.ts index ebc5fb467..c160f0c97 100644 --- a/lib/result.d.ts +++ b/lib/result.d.ts @@ -9,7 +9,7 @@ import { Warning, WarningOptions } from './postcss.js' -import Processor = require('./processor.js') +import Processor from './processor.js' declare namespace Result { export interface Message { diff --git a/lib/root.d.ts b/lib/root.d.ts index 991a8cda0..6e478d0ea 100644 --- a/lib/root.d.ts +++ b/lib/root.d.ts @@ -1,7 +1,7 @@ -import Container = require('./container.js') -import Document = require('./document.js') +import Container, { ContainerProps } from './container.js' +import Document from './document.js' import { ProcessOptions } from './postcss.js' -import Result = require('./result.js') +import Result from './result.js' interface RootRaws extends Record { /** @@ -32,7 +32,7 @@ interface RootRaws extends Record { } declare namespace Root { - export interface RootProps extends Container.ContainerProps { + export interface RootProps extends ContainerProps { /** * Information used to generate byte-to-byte equal node string * as it was in the origin input. @@ -54,7 +54,7 @@ declare namespace Root { * root.nodes.length //=> 2 * ``` */ -declare class Root_ extends Container.default { +declare class Root_ extends Container { constructor(defaults?: Root.RootProps) type: 'root' parent: Document | undefined diff --git a/lib/rule.d.ts b/lib/rule.d.ts index bd5c57f38..36ac33f12 100644 --- a/lib/rule.d.ts +++ b/lib/rule.d.ts @@ -1,4 +1,4 @@ -import Container = require('./container.js') +import Container, { ContainerProps } from './container.js' interface RuleRaws extends Record { /** @@ -37,7 +37,7 @@ interface RuleRaws extends Record { } declare namespace Rule { - export interface RuleProps extends Container.ContainerProps { + export interface RuleProps extends ContainerProps { /** Selector or selectors of the rule. */ selector?: string /** Selectors of the rule represented as an array of strings. */ @@ -69,7 +69,7 @@ declare namespace Rule { * rule.toString() //=> 'a{}' * ``` */ -declare class Rule_ extends Container.default { +declare class Rule_ extends Container { constructor(defaults?: Rule.RuleProps) type: 'rule' parent: Container | undefined diff --git a/lib/warning.d.ts b/lib/warning.d.ts index 6386e2fea..45b59dcb3 100644 --- a/lib/warning.d.ts +++ b/lib/warning.d.ts @@ -1,5 +1,5 @@ import { RangePosition } from './css-syntax-error.js' -import Node = require('./node.js') +import Node from './node.js' declare namespace Warning { export interface WarningOptions { From 4ac9d6ba01a1ce601d1ad9cb1bf5c1c02734b9c0 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Mon, 27 Mar 2023 18:50:14 +0200 Subject: [PATCH 12/16] Revert some unwanted changes --- lib/postcss.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/postcss.d.ts b/lib/postcss.d.ts index e93db9aa9..5394ca7f4 100644 --- a/lib/postcss.d.ts +++ b/lib/postcss.d.ts @@ -25,18 +25,18 @@ import LazyResult from './lazy-result.js' import Processor from './processor.js' type DocumentProcessor = ( - document: postcss.Document, + document: Document, helper: postcss.Helpers ) => Promise | void -type RootProcessor = (root: postcss.Root, helper: postcss.Helpers) => Promise | void +type RootProcessor = (root: Root, helper: postcss.Helpers) => Promise | void type DeclarationProcessor = ( - decl: postcss.Declaration, + decl: Declaration, helper: postcss.Helpers ) => Promise | void -type RuleProcessor = (rule: postcss.Rule, helper: postcss.Helpers) => Promise | void -type AtRuleProcessor = (atRule: postcss.AtRule, helper: postcss.Helpers) => Promise | void +type RuleProcessor = (rule: Rule, helper: postcss.Helpers) => Promise | void +type AtRuleProcessor = (atRule: AtRule, helper: postcss.Helpers) => Promise | void type CommentProcessor = ( - comment: postcss.Comment, + comment: Comment, helper: postcss.Helpers ) => Promise | void From f3f4b6cfc57ef4a7209da449916f107d9d03f267 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Fri, 14 Apr 2023 20:32:46 +0200 Subject: [PATCH 13/16] Export private types Closes #1830 --- lib/at-rule.d.ts | 62 ++++++++++++++++++++++---------------------- lib/comment.d.ts | 32 +++++++++++------------ lib/container.d.ts | 24 ++++++++--------- lib/declaration.d.ts | 46 ++++++++++++++++---------------- lib/root.d.ts | 52 ++++++++++++++++++------------------- lib/rule.d.ts | 62 ++++++++++++++++++++++---------------------- 6 files changed, 139 insertions(+), 139 deletions(-) diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index bb4ae3aba..05d5de1e3 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -1,42 +1,42 @@ 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 + } } -} -declare namespace AtRule { export interface AtRuleProps extends ContainerProps { /** Name of the at-rule. */ name: string @@ -79,7 +79,7 @@ declare namespace AtRule { declare class AtRule_ extends Container { type: 'atrule' parent: Container | undefined - raws: AtRuleRaws + raws: AtRule.AtRuleRaws /** * The at-rule’s name immediately follows the `@`. diff --git a/lib/comment.d.ts b/lib/comment.d.ts index dcdd75bcc..b6affa83d 100644 --- a/lib/comment.d.ts +++ b/lib/comment.d.ts @@ -1,24 +1,24 @@ 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 + } -declare namespace Comment { export interface CommentProps extends NodeProps { /** Content of the comment. */ text: string @@ -47,7 +47,7 @@ declare namespace Comment { declare class Comment_ extends Node { type: 'comment' parent: Container | undefined - raws: CommentRaws + raws: Comment.CommentRaws /** * The comment's text. diff --git a/lib/container.d.ts b/lib/container.d.ts index 5ef685f38..223fe8f5a 100644 --- a/lib/container.d.ts +++ b/lib/container.d.ts @@ -4,19 +4,19 @@ 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 + } -declare namespace Container { export interface ContainerProps extends NodeProps { nodes?: (ChildNode | ChildProps)[] } @@ -396,7 +396,7 @@ declare abstract class Container_< */ replaceValues( pattern: string | RegExp, - options: ValueOptions, + options: Container.ValueOptions, replaced: string | { (substring: string, ...args: any[]): string } ): this replaceValues( diff --git a/lib/declaration.d.ts b/lib/declaration.d.ts index 14d2fe945..5493ab301 100644 --- a/lib/declaration.d.ts +++ b/lib/declaration.d.ts @@ -1,33 +1,33 @@ 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?: { - value: string - raw: string + /** + * Declaration value with comments. + */ + value?: { + value: string + raw: string + } } -} -declare namespace Declaration { export interface DeclarationProps { /** Name of the declaration. */ prop: string @@ -66,7 +66,7 @@ declare class Declaration_ extends Node { type: 'decl' parent: Container | undefined - raws: DeclarationRaws + raws: Declaration.DeclarationRaws /** * The declaration's property name. diff --git a/lib/root.d.ts b/lib/root.d.ts index 6e478d0ea..f728e6ba0 100644 --- a/lib/root.d.ts +++ b/lib/root.d.ts @@ -3,35 +3,35 @@ 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 + } -declare namespace Root { export interface RootProps extends ContainerProps { /** * Information used to generate byte-to-byte equal node string @@ -58,7 +58,7 @@ declare class Root_ extends Container { constructor(defaults?: Root.RootProps) type: 'root' parent: Document | undefined - raws: RootRaws + raws: Root.RootRaws /** * Returns a `Result` instance representing the root’s CSS. diff --git a/lib/rule.d.ts b/lib/rule.d.ts index 36ac33f12..e853ebeb8 100644 --- a/lib/rule.d.ts +++ b/lib/rule.d.ts @@ -1,42 +1,42 @@ 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 + } } -} -declare namespace Rule { export interface RuleProps extends ContainerProps { /** Selector or selectors of the rule. */ selector?: string @@ -73,7 +73,7 @@ declare class Rule_ extends Container { constructor(defaults?: Rule.RuleProps) type: 'rule' parent: Container | undefined - raws: RuleRaws + raws: Rule.RuleRaws /** * The rule’s full selector represented as a string. From bda836aedf1a000927999f759e4f5db7a3763edc Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 15 Apr 2023 13:00:25 +0200 Subject: [PATCH 14/16] Remove intermediate classes for default exports --- lib/at-rule.d.ts | 5 ++--- lib/comment.d.ts | 5 ++--- lib/container.d.ts | 5 ++--- lib/css-syntax-error.d.ts | 5 ++--- lib/declaration.d.ts | 5 ++--- lib/document.d.ts | 5 ++--- lib/input.d.ts | 5 ++--- lib/lazy-result.d.ts | 5 ++--- lib/no-work-result.d.ts | 5 ++--- lib/previous-map.d.ts | 5 ++--- lib/processor.d.ts | 5 ++--- lib/result.d.ts | 5 ++--- lib/root.d.ts | 5 ++--- lib/rule.d.ts | 5 ++--- lib/stringifier.d.ts | 5 ++--- lib/warning.d.ts | 5 ++--- 16 files changed, 32 insertions(+), 48 deletions(-) diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index 05d5de1e3..3a8830f73 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -46,9 +46,8 @@ declare namespace AtRule { raws?: AtRuleRaws } - // eslint-disable-next-line @typescript-eslint/no-shadow - class AtRule extends AtRule_ {} - export { AtRule as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { AtRule_ as default } } /** diff --git a/lib/comment.d.ts b/lib/comment.d.ts index b6affa83d..2f00ff7be 100644 --- a/lib/comment.d.ts +++ b/lib/comment.d.ts @@ -26,9 +26,8 @@ declare namespace Comment { raws?: CommentRaws } - // eslint-disable-next-line @typescript-eslint/no-shadow - class Comment extends Comment_ {} - export { Comment as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Comment_ as default } } /** diff --git a/lib/container.d.ts b/lib/container.d.ts index 223fe8f5a..32953fe3d 100644 --- a/lib/container.d.ts +++ b/lib/container.d.ts @@ -21,9 +21,8 @@ declare namespace Container { nodes?: (ChildNode | ChildProps)[] } - // eslint-disable-next-line @typescript-eslint/no-shadow - class Container extends Container_ {} - export { Container as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Container_ as default } } /** diff --git a/lib/css-syntax-error.d.ts b/lib/css-syntax-error.d.ts index 2e7ea8f0b..f89b48479 100644 --- a/lib/css-syntax-error.d.ts +++ b/lib/css-syntax-error.d.ts @@ -16,9 +16,8 @@ declare namespace CssSyntaxError { column: number } - // eslint-disable-next-line @typescript-eslint/no-shadow - class CssSyntaxError extends CssSyntaxError_ {} - export { CssSyntaxError as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { CssSyntaxError_ as default } } /** diff --git a/lib/declaration.d.ts b/lib/declaration.d.ts index 5493ab301..a4b0aa256 100644 --- a/lib/declaration.d.ts +++ b/lib/declaration.d.ts @@ -39,9 +39,8 @@ declare namespace Declaration { raws?: DeclarationRaws } - // eslint-disable-next-line @typescript-eslint/no-shadow - class Declaration extends Declaration_ {} - export { Declaration as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Declaration_ as default } } /** diff --git a/lib/document.d.ts b/lib/document.d.ts index 3fff07bdc..4375855a3 100644 --- a/lib/document.d.ts +++ b/lib/document.d.ts @@ -16,9 +16,8 @@ declare namespace Document { raws?: Record } - // eslint-disable-next-line @typescript-eslint/no-shadow - class Document extends Document_ {} - export { Document as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Document_ as default } } /** diff --git a/lib/input.d.ts b/lib/input.d.ts index 03e63df8b..737694338 100644 --- a/lib/input.d.ts +++ b/lib/input.d.ts @@ -39,9 +39,8 @@ declare namespace Input { source?: string } - // eslint-disable-next-line @typescript-eslint/no-shadow - class Input extends Input_ {} - export { Input as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Input_ as default } } /** diff --git a/lib/lazy-result.d.ts b/lib/lazy-result.d.ts index 5b98980fd..089495c0a 100644 --- a/lib/lazy-result.d.ts +++ b/lib/lazy-result.d.ts @@ -5,9 +5,8 @@ import Warning from './warning.js' import Root from './root.js' declare namespace LazyResult { - // eslint-disable-next-line @typescript-eslint/no-shadow - class LazyResult extends LazyResult_ {} - export { LazyResult as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { LazyResult_ as default } } /** diff --git a/lib/no-work-result.d.ts b/lib/no-work-result.d.ts index 9e247b846..dd150c647 100644 --- a/lib/no-work-result.d.ts +++ b/lib/no-work-result.d.ts @@ -6,9 +6,8 @@ import Root from './root.js' import LazyResult from './lazy-result.js' declare namespace NoWorkResult { - // eslint-disable-next-line @typescript-eslint/no-shadow - class NoWorkResult extends NoWorkResult_ {} - export { NoWorkResult as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { NoWorkResult_ as default } } /** diff --git a/lib/previous-map.d.ts b/lib/previous-map.d.ts index 04ec275f6..ce0ddc24e 100644 --- a/lib/previous-map.d.ts +++ b/lib/previous-map.d.ts @@ -3,9 +3,8 @@ import { SourceMapConsumer } from 'source-map-js' import { ProcessOptions } from './postcss.js' declare namespace PreviousMap { - // eslint-disable-next-line @typescript-eslint/no-shadow - class PreviousMap extends PreviousMap_ {} - export { PreviousMap as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { PreviousMap_ as default } } /** diff --git a/lib/processor.d.ts b/lib/processor.d.ts index c2bb0649c..ff053c65f 100644 --- a/lib/processor.d.ts +++ b/lib/processor.d.ts @@ -11,9 +11,8 @@ import Root from './root.js' import NoWorkResult from './no-work-result.js' declare namespace Processor { - // eslint-disable-next-line @typescript-eslint/no-shadow - class Processor extends Processor_ {} - export { Processor as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Processor_ as default } } /** diff --git a/lib/result.d.ts b/lib/result.d.ts index c160f0c97..e5d279670 100644 --- a/lib/result.d.ts +++ b/lib/result.d.ts @@ -40,9 +40,8 @@ declare namespace Result { } - // eslint-disable-next-line @typescript-eslint/no-shadow - class Result extends Result_ {} - export { Result as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Result_ as default } } /** diff --git a/lib/root.d.ts b/lib/root.d.ts index f728e6ba0..2ec647583 100644 --- a/lib/root.d.ts +++ b/lib/root.d.ts @@ -40,9 +40,8 @@ declare namespace Root { raws?: RootRaws } - // eslint-disable-next-line @typescript-eslint/no-shadow - class Root extends Root_ {} - export { Root as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Root_ as default } } /** diff --git a/lib/rule.d.ts b/lib/rule.d.ts index e853ebeb8..6e1a811dc 100644 --- a/lib/rule.d.ts +++ b/lib/rule.d.ts @@ -46,9 +46,8 @@ declare namespace Rule { raws?: RuleRaws } - // eslint-disable-next-line @typescript-eslint/no-shadow - class Rule extends Rule_ {} - export { Rule as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Rule_ as default } } /** diff --git a/lib/stringifier.d.ts b/lib/stringifier.d.ts index 957db53e5..249acf4a0 100644 --- a/lib/stringifier.d.ts +++ b/lib/stringifier.d.ts @@ -11,9 +11,8 @@ import { } from './postcss.js' declare namespace Stringifier { - // eslint-disable-next-line @typescript-eslint/no-shadow - class Stringifier extends Stringifier_ {} - export { Stringifier as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Stringifier_ as default } } declare class Stringifier_ { diff --git a/lib/warning.d.ts b/lib/warning.d.ts index 45b59dcb3..20ec0656b 100644 --- a/lib/warning.d.ts +++ b/lib/warning.d.ts @@ -40,9 +40,8 @@ declare namespace Warning { plugin?: string } - // eslint-disable-next-line @typescript-eslint/no-shadow - class Warning extends Warning_ {} - export { Warning as default } + // eslint-disable-next-line @typescript-eslint/no-use-before-define + export { Warning_ as default } } /** From 4947e3b881a6d60eeffd98959f19be883f2105ee Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 15 Apr 2023 13:04:52 +0200 Subject: [PATCH 15/16] Fix broken test --- test/errors.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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) } } From 98de266f30d661680d7bb9661d16c428f60da936 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Sat, 15 Apr 2023 13:33:48 +0200 Subject: [PATCH 16/16] Revert unwanted changes --- lib/at-rule.d.ts | 1 + lib/declaration.d.ts | 2 -- lib/document.d.ts | 4 ++-- lib/root.d.ts | 4 ++-- lib/rule.d.ts | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/at-rule.d.ts b/lib/at-rule.d.ts index 3a8830f73..8db6ad45a 100644 --- a/lib/at-rule.d.ts +++ b/lib/at-rule.d.ts @@ -103,6 +103,7 @@ declare class AtRule_ extends Container { */ params: string + constructor(defaults?: AtRule.AtRuleProps) assign(overrides: object | AtRule.AtRuleProps): this clone(overrides?: Partial): this cloneBefore(overrides?: Partial): this diff --git a/lib/declaration.d.ts b/lib/declaration.d.ts index a4b0aa256..c9984a47a 100644 --- a/lib/declaration.d.ts +++ b/lib/declaration.d.ts @@ -61,8 +61,6 @@ declare namespace Declaration { * ``` */ declare class Declaration_ extends Node { - static default: typeof Declaration - type: 'decl' parent: Container | undefined raws: Declaration.DeclarationRaws diff --git a/lib/document.d.ts b/lib/document.d.ts index 4375855a3..7afb37472 100644 --- a/lib/document.d.ts +++ b/lib/document.d.ts @@ -35,11 +35,11 @@ declare namespace Document { * ``` */ declare class Document_ extends Container { - constructor(defaults?: Document.DocumentProps) - type: 'document' parent: undefined + constructor(defaults?: Document.DocumentProps) + /** * Returns a `Result` instance representing the document’s CSS roots. * diff --git a/lib/root.d.ts b/lib/root.d.ts index 2ec647583..d00cfbc52 100644 --- a/lib/root.d.ts +++ b/lib/root.d.ts @@ -54,7 +54,6 @@ declare namespace Root { * ``` */ declare class Root_ extends Container { - constructor(defaults?: Root.RootProps) type: 'root' parent: Document | undefined raws: Root.RootRaws @@ -72,8 +71,9 @@ declare class Root_ extends Container { * @param opts Options. * @return Result with current root’s CSS. */ - toResult(options?: ProcessOptions): Result + toResult(options?: ProcessOptions): Result + constructor(defaults?: Root.RootProps) assign(overrides: object | Root.RootProps): this } diff --git a/lib/rule.d.ts b/lib/rule.d.ts index 6e1a811dc..37fbb2510 100644 --- a/lib/rule.d.ts +++ b/lib/rule.d.ts @@ -69,7 +69,6 @@ declare namespace Rule { * ``` */ declare class Rule_ extends Container { - constructor(defaults?: Rule.RuleProps) type: 'rule' parent: Container | undefined raws: Rule.RuleRaws @@ -102,6 +101,7 @@ declare class Rule_ extends Container { */ selectors: string[] + constructor(defaults?: Rule.RuleProps) assign(overrides: object | Rule.RuleProps): this clone(overrides?: Partial): this cloneBefore(overrides?: Partial): this