Sha256: eadf0a365eaf36198f6637574f4ebbd3fd95e18fba93442c4d767044a38eb74a

Contents?: true

Size: 1.36 KB

Versions: 26

Compression:

Stored size: 1.36 KB

Contents

/**
 * @typedef { import('estree').Node} Node
 * @typedef {{
 *   skip: () => void;
 *   remove: () => void;
 *   replace: (node: Node) => void;
 * }} WalkerContext
 */

export class WalkerBase {
	constructor() {
		/** @type {boolean} */
		this.should_skip = false;

		/** @type {boolean} */
		this.should_remove = false;

		/** @type {Node | null} */
		this.replacement = null;

		/** @type {WalkerContext} */
		this.context = {
			skip: () => (this.should_skip = true),
			remove: () => (this.should_remove = true),
			replace: (node) => (this.replacement = node)
		};
	}

	/**
	 * @template {Node} Parent
	 * @param {Parent | null | undefined} parent
	 * @param {keyof Parent | null | undefined} prop
	 * @param {number | null | undefined} index
	 * @param {Node} node
	 */
	replace(parent, prop, index, node) {
		if (parent && prop) {
			if (index != null) {
				/** @type {Array<Node>} */ (parent[prop])[index] = node;
			} else {
				/** @type {Node} */ (parent[prop]) = node;
			}
		}
	}

	/**
	 * @template {Node} Parent
	 * @param {Parent | null | undefined} parent
	 * @param {keyof Parent | null | undefined} prop
	 * @param {number | null | undefined} index
	 */
	remove(parent, prop, index) {
		if (parent && prop) {
			if (index !== null && index !== undefined) {
				/** @type {Array<Node>} */ (parent[prop]).splice(index, 1);
			} else {
				delete parent[prop];
			}
		}
	}
}

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
clapton-0.0.6 lib/clapton/javascripts/node_modules/@vitest/mocker/node_modules/estree-walker/src/walker.js
clapton-0.0.5 lib/clapton/javascripts/node_modules/@vitest/mocker/node_modules/estree-walker/src/walker.js
clapton-0.0.4 lib/clapton/javascripts/node_modules/@vitest/mocker/node_modules/estree-walker/src/walker.js
clapton-0.0.3 lib/clapton/javascripts/node_modules/@vitest/mocker/node_modules/estree-walker/src/walker.js
clapton-0.0.2 lib/clapton/javascripts/node_modules/@vitest/mocker/node_modules/estree-walker/src/walker.js
clapton-0.0.1 lib/clapton/javascripts/node_modules/@vitest/mocker/node_modules/estree-walker/src/walker.js