Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type definitions #1815

Merged
merged 17 commits into from Apr 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 18 additions & 14 deletions lib/at-rule.d.ts
@@ -1,4 +1,4 @@
import Container, { ContainerProps } from './container.js'
import Container = require('./container.js')

interface AtRuleRaws extends Record<string, unknown> {
/**
Expand Down Expand Up @@ -36,13 +36,15 @@ interface AtRuleRaws extends Record<string, unknown> {
}
}

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
}
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -98,9 +100,11 @@ export default class AtRule extends Container {
*/
params: string

constructor(defaults?: AtRuleProps)
assign(overrides: object | AtRuleProps): this
clone(overrides?: Partial<AtRuleProps>): this
cloneBefore(overrides?: Partial<AtRuleProps>): this
cloneAfter(overrides?: Partial<AtRuleProps>): this
constructor(defaults?: AtRule.AtRuleProps)
assign(overrides: object | AtRule.AtRuleProps): this
clone(overrides?: Partial<AtRule.AtRuleProps>): this
cloneBefore(overrides?: Partial<AtRule.AtRuleProps>): this
cloneAfter(overrides?: Partial<AtRule.AtRuleProps>): this
}

export = AtRule
30 changes: 17 additions & 13 deletions 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<string, unknown> {
/**
Expand All @@ -18,11 +18,13 @@ interface CommentRaws extends Record<string, unknown> {
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
}
}

/**
Expand All @@ -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
Expand All @@ -48,9 +50,11 @@ export default class Comment extends Node {
*/
text: string

constructor(defaults?: CommentProps)
assign(overrides: object | CommentProps): this
clone(overrides?: Partial<CommentProps>): this
cloneBefore(overrides?: Partial<CommentProps>): this
cloneAfter(overrides?: Partial<CommentProps>): this
constructor(defaults?: Comment.CommentProps)
assign(overrides: object | Comment.CommentProps): this
clone(overrides?: Partial<Comment.CommentProps>): this
cloneBefore(overrides?: Partial<Comment.CommentProps>): this
cloneAfter(overrides?: Partial<Comment.CommentProps>): this
}

export = Comment
32 changes: 18 additions & 14 deletions 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')
remcohaszing marked this conversation as resolved.
Show resolved Hide resolved
import Declaration = require('./declaration.js')
import Comment = require('./comment.js')
import AtRule = require('./at-rule.js')
import Rule = require('./rule.js')

interface ValueOptions {
/**
Expand All @@ -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)[]
}
}

/**
Expand All @@ -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.
Expand Down Expand Up @@ -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

/**
Expand Down Expand Up @@ -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

/**
Expand All @@ -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

/**
Expand Down Expand Up @@ -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

/**
Expand All @@ -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

/**
Expand Down Expand Up @@ -440,3 +442,5 @@ export default abstract class Container<
*/
index(child: Child | number): number
}

export = Container
30 changes: 17 additions & 13 deletions 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
}
}

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -237,3 +239,5 @@ export default class CssSyntaxError {
*/
showSourceCode(color?: boolean): string
}

export = CssSyntaxError
38 changes: 21 additions & 17 deletions 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<string, unknown> {
/**
Expand Down Expand Up @@ -27,15 +27,17 @@ interface DeclarationRaws extends Record<string, unknown> {
}
}

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
}
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -116,9 +118,11 @@ export default class Declaration extends Node {
*/
variable: boolean

constructor(defaults?: DeclarationProps)
assign(overrides: object | DeclarationProps): this
clone(overrides?: Partial<DeclarationProps>): this
cloneBefore(overrides?: Partial<DeclarationProps>): this
cloneAfter(overrides?: Partial<DeclarationProps>): this
constructor(defaults?: Declaration.DeclarationProps)
assign(overrides: object | Declaration.DeclarationProps): this
clone(overrides?: Partial<Declaration.DeclarationProps>): this
cloneBefore(overrides?: Partial<Declaration.DeclarationProps>): this
cloneAfter(overrides?: Partial<Declaration.DeclarationProps>): this
}

export = Declaration
35 changes: 18 additions & 17 deletions 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<string, any>
/**
* Information to generate byte-to-byte equal node string as it was
* in the origin input.
*
* Every parser saves its own properties.
*/
raws?: Record<string, any>
}
}

type ChildNode = Root
type ChildProps = RootProps

/**
* Represents a file and contains all its parsed nodes.
*
Expand All @@ -32,11 +31,11 @@ type ChildProps = RootProps
* document.nodes.length //=> 2
* ```
*/
export default class Document extends Container<Root> {
declare class Document extends Container<Root> {
type: 'document'
parent: undefined

constructor(defaults?: DocumentProps)
constructor(defaults?: Document.DocumentProps)

/**
* Returns a `Result` instance representing the document’s CSS roots.
Expand All @@ -55,3 +54,5 @@ export default class Document extends Container<Root> {
*/
toResult(options?: ProcessOptions): Result
}

export = Document
2 changes: 1 addition & 1 deletion lib/fromJSON.d.ts
Expand Up @@ -2,4 +2,4 @@ import { JSONHydrator } from './postcss.js'

declare const fromJSON: JSONHydrator

export default fromJSON
export = fromJSON