All files / src/compiler/phases/1-parse acorn.js

100% Statements 166/166
97.14% Branches 34/35
100% Functions 8/8
100% Lines 162/162

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 1632x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 4262x 4262x 4262x 4262x 4262x 4262x 4262x 4262x 4262x 4262x 4262x 4262x 4262x 4262x 4262x 2x 2x 2x 2x 2x 2x 2x 2x 12187x 12187x 12187x 12187x 12187x 12187x 12187x 12187x 12187x 12187x 12187x 12182x 12182x 12182x 12182x 2x 2x 2x 2x 2x 2x 2x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 16449x 237x 4x 4x 4x 4x 4x 4x 4x 4x 4x 237x 237x 16449x 16449x 16449x 16449x 16444x 129x 129x 129x 3982x 3982x 3982x 175x 175x 175x 3982x 3982x 3982x 3982x 2186x 2186x 1552x 1552x 1552x 61x 61x 1552x 2186x 3982x 129x 15659x 1x 1x 16444x 16449x 16449x 2x 2x 2x 2x 2x 2x 274x 274x 274x 1871x 1871x 1871x 1871x 1871x 1871x 24x 24x 24x 24x 24x 24x 24x 24x 1871x 1871x 1871x 136x 1871x 37x 37x 37x 37x 37x 37x 1871x 1871x 1871x 274x 274x  
import * as acorn from 'acorn';
import { walk } from 'zimmerframe';
import { tsPlugin } from 'acorn-typescript';
import { locator } from '../../state.js';
 
const ParserWithTS = acorn.Parser.extend(tsPlugin({ allowSatisfies: true }));
 
/**
 * @param {string} source
 * @param {boolean} typescript
 */
export function parse(source, typescript) {
	const parser = typescript ? ParserWithTS : acorn.Parser;
	const { onComment, add_comments } = get_comment_handlers(source);
 
	const ast = parser.parse(source, {
		onComment,
		sourceType: 'module',
		ecmaVersion: 13,
		locations: true
	});
 
	if (typescript) amend(source, ast);
	add_comments(ast);
 
	return /** @type {import('estree').Program} */ (ast);
}
 
/**
 * @param {string} source
 * @param {boolean} typescript
 * @param {number} index
 * @returns {acorn.Expression & { leadingComments?: CommentWithLocation[]; trailingComments?: CommentWithLocation[]; }}
 */
export function parse_expression_at(source, typescript, index) {
	const parser = typescript ? ParserWithTS : acorn.Parser;
	const { onComment, add_comments } = get_comment_handlers(source);
 
	const ast = parser.parseExpressionAt(source, index, {
		onComment,
		sourceType: 'module',
		ecmaVersion: 13,
		locations: true
	});
 
	if (typescript) amend(source, ast);
	add_comments(ast);
 
	return ast;
}
 
/**
 * Acorn doesn't add comments to the AST by itself. This factory returns the capabilities
 * to add them after the fact. They are needed in order to support `svelte-ignore` comments
 * in JS code and so that `prettier-plugin-svelte` doesn't remove all comments when formatting.
 * @param {string} source
 */
function get_comment_handlers(source) {
	/**
	 * @typedef {import('estree').Comment & {
	 *   start: number;
	 *   end: number;
	 * }} CommentWithLocation
	 */
 
	/** @type {CommentWithLocation[]} */
	const comments = [];
 
	return {
		/**
		 * @param {boolean} block
		 * @param {string} value
		 * @param {number} start
		 * @param {number} end
		 */
		onComment: (block, value, start, end) => {
			if (block && /\n/.test(value)) {
				let a = start;
				while (a > 0 && source[a - 1] !== '\n') a -= 1;
 
				let b = a;
				while (/[ \t]/.test(source[b])) b += 1;
 
				const indentation = source.slice(a, b);
				value = value.replace(new RegExp(`^${indentation}`, 'gm'), '');
			}
 
			comments.push({ type: block ? 'Block' : 'Line', value, start, end });
		},
 
		/** @param {acorn.Node & { leadingComments?: CommentWithLocation[]; trailingComments?: CommentWithLocation[]; }} ast */
		add_comments(ast) {
			if (comments.length === 0) return;
 
			walk(ast, null, {
				_(node, { next, path }) {
					let comment;
 
					while (comments[0] && comments[0].start < node.start) {
						comment = /** @type {CommentWithLocation} */ (comments.shift());
						(node.leadingComments ||= []).push(comment);
					}
 
					next();
 
					if (comments[0]) {
						const parent = path.at(-1);
						if (parent === undefined || node.end !== parent.end) {
							const slice = source.slice(node.end, comments[0].start);
 
							if (/^[,) \t]*$/.test(slice)) {
								node.trailingComments = [/** @type {CommentWithLocation} */ (comments.shift())];
							}
						}
					}
				}
			});
			if (comments.length > 0) {
				(ast.trailingComments ||= []).push(...comments.splice(0));
			}
		}
	};
}
 
/**
 * Tidy up some stuff left behind by acorn-typescript
 * @param {string} source
 * @param {import('acorn').Node} node
 */
function amend(source, node) {
	return walk(node, null, {
		_(node, context) {
			// @ts-expect-error
			delete node.loc.start.index;
			// @ts-expect-error
			delete node.loc.end.index;
 
			if (typeof node.loc?.end === 'number') {
				const loc = locator(node.loc.end);
				if (loc) {
					node.loc.end = {
						line: loc.line,
						column: loc.column
					};
				}
			}
 
			if (
				/** @type {any} */ (node).typeAnnotation &&
				(node.end === undefined || node.end < node.start)
			) {
				// i think there might be a bug in acorn-typescript that prevents
				// `end` from being assigned when there's a type annotation
				let end = /** @type {any} */ (node).typeAnnotation.start;
				while (/\s/.test(source[end - 1])) end -= 1;
				node.end = end;
			}
 
			context.next();
		}
	});
}