File size: 2,839 Bytes
04ec17f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
/** @import { TSESTree } from '@typescript-eslint/types' */
/** @import { BaseNode, Command, Visitors } from './types' */
export const margin = 0;
export const newline = 1;
export const indent = 2;
export const dedent = 3;
export const space = 4;
export class Context {
#visitors;
#commands;
#has_newline = false;
multiline = false;
/**
*
* @param {Visitors} visitors
* @param {Command[]} commands
*/
constructor(visitors, commands = []) {
this.#visitors = visitors;
this.#commands = commands;
}
indent() {
this.#commands.push(indent);
}
dedent() {
this.#commands.push(dedent);
}
margin() {
this.#commands.push(margin);
}
newline() {
this.#has_newline = true;
this.#commands.push(newline);
}
space() {
this.#commands.push(space);
}
/**
* @param {Context} context
*/
append(context) {
this.#commands.push(context.#commands);
if (this.#has_newline) {
this.multiline = true;
}
}
/**
*
* @param {string} content
* @param {BaseNode} [node]
*/
write(content, node) {
if (node?.loc) {
this.location(node.loc.start.line, node.loc.start.column);
this.#commands.push(content);
this.location(node.loc.end.line, node.loc.end.column);
} else {
this.#commands.push(content);
}
if (this.#has_newline) {
this.multiline = true;
}
}
/**
*
* @param {number} line
* @param {number} column
*/
location(line, column) {
this.#commands.push({ type: 'Location', line, column });
}
/**
* @param {{ type: string }} node
*/
visit(node) {
const visitor = this.#visitors[node.type];
if (!visitor) {
let message = `Not implemented: ${node.type}`;
if (node.type.includes('TS')) {
message += ` (consider using 'esrap/languages/ts')`;
}
if (node.type.includes('JSX')) {
message += ` (consider using 'esrap/languages/tsx')`;
}
throw new Error(message);
}
if (this.#visitors._) {
// @ts-ignore
this.#visitors._(node, this, (node) => visitor(node, this));
} else {
// @ts-ignore
visitor(node, this);
}
}
empty() {
return !this.#commands.some(has_content);
}
measure() {
return measure(this.#commands);
}
new() {
return new Context(this.#visitors);
}
}
/**
*
* @param {Command[]} commands
* @param {number} [from]
* @param {number} [to]
*/
function measure(commands, from = 0, to = commands.length) {
let total = 0;
for (let i = from; i < to; i += 1) {
const command = commands[i];
if (typeof command === 'string') {
total += command.length;
} else if (Array.isArray(command)) {
total += measure(command);
}
}
return total;
}
/**
* @param {Command} command
*/
function has_content(command) {
if (Array.isArray(command)) {
return command.some(has_content);
}
if (typeof command === 'string') {
return command.length > 0;
}
return false;
}
|