Skip to content

Commit 4a5ff90

Browse files
committed
remove unwrap_ts_expression
1 parent 3939bf9 commit 4a5ff90

File tree

7 files changed

+14
-65
lines changed

7 files changed

+14
-65
lines changed

packages/svelte/src/compiler/phases/2-analyze/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import {
77
extract_paths,
88
is_event_attribute,
99
is_text_attribute,
10-
object,
11-
unwrap_ts_expression
10+
object
1211
} from '../../utils/ast.js';
1312
import * as b from '../../utils/builders.js';
1413
import { ReservedKeywords, Runes, SVGElements } from '../constants.js';
@@ -706,7 +705,7 @@ const runes_scope_tweaker = {
706705
}
707706
},
708707
VariableDeclarator(node, { state }) {
709-
const init = unwrap_ts_expression(node.init);
708+
const init = node.init;
710709
if (!init || init.type !== 'CallExpression') return;
711710
const rune = get_rune(init, state.scope);
712711
if (rune === null) return;

packages/svelte/src/compiler/phases/2-analyze/validation.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { error } from '../../errors.js';
2-
import {
3-
extract_identifiers,
4-
get_parent,
5-
is_text_attribute,
6-
unwrap_ts_expression
7-
} from '../../utils/ast.js';
2+
import { extract_identifiers, get_parent, is_text_attribute } from '../../utils/ast.js';
83
import { warn } from '../../warnings.js';
94
import fuzzymatch from '../1-parse/utils/fuzzymatch.js';
105
import { disallowed_parapgraph_contents, interactive_elements } from '../1-parse/utils/html.js';
@@ -338,11 +333,11 @@ const validation = {
338333
BindDirective(node, context) {
339334
validate_no_const_assignment(node, node.expression, context.state.scope, true);
340335

341-
const assignee = unwrap_ts_expression(node.expression);
336+
const assignee = node.expression;
342337
let left = assignee;
343338

344339
while (left.type === 'MemberExpression') {
345-
left = unwrap_ts_expression(/** @type {import('estree').MemberExpression} */ (left.object));
340+
left = /** @type {import('estree').MemberExpression} */ (left.object);
346341
}
347342

348343
if (left.type !== 'Identifier') {
@@ -950,7 +945,7 @@ export const validation_runes = merge(validation, a11y_validators, {
950945
next({ ...state });
951946
},
952947
VariableDeclarator(node, { state, path }) {
953-
const init = unwrap_ts_expression(node.init);
948+
const init = node.init;
954949
const rune = get_rune(init, state.scope);
955950

956951
if (rune === null) return;

packages/svelte/src/compiler/phases/3-transform/client/utils.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import * as b from '../../../utils/builders.js';
2-
import {
3-
extract_paths,
4-
is_simple_expression,
5-
object,
6-
unwrap_ts_expression
7-
} from '../../../utils/ast.js';
2+
import { extract_paths, is_simple_expression, object } from '../../../utils/ast.js';
83
import { error } from '../../../errors.js';
94
import {
105
PROPS_IS_LAZY_INITIAL,
@@ -228,7 +223,7 @@ function is_expression_async(expression) {
228223
export function serialize_set_binding(node, context, fallback, options) {
229224
const { state, visit } = context;
230225

231-
const assignee = unwrap_ts_expression(node.left);
226+
const assignee = node.left;
232227
if (
233228
assignee.type === 'ArrayPattern' ||
234229
assignee.type === 'ObjectPattern' ||

packages/svelte/src/compiler/phases/3-transform/client/visitors/javascript-runes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { is_hoistable_function, transform_inspect_rune } from '../../utils.js';
33
import * as b from '../../../../utils/builders.js';
44
import * as assert from '../../../../utils/assert.js';
55
import { get_prop_source, is_state_source, should_proxy_or_freeze } from '../utils.js';
6-
import { extract_paths, unwrap_ts_expression } from '../../../../utils/ast.js';
6+
import { extract_paths } from '../../../../utils/ast.js';
77

88
/** @type {import('../types.js').ComponentVisitors} */
99
export const javascript_visitors_runes = {
@@ -174,7 +174,7 @@ export const javascript_visitors_runes = {
174174
const declarations = [];
175175

176176
for (const declarator of node.declarations) {
177-
const init = unwrap_ts_expression(declarator.init);
177+
const init = declarator.init;
178178
const rune = get_rune(init, state.scope);
179179
if (!rune || rune === '$effect.active' || rune === '$effect.root' || rune === '$inspect') {
180180
if (init != null && is_hoistable_function(init)) {

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import {
33
extract_paths,
44
is_event_attribute,
55
is_text_attribute,
6-
object,
7-
unwrap_ts_expression
6+
object
87
} from '../../../../utils/ast.js';
98
import { binding_properties } from '../../../bindings.js';
109
import {
@@ -2576,7 +2575,7 @@ export const template_visitors = {
25762575
},
25772576
BindDirective(node, context) {
25782577
const { state, path, visit } = context;
2579-
const expression = unwrap_ts_expression(node.expression);
2578+
const expression = node.expression;
25802579
const getter = b.thunk(/** @type {import('estree').Expression} */ (visit(expression)));
25812580
const assignment = b.assignment('=', expression, b.id('$$value'));
25822581
const setter = b.arrow(

packages/svelte/src/compiler/phases/3-transform/server/transform-server.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { walk } from 'zimmerframe';
22
import { set_scope, get_rune } from '../../scope.js';
3-
import {
4-
extract_identifiers,
5-
extract_paths,
6-
is_event_attribute,
7-
unwrap_ts_expression
8-
} from '../../../utils/ast.js';
3+
import { extract_identifiers, extract_paths, is_event_attribute } from '../../../utils/ast.js';
94
import * as b from '../../../utils/builders.js';
105
import is_reference from 'is-reference';
116
import {
@@ -569,7 +564,7 @@ const javascript_visitors_runes = {
569564
const declarations = [];
570565

571566
for (const declarator of node.declarations) {
572-
const init = unwrap_ts_expression(declarator.init);
567+
const init = declarator.init;
573568
const rune = get_rune(init, state.scope);
574569
if (!rune || rune === '$effect.active' || rune === '$inspect') {
575570
declarations.push(/** @type {import('estree').VariableDeclarator} */ (visit(declarator)));

packages/svelte/src/compiler/utils/ast.js

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ import * as b from '../utils/builders.js';
66
* @returns {import('estree').Identifier | null}
77
*/
88
export function object(expression) {
9-
expression = unwrap_ts_expression(expression);
10-
119
while (expression.type === 'MemberExpression') {
1210
expression = /** @type {import('estree').MemberExpression | import('estree').Identifier} */ (
1311
expression.object
1412
);
15-
expression = unwrap_ts_expression(expression);
1613
}
1714

1815
if (expression.type !== 'Identifier') {
@@ -268,37 +265,6 @@ function _extract_paths(assignments = [], param, expression, update_expression)
268265
return assignments;
269266
}
270267

271-
/**
272-
* The Acorn TS plugin defines `foo!` as a `TSNonNullExpression` node, and
273-
* `foo as Bar` as a `TSAsExpression` node. This function unwraps those.
274-
*
275-
* We can't just remove the typescript AST nodes in the parser stage because subsequent
276-
* parsing would fail, since AST start/end nodes would point at the wrong positions.
277-
*
278-
* @template {import('#compiler').SvelteNode | undefined | null} T
279-
* @param {T} node
280-
* @returns {T}
281-
*/
282-
export function unwrap_ts_expression(node) {
283-
if (!node) {
284-
return node;
285-
}
286-
287-
if (
288-
// @ts-expect-error these types don't exist on the base estree types
289-
node.type === 'TSNonNullExpression' ||
290-
// @ts-expect-error these types don't exist on the base estree types
291-
node.type === 'TSAsExpression' ||
292-
// @ts-expect-error these types don't exist on the base estree types
293-
node.type === 'TSSatisfiesExpression'
294-
) {
295-
// @ts-expect-error
296-
return node.expression;
297-
}
298-
299-
return node;
300-
}
301-
302268
/**
303269
* Like `path.at(x)`, but skips over `TSNonNullExpression` and `TSAsExpression` nodes and eases assertions a bit
304270
* by removing the `| undefined` from the resulting type.

0 commit comments

Comments
 (0)