fix: adapt lang="ts" hint to simplified parser architecture

Resolve merge issues with #18075 and #18077:
- Fix inverted typescript flag in handle_parse_error
- Remove stale parser.acorn_error() call in tag.js
- Move TS syntax check inside try block in read_expression
pull/17954/head
Julia 5 months ago
parent 592adfb389
commit 70393e8d0e

@ -61,7 +61,7 @@ export function parse(source, comments, typescript, is_script) {
} catch (err) {
// TODO the `return` in necessary for TS<7 due to a bug; otherwise
// the `finally` block is regarded as unreachable
return handle_parse_error(err, source, typescript);
return handle_parse_error(err);
} finally {
if (is_script) {
// @ts-expect-error
@ -102,15 +102,15 @@ const regex_position_indicator = / \(\d+:\d+\)$/;
/**
* @param {any} err
* @param {string} source
* @param {boolean} typescript
* @param {string} [source]
* @param {boolean} [typescript]
* @returns {never}
*/
function handle_parse_error(err, source, typescript) {
let message = /** @type {string} */ (err.message).replace(regex_position_indicator, '');
let pos = /** @type {number} */ (err.pos);
if (typescript && source[pos] === ':') {
if (!typescript && source && source[pos] === ':') {
message += ` (did you forget to add \`lang="ts"\`?)`;
}

@ -33,11 +33,8 @@ export function get_loose_identifier(parser, opening_token) {
* @returns {Expression}
*/
export default function read_expression(parser, opening_token, disallow_loose) {
/** @type {ReturnType<typeof parse_expression_at>} */
let node;
try {
node = parse_expression_at(parser, parser.template, parser.index);
const node = parse_expression_at(parser, parser.template, parser.index);
let index = /** @type {number} */ (node.end);
@ -45,18 +42,6 @@ export default function read_expression(parser, opening_token, disallow_loose) {
if (last_comment && last_comment.end > index) index = last_comment.end;
parser.index = index;
} 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;
}
if (!parser.ts) {
let j = parser.index;
@ -75,4 +60,16 @@ export default function read_expression(parser, opening_token, disallow_loose) {
}
return /** @type {Expression} */ (remove_parens(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;
}
}

@ -390,19 +390,11 @@ function open(parser) {
const prelude = parser.template.slice(0, params_start).replace(/\S/g, ' ');
const params = parser.template.slice(params_start, parser.index);
let function_expression;
if (matched) {
try {
function_expression = /** @type {ArrowFunctionExpression} */ (
let function_expression = matched
? /** @type {ArrowFunctionExpression} */ (
parse_expression_at(parser, prelude + `${params} => {}`, params_start)
);
} catch (/** @type {any} */ err) {
parser.acorn_error(err);
}
} else {
function_expression = { params: [] };
}
)
: { params: [] };
parser.allow_whitespace();
parser.eat('}', true);

Loading…
Cancel
Save