All files / src/compiler/utils builders.js

100% Statements 629/629
100% Branches 71/71
100% Functions 54/54
100% Lines 627/627

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 6282x 2x 2x 2x 2x 2x 2x 2x 693x 693x 2x 2x 2x 2x 2x 2x 4x 4x 2x 2x 2x 2x 2x 2x 2x 17x 17x 2x 2x 2x 2x 2x 2x 2x 7596x 7596x 7596x 7596x 7596x 7596x 7596x 7596x 7596x 7596x 2x 2x 2x 2x 2x 2x 2x 2x 9429x 9429x 2x 2x 2x 2x 2x 2x 2x 21x 21x 2x 2x 2x 2x 2x 39x 39x 39x 2x 2x 2x 2x 2x 2x 2x 2x 933x 933x 2x 2x 2x 2x 2x 2x 18410x 18410x 2x 2x 2x 2x 2x 2x 2x 140x 140x 2x 2x 2x 2x 2x 2x 2x 60677x 60677x 60677x 60677x 60677x 60677x 60677x 60677x 60677x 60677x 60677x 60677x 2x 2x 2x 2x 2x 2x 2x 60x 60x 60x 60x 60x 60x 60x 60x 2x 2x 2x 2x 2x 2x 2x 74x 74x 2x 2x 2x 2x 2x 2x 2x 2x 135x 135x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 14056x 14056x 14056x 14056x 14056x 14056x 14056x 14056x 2x 2x 2x 2x 2x 2x 2x 19850x 19850x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4360x 4360x 2x 2x 2x 2x 2x 2x 2x 2x 4430x 4430x 4430x 4430x 4430x 4430x 4430x 4430x 4430x 4430x 2x 2x 2x 2x 2x 2x 2x 2622x 2622x 2x 2x 2x 2x 2x 2x 149197x 149197x 2x 2x 2x 2x 2x 2x 55x 55x 2x 2x 2x 2x 2x 4372x 4372x 4372x 4372x 4372x 4372x 2x 2x 2x 2x 2x 2x 2x 3268x 3268x 2x 2x 2x 2x 2x 2x 28140x 28140x 28140x 2x 2x 2x 2x 2x 2x 2x 2x 2x 11052x 11052x 2x 2x 2x 2x 2x 2x 391x 391x 391x 391x 391x 391x 391x 391x 391x 391x 2x 2x 2x 2x 2x 2x 4628x 4628x 2x 2x 2x 2x 2x 2x 12x 12x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 9051x 9051x 2x 2x 2x 2x 2x 2x 2x 2x 2x 65x 65x 2x 2x 2x 2x 2x 2x 2x 13541x 13541x 13541x 2x 2x 2x 2x 2x 2x 35x 35x 2x 2x 2x 2x 2x 2x 379x 379x 2x 2x 2x 2x 2x 2x 2x 2213x 2213x 2x 2x 2x 2x 2x 2x 4x 4x 2x 2x 2x 2x 2x 2x 34793x 34793x 2x 2x 2x 2x 2x 2x 2x 9028x 9028x 2x 2x 2x 2x 2x 2x 2x 5725x 5725x 5725x 5725x 5725x 2230x 5725x 694x 694x 5031x 5031x 5725x 5031x 5031x 2x 2x 2x 2x 2x 2x 2x 385x 385x 385x 385x 385x 385x 385x 385x 385x 2x 2x 2x 2x 2x 2x 2x 2x 450x 450x 2x 2x 2x 2x 2x 2x 2x 74x 74x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 811x 811x 811x 2x 2x 2x 2x 2x 2x 1912x 1912x 1912x 2x 2x 2x 2x 2x 2x 11202x 11202x 11202x 2x 2x 2x 2x 2x 2x 2x 2x 2x 450x 450x 450x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 84x 84x 84x 84x 84x 84x 84x 84x 84x 2x 2x 2x 2x 2x 2x 2x 2x 5124x 5124x 5124x 5124x 5124x 5124x 5124x 5124x 5124x 5124x 5124x 2x 2x 2x 2x 2x 2x 2x 630x 630x 630x 2x 2x 2x 2x 2x 2x 2x 4372x 4372x 4372x 4372x 4372x 4372x 2x 2x 2x 2x 2x 2x 2x 2136x 2136x 2136x 2136x 8x 8x 8x 2136x 2136x 2136x 2x 2x 2x 2x 2x 4050x 4050x 4050x 2x 2x 2x 2x 2x 2x 385x 385x 385x 385x 385x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8531x 8531x  
import { regex_is_valid_identifier } from '../phases/patterns.js';
import { sanitize_template_string } from './sanitize_template_string.js';
 
/**
 * @param {Array<import('estree').Expression | import('estree').SpreadElement | null>} elements
 * @returns {import('estree').ArrayExpression}
 */
export function array(elements = []) {
	return { type: 'ArrayExpression', elements };
}
 
/**
 * @param {Array<import('estree').Pattern | null>} elements
 * @returns {import('estree').ArrayPattern}
 */
export function array_pattern(elements) {
	return { type: 'ArrayPattern', elements };
}
 
/**
 * @param {import('estree').Pattern} left
 * @param {import('estree').Expression} right
 * @returns {import('estree').AssignmentPattern}
 */
export function assignment_pattern(left, right) {
	return { type: 'AssignmentPattern', left, right };
}
 
/**
 * @param {Array<import('estree').Pattern>} params
 * @param {import('estree').BlockStatement | import('estree').Expression} body
 * @returns {import('estree').ArrowFunctionExpression}
 */
export function arrow(params, body) {
	return {
		type: 'ArrowFunctionExpression',
		params,
		body,
		expression: body.type !== 'BlockStatement',
		generator: false,
		async: false,
		metadata: /** @type {any} */ (null) // should not be used by codegen
	};
}
 
/**
 * @param {import('estree').AssignmentOperator} operator
 * @param {import('estree').Pattern} left
 * @param {import('estree').Expression} right
 * @returns {import('estree').AssignmentExpression}
 */
export function assignment(operator, left, right) {
	return { type: 'AssignmentExpression', operator, left, right };
}
 
/**
 * @template T
 * @param {T & import('estree').BaseFunction} func
 * @returns {T & import('estree').BaseFunction}
 */
export function async(func) {
	return { ...func, async: true };
}
 
/**
 * @param {import('estree').Expression} argument
 * @returns {import('estree').AwaitExpression}
 */
function await_builder(argument) {
	return { type: 'AwaitExpression', argument };
}
 
/**
 * @param {import('estree').BinaryOperator} operator
 * @param {import('estree').Expression} left
 * @param {import('estree').Expression} right
 * @returns {import('estree').BinaryExpression}
 */
export function binary(operator, left, right) {
	return { type: 'BinaryExpression', operator, left, right };
}
 
/**
 * @param {import('estree').Statement[]} body
 * @returns {import('estree').BlockStatement}
 */
export function block(body) {
	return { type: 'BlockStatement', body };
}
 
/**
 * @param {string} name
 * @param {import('estree').Statement} body
 * @returns {import('estree').LabeledStatement}
 */
export function labeled(name, body) {
	return { type: 'LabeledStatement', label: id(name), body };
}
 
/**
 * @param {string | import('estree').Expression} callee
 * @param {...(import('estree').Expression | import('estree').SpreadElement)} args
 * @returns {import('estree').CallExpression}
 */
export function call(callee, ...args) {
	if (typeof callee === 'string') callee = id(callee);
	args = args.slice();
 
	while (args.length > 0 && !args.at(-1)) args.pop();
 
	return {
		type: 'CallExpression',
		callee,
		arguments: args,
		optional: false
	};
}
 
/**
 * @param {string | import('estree').Expression} callee
 * @param {...import('estree').Expression} args
 * @returns {import('estree').ChainExpression}
 */
export function maybe_call(callee, ...args) {
	const expression = /** @type {import('estree').SimpleCallExpression} */ (call(callee, ...args));
	expression.optional = true;
 
	return {
		type: 'ChainExpression',
		expression
	};
}
 
/**
 * @param {import('estree').UnaryOperator} operator
 * @param {import('estree').Expression} argument
 * @returns {import('estree').UnaryExpression}
 */
export function unary(operator, argument) {
	return { type: 'UnaryExpression', argument, operator, prefix: true };
}
 
/**
 * @param {import('estree').Expression} test
 * @param {import('estree').Expression} consequent
 * @param {import('estree').Expression} alternate
 * @returns {import('estree').ConditionalExpression}
 */
export function conditional(test, consequent, alternate) {
	return { type: 'ConditionalExpression', test, consequent, alternate };
}
 
/**
 * @param {import('estree').LogicalOperator} operator
 * @param {import('estree').Expression} left
 * @param {import('estree').Expression} right
 * @returns {import('estree').LogicalExpression}
 */
export function logical(operator, left, right) {
	return { type: 'LogicalExpression', operator, left, right };
}
 
/**
 * @param {'const' | 'let' | 'var'} kind
 * @param {string | import('estree').Pattern} pattern
 * @param {import('estree').Expression} [init]
 * @returns {import('estree').VariableDeclaration}
 */
export function declaration(kind, pattern, init) {
	if (typeof pattern === 'string') pattern = id(pattern);
 
	return {
		type: 'VariableDeclaration',
		kind,
		declarations: [init ? declarator(pattern, init) : declarator(pattern)]
	};
}
 
/**
 * @param {import('estree').Pattern} id
 * @param {import('estree').Expression} [init]
 * @returns {import('estree').VariableDeclarator}
 */
export function declarator(id, init) {
	return { type: 'VariableDeclarator', id, init };
}
 
/** @type {import('estree').EmptyStatement} */
export const empty = {
	type: 'EmptyStatement'
};
 
/**
 * @param {import('estree').Expression | import('estree').MaybeNamedClassDeclaration | import('estree').MaybeNamedFunctionDeclaration} declaration
 * @returns {import('estree').ExportDefaultDeclaration}
 */
export function export_default(declaration) {
	return { type: 'ExportDefaultDeclaration', declaration };
}
 
/**
 * @param {import('estree').Identifier} id
 * @param {import('estree').Pattern[]} params
 * @param {import('estree').BlockStatement} body
 * @returns {import('estree').FunctionDeclaration}
 */
export function function_declaration(id, params, body) {
	return {
		type: 'FunctionDeclaration',
		id,
		params,
		body,
		generator: false,
		async: false,
		metadata: /** @type {any} */ (null) // should not be used by codegen
	};
}
 
/**
 * @param {string} name
 * @param {import('estree').Statement[]} body
 * @returns {import('estree').Property & { value: import('estree').FunctionExpression}}}
 */
export function get(name, body) {
	return prop('get', key(name), function_builder(null, [], block(body)));
}
 
/**
 * @param {string} name
 * @returns {import('estree').Identifier}
 */
export function id(name) {
	return { type: 'Identifier', name };
}
 
/**
 * @param {string} name
 * @returns {import('estree').PrivateIdentifier}
 */
export function private_id(name) {
	return { type: 'PrivateIdentifier', name };
}
 
/**
 * @param {string} local
 * @returns {import('estree').ImportNamespaceSpecifier}
 */
function import_namespace(local) {
	return {
		type: 'ImportNamespaceSpecifier',
		local: id(local)
	};
}
 
/**
 * @param {string} name
 * @param {import('estree').Expression} value
 * @returns {import('estree').Property}
 */
export function init(name, value) {
	return prop('init', key(name), value);
}
 
/**
 * @param {string | boolean | null | number | RegExp} value
 * @returns {import('estree').Literal}
 */
export function literal(value) {
	// @ts-expect-error we don't want to muck around with bigint here
	return { type: 'Literal', value };
}
 
/**
 * @param {import('estree').Expression | import('estree').Super} object
 * @param {import('estree').Expression | import('estree').PrivateIdentifier} property
 * @param {boolean} computed
 * @param {boolean} optional
 * @returns {import('estree').MemberExpression}
 */
export function member(object, property, computed = false, optional = false) {
	return { type: 'MemberExpression', object, property, computed, optional };
}
 
/**
 * @param {string} path
 * @returns {import('estree').Identifier | import('estree').MemberExpression}
 */
export function member_id(path) {
	const parts = path.split('.');
 
	/** @type {import('estree').Identifier | import('estree').MemberExpression} */
	let expression = id(parts[0]);
 
	for (let i = 1; i < parts.length; i += 1) {
		expression = member(expression, id(parts[i]));
	}
	return expression;
}
 
/**
 * @param {Array<import('estree').Property | import('estree').SpreadElement>} properties
 * @returns {import('estree').ObjectExpression}
 */
export function object(properties) {
	return { type: 'ObjectExpression', properties };
}
 
/**
 * @param {Array<import('estree').RestElement | import('estree').AssignmentProperty>} properties
 * @returns {import('estree').ObjectPattern}
 */
export function object_pattern(properties) {
	return { type: 'ObjectPattern', properties };
}
 
/**
 * @template {import('estree').Expression} Value
 * @param {'init' | 'get' | 'set'} kind
 * @param {import('estree').Expression} key
 * @param {Value} value
 * @param {boolean} computed
 * @returns {import('estree').Property & { value: Value }}
 */
export function prop(kind, key, value, computed = false) {
	return { type: 'Property', kind, key, value, method: false, shorthand: false, computed };
}
 
/**
 * @param {import('estree').Expression | import('estree').PrivateIdentifier} key
 * @param {import('estree').Expression | null | undefined} value
 * @param {boolean} computed
 * @param {boolean} is_static
 * @returns {import('estree').PropertyDefinition}
 */
export function prop_def(key, value, computed = false, is_static = false) {
	return { type: 'PropertyDefinition', key, value, computed, static: is_static };
}
 
/**
 * @param {string} cooked
 * @param {boolean} tail
 * @returns {import('estree').TemplateElement}
 */
export function quasi(cooked, tail = false) {
	const raw = sanitize_template_string(cooked);
	return { type: 'TemplateElement', value: { raw, cooked }, tail };
}
 
/**
 * @param {import('estree').Pattern} argument
 * @returns {import('estree').RestElement}
 */
export function rest(argument) {
	return { type: 'RestElement', argument };
}
 
/**
 * @param {import('estree').Expression[]} expressions
 * @returns {import('estree').SequenceExpression}
 */
export function sequence(expressions) {
	return { type: 'SequenceExpression', expressions };
}
 
/**
 * @param {string} name
 * @param {import('estree').Statement[]} body
 * @returns {import('estree').Property & { value: import('estree').FunctionExpression}}
 */
export function set(name, body) {
	return prop('set', key(name), function_builder(null, [id('$$value')], block(body)));
}
 
/**
 * @param {import('estree').Expression} argument
 * @returns {import('estree').SpreadElement}
 */
export function spread(argument) {
	return { type: 'SpreadElement', argument };
}
 
/**
 * @param {import('estree').Expression} expression
 * @returns {import('estree').ExpressionStatement}
 */
export function stmt(expression) {
	return { type: 'ExpressionStatement', expression };
}
 
/**
 * @param {import('estree').TemplateElement[]} elements
 * @param {import('estree').Expression[]} expressions
 * @returns {import('estree').TemplateLiteral}
 */
export function template(elements, expressions) {
	return { type: 'TemplateLiteral', quasis: elements, expressions };
}
 
/**
 * @param {import('estree').Expression | import('estree').BlockStatement} expression
 * @param {boolean} [async]
 * @returns {import('estree').Expression}
 */
export function thunk(expression, async = false) {
	if (
		expression.type === 'CallExpression' &&
		expression.callee.type !== 'Super' &&
		expression.callee.type !== 'MemberExpression' &&
		expression.callee.type !== 'CallExpression' &&
		expression.arguments.length === 0
	) {
		return expression.callee;
	}
 
	const fn = arrow([], expression);
	if (async) fn.async = true;
	return fn;
}
 
/**
 *
 * @param {string | import('estree').Expression} expression
 * @param  {...import('estree').Expression} args
 * @returns {import('estree').NewExpression}
 */
function new_builder(expression, ...args) {
	if (typeof expression === 'string') expression = id(expression);
 
	return {
		callee: expression,
		arguments: args,
		type: 'NewExpression'
	};
}
 
/**
 * @param {import('estree').UpdateOperator} operator
 * @param {import('estree').Expression} argument
 * @param {boolean} prefix
 * @returns {import('estree').UpdateExpression}
 */
export function update(operator, argument, prefix = false) {
	return { type: 'UpdateExpression', operator, argument, prefix };
}
 
/**
 * @param {import('estree').Expression} test
 * @param {import('estree').Statement} body
 * @returns {import('estree').DoWhileStatement}
 */
export function do_while(test, body) {
	return { type: 'DoWhileStatement', test, body };
}
 
const true_instance = literal(true);
const false_instance = literal(false);
 
/** @type {import('estree').DebuggerStatement} */
const debugger_builder = {
	type: 'DebuggerStatement'
};
 
/** @type {import('estree').ThisExpression} */
const this_instance = {
	type: 'ThisExpression'
};
 
/**
 * @param {string | import('estree').Pattern} pattern
 * @param { import('estree').Expression} [init]
 * @returns {import('estree').VariableDeclaration}
 */
function let_builder(pattern, init) {
	return declaration('let', pattern, init);
}
 
/**
 * @param {string | import('estree').Pattern} pattern
 * @param { import('estree').Expression} init
 * @returns {import('estree').VariableDeclaration}
 */
function const_builder(pattern, init) {
	return declaration('const', pattern, init);
}
 
/**
 * @param {string | import('estree').Pattern} pattern
 * @param { import('estree').Expression} [init]
 * @returns {import('estree').VariableDeclaration}
 */
function var_builder(pattern, init) {
	return declaration('var', pattern, init);
}
 
/**
 *
 * @param {import('estree').VariableDeclaration | import('estree').Expression | null} init
 * @param {import('estree').Expression} test
 * @param {import('estree').Expression} update
 * @param {import('estree').Statement} body
 * @returns {import('estree').ForStatement}
 */
function for_builder(init, test, update, body) {
	return { type: 'ForStatement', init, test, update, body };
}
 
/**
 *
 * @param {'constructor' | 'method' | 'get' | 'set'} kind
 * @param {import('estree').Expression | import('estree').PrivateIdentifier} key
 * @param {import('estree').Pattern[]} params
 * @param {import('estree').Statement[]} body
 * @param {boolean} computed
 * @param {boolean} is_static
 * @returns {import('estree').MethodDefinition}
 */
export function method(kind, key, params, body, computed = false, is_static = false) {
	return {
		type: 'MethodDefinition',
		key,
		kind,
		value: function_builder(null, params, block(body)),
		computed,
		static: is_static
	};
}
 
/**
 *
 * @param {import('estree').Identifier | null} id
 * @param {import('estree').Pattern[]} params
 * @param {import('estree').BlockStatement} body
 * @returns {import('estree').FunctionExpression}
 */
function function_builder(id, params, body) {
	return {
		type: 'FunctionExpression',
		id,
		params,
		body,
		generator: false,
		async: false,
		metadata: /** @type {any} */ (null) // should not be used by codegen
	};
}
 
/**
 * @param {import('estree').Expression} test
 * @param {import('estree').Statement} consequent
 * @param {import('estree').Statement} [alternate]
 * @returns {import('estree').IfStatement}
 */
function if_builder(test, consequent, alternate) {
	return { type: 'IfStatement', test, consequent, alternate };
}
 
/**
 * @param {string} as
 * @param {string} source
 * @returns {import('estree').ImportDeclaration}
 */
export function import_all(as, source) {
	return {
		type: 'ImportDeclaration',
		source: literal(source),
		specifiers: [import_namespace(as)]
	};
}
 
/**
 * @param {Array<[string, string]>} parts
 * @param {string} source
 * @returns {import('estree').ImportDeclaration}
 */
export function imports(parts, source) {
	return {
		type: 'ImportDeclaration',
		source: literal(source),
		specifiers: parts.map((p) => ({
			type: 'ImportSpecifier',
			imported: id(p[0]),
			local: id(p[1])
		}))
	};
}
 
/**
 * @param {import('estree').Expression | null} argument
 * @returns {import('estree').ReturnStatement}
 */
function return_builder(argument = null) {
	return { type: 'ReturnStatement', argument };
}
 
/**
 * @param {string} str
 * @returns {import('estree').ThrowStatement}
 */
export function throw_error(str) {
	return {
		type: 'ThrowStatement',
		argument: new_builder('Error', literal(str))
	};
}
 
export {
	await_builder as await,
	let_builder as let,
	const_builder as const,
	var_builder as var,
	true_instance as true,
	false_instance as false,
	for_builder as for,
	function_builder as function,
	return_builder as return,
	if_builder as if,
	this_instance as this,
	debugger_builder as debugger
};
 
/**
 * @param {string} name
 * @returns {import('estree').Expression}
 */
export function key(name) {
	return regex_is_valid_identifier.test(name) ? id(name) : literal(name);
}