mirror of https://github.com/sveltejs/svelte
parent
e5e5f4d40f
commit
803476f793
@ -1,36 +0,0 @@
|
||||
/** @import { Pattern } from 'estree' */
|
||||
/** @import { Parser } from '../index.js' */
|
||||
import { parse_pattern_at } from '../js.js';
|
||||
import * as e from '../../../errors.js';
|
||||
|
||||
/**
|
||||
* @param {Parser} parser
|
||||
* @returns {Pattern}
|
||||
*/
|
||||
export default function read_pattern(parser) {
|
||||
const start = parser.index;
|
||||
|
||||
const id = parser.read_identifier();
|
||||
|
||||
if (id.name !== '') {
|
||||
const after = parser.index;
|
||||
parser.allow_whitespace();
|
||||
|
||||
// a type annotation makes it a job for the parser
|
||||
if (!parser.match(':')) {
|
||||
parser.index = after;
|
||||
return id;
|
||||
}
|
||||
} else {
|
||||
const char = parser.template[start];
|
||||
|
||||
if (char !== '{' && char !== '[') {
|
||||
e.expected_pattern(start);
|
||||
}
|
||||
}
|
||||
|
||||
const { node, end } = parse_pattern_at(parser, start);
|
||||
parser.index = end;
|
||||
|
||||
return node;
|
||||
}
|
||||
@ -1,52 +0,0 @@
|
||||
/** @import { Expression, Identifier } from 'estree' */
|
||||
/** @import { Parser } from '../index.js' */
|
||||
import { parse_expression_at } from '../js.js';
|
||||
import * as e from '../../../errors.js';
|
||||
import { find_matching_bracket } from '../utils/bracket.js';
|
||||
|
||||
/**
|
||||
* @param {Parser} parser
|
||||
* @param {string} [opening_token]
|
||||
* @returns {Expression | undefined}
|
||||
*/
|
||||
export function get_loose_identifier(parser, opening_token) {
|
||||
// Find the next } and treat it as the end of the expression
|
||||
const end = find_matching_bracket(parser.template, parser.index, opening_token ?? '{');
|
||||
if (end) {
|
||||
const start = parser.index;
|
||||
parser.index = end;
|
||||
// We don't know what the expression is and signal this by returning an empty identifier
|
||||
return {
|
||||
type: 'Identifier',
|
||||
start,
|
||||
end,
|
||||
name: ''
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Parser} parser
|
||||
* @param {string} [opening_token]
|
||||
* @param {boolean} [disallow_loose]
|
||||
* @param {'as'} [until] the host's `as` follows the expression, as an each block's item does
|
||||
* @returns {Expression}
|
||||
*/
|
||||
export default function read_expression(parser, opening_token, disallow_loose, until) {
|
||||
try {
|
||||
const { node, end } = parse_expression_at(parser, parser.index, until);
|
||||
parser.index = end;
|
||||
return node;
|
||||
} catch (err) {
|
||||
// If we are in an each loop we need the error to be thrown in cases like
|
||||
// `as { y = z }` so we still throw and handle the error there
|
||||
if (parser.loose && !disallow_loose) {
|
||||
const expression = get_loose_identifier(parser, opening_token);
|
||||
if (expression) {
|
||||
return expression;
|
||||
}
|
||||
}
|
||||
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue