Skip to content

Commit

Permalink
Add new type definitions from rwalle/acorn-types
Browse files Browse the repository at this point in the history
Closes #1136
Closes #1004
Closes #946
Closes #741

Co-authored-by: rwalle <rwalle@users.noreply.github.com>
  • Loading branch information
marijnh and rwalle committed Sep 21, 2023
1 parent 288f80b commit 2ffb70f
Show file tree
Hide file tree
Showing 13 changed files with 1,065 additions and 443 deletions.
9 changes: 2 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
.DS_Store
**/node_modules
!/acorn-loose/node_modules
/.tern-port
.tern-port
/node_modules
/local
/acorn/dist
/acorn-loose/dist
/acorn-walk/dist
/yarn.lock
!/acorn/dist/acorn.d.ts
!/acorn/dist/acorn.d.mts
!/acorn-walk/dist/walk.d.ts
!/acorn-walk/dist/walk.d.mts
1 change: 1 addition & 0 deletions acorn-loose/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"homepage": "https://github.com/acornjs/acorn",
"main": "dist/acorn-loose.js",
"module": "dist/acorn-loose.mjs",
"types": "dist/acorn-loose.d.ts",
"exports": {
".": [
{
Expand Down
9 changes: 8 additions & 1 deletion acorn-loose/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import buble from "@rollup/plugin-buble"
import {promises as fs} from "node:fs"

const copy = (from, to) => ({
async buildEnd() { await fs.writeFile(to, await fs.readFile(from)) }
})

export default {
external: ["acorn"],
Expand All @@ -17,6 +22,8 @@ export default {
}
],
plugins: [
buble({transforms: {dangerousForOf: true}})
buble({transforms: {dangerousForOf: true}}),
copy("acorn-loose/src/acorn-loose.d.ts", "acorn-loose/dist/acorn-loose.d.ts"),
copy("acorn-loose/src/acorn-loose.d.ts", "acorn-loose/dist/acorn-loose.d.mts")
]
}
10 changes: 10 additions & 0 deletions acorn-loose/src/acorn-loose.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as acorn from "acorn"

export const LooseParser: typeof acorn.Parser

export function parse (input: string, options: acorn.Options): acorn.Program

/**
* returns `true` if it is a dummy node inserted by the parser. The function performs a simple equality check on the node's name.
*/
export function isDummy(node: acorn.Node): boolean
1 change: 0 additions & 1 deletion acorn-walk/dist/walk.d.mts

This file was deleted.

114 changes: 0 additions & 114 deletions acorn-walk/dist/walk.d.ts

This file was deleted.

1 change: 1 addition & 0 deletions acorn-walk/node_modules/acorn

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion acorn-walk/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import buble from "@rollup/plugin-buble"
import {promises as fs} from "node:fs"

const copy = (from, to) => ({
async buildEnd() { await fs.writeFile(to, await fs.readFile(from)) }
})

export default {
input: "acorn-walk/src/index.js",
Expand All @@ -14,6 +19,8 @@ export default {
}
],
plugins: [
buble({transforms: {dangerousForOf: true}})
buble({transforms: {dangerousForOf: true}}),
copy("acorn-walk/src/walk.d.ts", "acorn-walk/dist/walk.d.ts"),
copy("acorn-walk/src/walk.d.ts", "acorn-walk/dist/walk.d.mts")
]
}
170 changes: 170 additions & 0 deletions acorn-walk/src/walk.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import * as acorn from "acorn"

export type FullWalkerCallback<TState> = (
node: acorn.Node,
state: TState,
type: string
) => void

export type FullAncestorWalkerCallback<TState> = (
node: acorn.Node,
state: TState,
ancestors: acorn.Node[],
type: string
) => void

type AggregateType = {
Expression: acorn.Expression,
Statement: acorn.Statement,
Pattern: acorn.Pattern,
ForInit: acorn.VariableDeclaration | acorn.Expression
}

export type SimpleVisitors<TState> = {
[type in acorn.AnyNode["type"]]?: (node: Extract<acorn.AnyNode, { type: type }>, state: TState) => void
} & {
[type in keyof AggregateType]?: (node: AggregateType[type], state: TState) => void
}

export type AncestorVisitors<TState> = {
[type in acorn.AnyNode["type"]]?: ( node: Extract<acorn.AnyNode, { type: type }>, state: TState, ancestors: acorn.Node[]
) => void
} & {
[type in keyof AggregateType]?: (node: AggregateType[type], state: TState, ancestors: acorn.Node[]) => void
}

export type WalkerCallback<TState> = (node: acorn.Node, state: TState) => void

export type RecursiveVisitors<TState> = {
[type in acorn.AnyNode["type"]]?: ( node: Extract<acorn.AnyNode, { type: type }>, state: TState, callback: WalkerCallback<TState>) => void
} & {
[type in keyof AggregateType]?: (node: AggregateType[type], state: TState, callback: WalkerCallback<TState>) => void
}

export type FindPredicate = (type: string, node: acorn.Node) => boolean

export interface Found<TState> {
node: acorn.Node,
state: TState
}

/**
* does a 'simple' walk over a tree
* @param node the AST node to walk
* @param visitors an object with properties whose names correspond to node types in the {@link https://github.com/estree/estree | ESTree spec}. The properties should contain functions that will be called with the node object and, if applicable the state at that point.
* @param base a walker algorithm
* @param state a start state. The default walker will simply visit all statements and expressions and not produce a meaningful state. (An example of a use of state is to track scope at each point in the tree.)
*/
export function simple<TState>(
node: acorn.Node,
visitors: SimpleVisitors<TState>,
base?: RecursiveVisitors<TState>,
state?: TState
): void

/**
* does a 'simple' walk over a tree, building up an array of ancestor nodes (including the current node) and passing the array to the callbacks as a third parameter.
* @param node
* @param visitors
* @param base
* @param state
*/
export function ancestor<TState>(
node: acorn.Node,
visitors: AncestorVisitors<TState>,
base?: RecursiveVisitors<TState>,
state?: TState
): void

/**
* does a 'recursive' walk, where the walker functions are responsible for continuing the walk on the child nodes of their target node.
* @param node
* @param state the start state
* @param functions contain an object that maps node types to walker functions
* @param base provides the fallback walker functions for node types that aren't handled in the {@link functions} object. If not given, the default walkers will be used.
*/
export function recursive<TState>(
node: acorn.Node,
state: TState,
functions: RecursiveVisitors<TState>,
base?: RecursiveVisitors<TState>
): void

/**
* does a 'full' walk over a tree, calling the {@link callback} with the arguments (node, state, type) for each node
* @param node
* @param callback
* @param base
* @param state
*/
export function full<TState>(
node: acorn.Node,
callback: FullWalkerCallback<TState>,
base?: RecursiveVisitors<TState>,
state?: TState
): void

/**
* does a 'full' walk over a tree, building up an array of ancestor nodes (including the current node) and passing the array to the callbacks as a third parameter.
* @param node
* @param callback
* @param base
* @param state
*/
export function fullAncestor<TState>(
node: acorn.Node,
callback: FullAncestorWalkerCallback<TState>,
base?: RecursiveVisitors<TState>,
state?: TState
): void

/**
* builds a new walker object by using the walker functions in {@link functions} and filling in the missing ones by taking defaults from {@link base}.
* @param functions
* @param base
*/
export function make<TState>(
functions: RecursiveVisitors<TState>,
base?: RecursiveVisitors<TState>
): RecursiveVisitors<TState>

/**
* tries to locate a node in a tree at the given start and/or end offsets, which satisfies the predicate test. {@link start} and {@link end} can be either `null` (as wildcard) or a `number`. {@link test} may be a string (indicating a node type) or a function that takes (nodeType, node) arguments and returns a boolean indicating whether this node is interesting. {@link base} and {@link state} are optional, and can be used to specify a custom walker. Nodes are tested from inner to outer, so if two nodes match the boundaries, the inner one will be preferred.
* @param node
* @param start
* @param end
* @param type
* @param base
* @param state
*/
export function findNodeAt<TState>(
node: acorn.Node,
start: number | undefined,
end?: number | undefined,
type?: FindPredicate | string,
base?: RecursiveVisitors<TState>,
state?: TState
): Found<TState> | undefined

/**
* like {@link findNodeAt}, but will match any node that exists 'around' (spanning) the given position.
* @param node
* @param start
* @param type
* @param base
* @param state
*/
export function findNodeAround<TState>(
node: acorn.Node,
start: number | undefined,
type?: FindPredicate | string,
base?: RecursiveVisitors<TState>,
state?: TState
): Found<TState> | undefined

/**
* similar to {@link findNodeAround}, but will match all nodes after the given position (testing outer nodes before inner nodes).
*/
export const findNodeAfter: typeof findNodeAround

export const base: RecursiveVisitors<any>
26 changes: 0 additions & 26 deletions acorn/dist/acorn.d.mts

This file was deleted.

0 comments on commit 2ffb70f

Please sign in to comment.