fix: vite
This commit is contained in:
21
node_modules/zimmerframe/LICENSE
generated
vendored
Normal file
21
node_modules/zimmerframe/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 [these people](https://github.com/Rich-Harris/zimmerframe/graphs/contributors)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
180
node_modules/zimmerframe/README.md
generated
vendored
Normal file
180
node_modules/zimmerframe/README.md
generated
vendored
Normal file
@@ -0,0 +1,180 @@
|
||||
# zimmerframe
|
||||
|
||||
A tool for walking.
|
||||
|
||||
Specifically, it's a tool for walking an abstract syntax tree (AST), where every node is an object with a `type: string`. This includes [ESTree](https://github.com/estree/estree) nodes, such as you might generate with [Acorn](https://github.com/acornjs/acorn) or [Meriyah](https://github.com/meriyah/meriyah), but also includes things like [CSSTree](https://github.com/csstree/csstree) or an arbitrary AST of your own devising.
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import { walk } from 'zimmerframe';
|
||||
import { parse } from 'acorn';
|
||||
import { Node } from 'estree';
|
||||
|
||||
const program = parse(`
|
||||
let message = 'hello';
|
||||
console.log(message);
|
||||
|
||||
if (true) {
|
||||
let answer = 42;
|
||||
console.log(answer);
|
||||
}
|
||||
`);
|
||||
|
||||
// You can pass in arbitrary state
|
||||
const state = {
|
||||
declarations: [],
|
||||
depth: 0
|
||||
};
|
||||
|
||||
const transformed = walk(program as Node, state, {
|
||||
_(node, { state, next }) {
|
||||
// the `_` visitor is 'universal' — if provided,
|
||||
// it will run for every node, before deferring
|
||||
// to specialised visitors. you can pass a new
|
||||
// `state` object to `next`
|
||||
next({ ...state, answer: 42 });
|
||||
},
|
||||
VariableDeclarator(node, { state }) {
|
||||
// `state` is passed into each visitor
|
||||
if (node.id.type === 'Identifier') {
|
||||
state.declarations.push({
|
||||
depth: state.depth,
|
||||
name: node.id.name
|
||||
});
|
||||
}
|
||||
},
|
||||
BlockStatement(node, { state, next, stop }) {
|
||||
// you must call `next()` or `next(childState)`
|
||||
// to visit child nodes
|
||||
console.log('entering BlockStatement');
|
||||
next({ ...state, depth: state.depth + 1 });
|
||||
console.log('leaving BlockStatement');
|
||||
},
|
||||
Literal(node) {
|
||||
// if you return something, it will replace
|
||||
// the current node
|
||||
if (node.value === 'hello') {
|
||||
return {
|
||||
...node,
|
||||
value: 'goodbye'
|
||||
};
|
||||
}
|
||||
},
|
||||
IfStatement(node, { visit }) {
|
||||
// normally, returning a value will halt
|
||||
// traversal into child nodes. you can
|
||||
// transform children with the current
|
||||
// visitors using `visit(node, state?)`
|
||||
if (node.test.type === 'Literal' && node.test.value === true) {
|
||||
return visit(node.consequent);
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
The `transformed` AST would look like this:
|
||||
|
||||
```js
|
||||
let message = 'goodbye';
|
||||
console.log(message);
|
||||
|
||||
{
|
||||
let answer = 42;
|
||||
console.log(answer);
|
||||
}
|
||||
```
|
||||
|
||||
## Types
|
||||
|
||||
The type of `node` in each visitor is inferred from the visitor's name. For example:
|
||||
|
||||
```ts
|
||||
walk(ast as estree.Node, state, {
|
||||
ArrowFunctionExpression(node) {
|
||||
// `node` is of type estree.ArrowFunctionExpression
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
For this to work, the first argument should be casted to an union of all the types you plan to visit.
|
||||
|
||||
You can import types from 'zimmerframe':
|
||||
|
||||
```ts
|
||||
import {
|
||||
walk,
|
||||
type Visitor,
|
||||
type Visitors,
|
||||
type Context
|
||||
} from 'zimmerframe';
|
||||
import type { Node } from 'estree';
|
||||
|
||||
interface State {...}
|
||||
|
||||
const node: Node = {...};
|
||||
const state: State = {...};
|
||||
const visitors: Visitors<Node, State> = {...}
|
||||
|
||||
walk(node, state, visitors);
|
||||
```
|
||||
|
||||
## Context
|
||||
|
||||
Each visitor receives a second argument, `context`, which is an object with the following properties and methods:
|
||||
|
||||
- `next(state?: State): void` — a function that allows you to control when child nodes are visited, and which state they are visited with. If child visitors transform their inputs, this will return the transformed node (if not, returns `undefined`)
|
||||
- `path: Node[]` — an array of parent nodes. For example, to get the root node you would do `path.at(0)`; to get the current node's immediate parent you would do `path.at(-1)`
|
||||
- `state: State` — an object of the same type as the second argument to `walk`. Visitors can pass new state objects to their children with `next(childState)` or `visit(node, childState)`
|
||||
- `stop(): void` — prevents any subsequent traversal
|
||||
- `visit(node: Node, state?: State): Node` — returns the result of visiting `node` with the current set of visitors. If no `state` is provided, children will inherit the current state
|
||||
|
||||
## Immutability
|
||||
|
||||
ASTs are regarded as immutable. If you return a transformed node from a visitor, then all parents of the node will be replaced with clones, but unchanged subtrees will reuse the existing nodes.
|
||||
|
||||
For example in this case, no transformation takes place, meaning that the returned value is identical to the original AST:
|
||||
|
||||
```js
|
||||
const transformed = walk(original, state, {
|
||||
Literal(node) {
|
||||
console.log(node.value);
|
||||
}
|
||||
});
|
||||
|
||||
transformed === original; // true
|
||||
```
|
||||
|
||||
In this case, however, we replace one of the nodes:
|
||||
|
||||
```js
|
||||
const original = {
|
||||
type: 'BinaryExpression',
|
||||
operator: '+',
|
||||
left: {
|
||||
type: 'Identifier',
|
||||
name: 'foo'
|
||||
},
|
||||
left: {
|
||||
type: 'Identifier',
|
||||
name: 'bar'
|
||||
}
|
||||
};
|
||||
|
||||
const transformed = walk(original, state, {
|
||||
Identifier(node) {
|
||||
if (node.name === 'bar') {
|
||||
return { ...node, name: 'baz' };
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
transformed === original; // false, the BinaryExpression node is cloned
|
||||
transformed.left === original.left; // true, we can safely reuse this node
|
||||
```
|
||||
|
||||
This makes it very easy to transform parts of your AST without incurring the performance and memory overhead of cloning the entire thing, and without the footgun of mutating it in place.
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
34
node_modules/zimmerframe/package.json
generated
vendored
Normal file
34
node_modules/zimmerframe/package.json
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "zimmerframe",
|
||||
"description": "A tool for walking ASTs",
|
||||
"version": "1.1.2",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/Rich-Harris/zimmerframe"
|
||||
},
|
||||
"type": "module",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./types/index.d.ts",
|
||||
"import": "./src/walk.js"
|
||||
}
|
||||
},
|
||||
"types": "./types/index.d.ts",
|
||||
"files": [
|
||||
"src",
|
||||
"types"
|
||||
],
|
||||
"devDependencies": {
|
||||
"dts-buddy": "^0.1.13",
|
||||
"typescript": "^5.1.6",
|
||||
"vitest": "^0.34.2"
|
||||
},
|
||||
"scripts": {
|
||||
"prepublishOnly": "dts-buddy -m zimmerframe:src/index.d.ts",
|
||||
"check": "tsc",
|
||||
"test": "vitest --run",
|
||||
"test:watch": "vitest"
|
||||
},
|
||||
"license": "MIT",
|
||||
"packageManager": "pnpm@8.6.12"
|
||||
}
|
||||
2
node_modules/zimmerframe/src/index.d.ts
generated
vendored
Normal file
2
node_modules/zimmerframe/src/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './walk.js';
|
||||
export * from './types.d.ts';
|
||||
21
node_modules/zimmerframe/src/types.d.ts
generated
vendored
Normal file
21
node_modules/zimmerframe/src/types.d.ts
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
type BaseNode = { type: string };
|
||||
|
||||
type NodeOf<T extends string, X> = X extends { type: T } ? X : never;
|
||||
|
||||
type SpecialisedVisitors<T extends BaseNode, U> = {
|
||||
[K in T['type']]?: Visitor<NodeOf<K, T>, U, T>;
|
||||
};
|
||||
|
||||
export type Visitor<T, U, V> = (node: T, context: Context<V, U>) => V | void;
|
||||
|
||||
export type Visitors<T extends BaseNode, U> = T['type'] extends '_'
|
||||
? never
|
||||
: SpecialisedVisitors<T, U> & { _?: Visitor<T, U, T> };
|
||||
|
||||
export interface Context<T, U> {
|
||||
next: (state?: U) => T | void;
|
||||
path: T[];
|
||||
state: U;
|
||||
stop: () => void;
|
||||
visit: (node: T, state?: U) => T;
|
||||
}
|
||||
159
node_modules/zimmerframe/src/walk.js
generated
vendored
Normal file
159
node_modules/zimmerframe/src/walk.js
generated
vendored
Normal file
@@ -0,0 +1,159 @@
|
||||
/**
|
||||
* @template {{type: string}} T
|
||||
* @template {Record<string, any> | null} U
|
||||
* @param {T} node
|
||||
* @param {U} state
|
||||
* @param {import('./types').Visitors<T, U>} visitors
|
||||
*/
|
||||
export function walk(node, state, visitors) {
|
||||
const universal = visitors._;
|
||||
|
||||
let stopped = false;
|
||||
|
||||
/** @type {import('./types').Visitor<T, U, T>} _ */
|
||||
function default_visitor(_, { next, state }) {
|
||||
next(state);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {T} node
|
||||
* @param {T[]} path
|
||||
* @param {U} state
|
||||
* @returns {T | undefined}
|
||||
*/
|
||||
function visit(node, path, state) {
|
||||
// Don't return the node here or it could lead to false-positive mutation detection
|
||||
if (stopped) return;
|
||||
if (!node.type) return;
|
||||
|
||||
/** @type {T | void} */
|
||||
let result;
|
||||
|
||||
/** @type {Record<string, any>} */
|
||||
const mutations = {};
|
||||
|
||||
/** @type {import('./types').Context<T, U>} */
|
||||
const context = {
|
||||
path,
|
||||
state,
|
||||
next: (next_state = state) => {
|
||||
path.push(node);
|
||||
for (const key in node) {
|
||||
if (key === 'type') continue;
|
||||
|
||||
const child_node = node[key];
|
||||
if (child_node && typeof child_node === 'object') {
|
||||
if (Array.isArray(child_node)) {
|
||||
/** @type {Record<number, T>} */
|
||||
const array_mutations = {};
|
||||
|
||||
child_node.forEach((node, i) => {
|
||||
if (node && typeof node === 'object') {
|
||||
const result = visit(node, path, next_state);
|
||||
if (result) array_mutations[i] = result;
|
||||
}
|
||||
});
|
||||
|
||||
if (Object.keys(array_mutations).length > 0) {
|
||||
mutations[key] = child_node.map(
|
||||
(node, i) => array_mutations[i] ?? node
|
||||
);
|
||||
}
|
||||
} else {
|
||||
const result = visit(
|
||||
/** @type {T} */ (child_node),
|
||||
path,
|
||||
next_state
|
||||
);
|
||||
|
||||
// @ts-ignore
|
||||
if (result) {
|
||||
mutations[key] = result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
path.pop();
|
||||
|
||||
if (Object.keys(mutations).length > 0) {
|
||||
return apply_mutations(node, mutations);
|
||||
}
|
||||
},
|
||||
stop: () => {
|
||||
stopped = true;
|
||||
},
|
||||
visit: (next_node, next_state = state) => {
|
||||
path.push(node);
|
||||
const result = visit(next_node, path, next_state) ?? next_node;
|
||||
path.pop();
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
let visitor = /** @type {import('./types').Visitor<T, U, T>} */ (
|
||||
visitors[/** @type {T['type']} */ (node.type)] ?? default_visitor
|
||||
);
|
||||
|
||||
if (universal) {
|
||||
/** @type {T | void} */
|
||||
let inner_result;
|
||||
|
||||
result = universal(node, {
|
||||
...context,
|
||||
/** @param {U} next_state */
|
||||
next: (next_state = state) => {
|
||||
state = next_state; // make it the default for subsequent specialised visitors
|
||||
|
||||
inner_result = visitor(node, {
|
||||
...context,
|
||||
state: next_state
|
||||
});
|
||||
|
||||
return inner_result;
|
||||
}
|
||||
});
|
||||
|
||||
// @ts-expect-error TypeScript doesn't understand that `context.next(...)` is called immediately
|
||||
if (!result && inner_result) {
|
||||
result = inner_result;
|
||||
}
|
||||
} else {
|
||||
result = visitor(node, context);
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
if (Object.keys(mutations).length > 0) {
|
||||
result = apply_mutations(node, mutations);
|
||||
}
|
||||
}
|
||||
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return visit(node, [], state) ?? node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @template {Record<string, any>} T
|
||||
* @param {T} node
|
||||
* @param {Record<string, any>} mutations
|
||||
* @returns {T}
|
||||
*/
|
||||
function apply_mutations(node, mutations) {
|
||||
/** @type {Record<string, any>} */
|
||||
const obj = {};
|
||||
|
||||
const descriptors = Object.getOwnPropertyDescriptors(node);
|
||||
|
||||
for (const key in descriptors) {
|
||||
Object.defineProperty(obj, key, descriptors[key]);
|
||||
}
|
||||
|
||||
for (const key in mutations) {
|
||||
obj[key] = mutations[key];
|
||||
}
|
||||
|
||||
return /** @type {T} */ (obj);
|
||||
}
|
||||
28
node_modules/zimmerframe/types/index.d.ts
generated
vendored
Normal file
28
node_modules/zimmerframe/types/index.d.ts
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
declare module 'zimmerframe' {
|
||||
export function walk<T extends {
|
||||
type: string;
|
||||
}, U extends Record<string, any> | null>(node: T, state: U, visitors: Visitors<T, U>): T;
|
||||
type BaseNode = { type: string };
|
||||
|
||||
type NodeOf<T extends string, X> = X extends { type: T } ? X : never;
|
||||
|
||||
type SpecialisedVisitors<T extends BaseNode, U> = {
|
||||
[K in T['type']]?: Visitor<NodeOf<K, T>, U, T>;
|
||||
};
|
||||
|
||||
export type Visitor<T, U, V> = (node: T, context: Context<V, U>) => V | void;
|
||||
|
||||
export type Visitors<T extends BaseNode, U> = T['type'] extends '_'
|
||||
? never
|
||||
: SpecialisedVisitors<T, U> & { _?: Visitor<T, U, T> };
|
||||
|
||||
export interface Context<T, U> {
|
||||
next: (state?: U) => T | void;
|
||||
path: T[];
|
||||
state: U;
|
||||
stop: () => void;
|
||||
visit: (node: T, state?: U) => T;
|
||||
}
|
||||
}
|
||||
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
19
node_modules/zimmerframe/types/index.d.ts.map
generated
vendored
Normal file
19
node_modules/zimmerframe/types/index.d.ts.map
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"version": 3,
|
||||
"file": "index.d.ts",
|
||||
"names": [
|
||||
"walk",
|
||||
"Visitor",
|
||||
"Visitors",
|
||||
"Context"
|
||||
],
|
||||
"sources": [
|
||||
"../src/walk.js",
|
||||
"../src/types.d.ts"
|
||||
],
|
||||
"sourcesContent": [
|
||||
null,
|
||||
null
|
||||
],
|
||||
"mappings": ";iBAOgBA,IAAIA;;;;;;;;;;;aCCRC,OAAOA;;aAEPC,QAAQA;;;;kBAIHC,OAAOA"
|
||||
}
|
||||
Reference in New Issue
Block a user