Skip to content

Commit 4d931ab

Browse files
committed
feat: parse as a whole function
1 parent 4b740ae commit 4d931ab

File tree

1 file changed

+31
-57
lines changed
  • packages/svelte/src/compiler/phases/1-parse/state

1 file changed

+31
-57
lines changed

packages/svelte/src/compiler/phases/1-parse/state/tag.js

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import read_expression from '../read/expression.js';
33
import * as e from '../../../errors.js';
44
import { create_fragment } from '../utils/create.js';
55
import { walk } from 'zimmerframe';
6+
import { parse_expression_at } from '../acorn.js';
67

78
const regex_whitespace_with_closing_curly_brace = /^\s*}/;
89

@@ -270,68 +271,41 @@ function open(parser) {
270271

271272
parser.eat('(', true);
272273

273-
parser.allow_whitespace();
274-
275-
/** @type {import('estree').Pattern[]} */
276-
const parameters = [];
277-
278-
while (!parser.match(')')) {
279-
let pattern = read_pattern(parser, true);
280-
281-
parser.allow_whitespace();
282-
if (parser.eat('=')) {
283-
parser.allow_whitespace();
284-
285-
let index = parser.index;
286-
let right = read_expression(parser);
287-
288-
parser.allow_whitespace();
289-
290-
// multiple assignment expression will be confused by the parser for a sequence expression
291-
// so if the returned value is a sequence expression we use the first expression as the
292-
// right and we reset the parser.index
293-
if (right.type === 'SequenceExpression' && parser.template[index] !== '(') {
294-
right = right.expressions[0];
295-
parser.index = /** @type {number} */ (right.end);
296-
}
297-
298-
pattern = {
299-
type: 'AssignmentPattern',
300-
left: pattern,
301-
right,
302-
start: pattern.start,
303-
end: right.end
304-
};
305-
}
306-
307-
parameters.push(pattern);
274+
let parentheses = 1;
275+
let params = '';
308276

309-
if (!parser.eat(',')) break;
310-
parser.allow_whitespace();
277+
while (!parser.match(')') || parentheses !== 1) {
278+
if (parser.match('(')) parentheses++;
279+
if (parser.match(')')) parentheses--;
280+
params += parser.read(/^./);
311281
}
312282

313-
parser.eat(')', true);
283+
const function_expression = parse_expression_at(`function ${name}(${params}){}`, parser.ts, 0);
314284

315-
parser.allow_whitespace();
316-
parser.eat('}', true);
285+
parser.index += 2;
317286

318-
/** @type {ReturnType<typeof parser.append<import('#compiler').SnippetBlock>>} */
319-
const block = parser.append({
320-
type: 'SnippetBlock',
321-
start,
322-
end: -1,
323-
expression: {
324-
type: 'Identifier',
325-
start: name_start,
326-
end: name_end,
327-
name
328-
},
329-
parameters,
330-
body: create_fragment()
331-
});
332-
333-
parser.stack.push(block);
334-
parser.fragments.push(block.body);
287+
if (function_expression.type === 'FunctionExpression') {
288+
/** @type {ReturnType<typeof parser.append<import('#compiler').SnippetBlock>>} */
289+
const block = parser.append({
290+
type: 'SnippetBlock',
291+
start,
292+
end: -1,
293+
expression: {
294+
type: 'Identifier',
295+
start: name_start,
296+
end: name_end,
297+
name
298+
},
299+
parameters: function_expression.params.map((param) => {
300+
param.start += start + 1;
301+
param.end += start + 1;
302+
return param;
303+
}),
304+
body: create_fragment()
305+
});
306+
parser.stack.push(block);
307+
parser.fragments.push(block.body);
308+
}
335309

336310
return;
337311
}

0 commit comments

Comments
 (0)