Skip to content

Commit ce3679e

Browse files
authored
(fix) don't track propertyName of named import alias (#551)
#549 Don't track `a` in `import {a as b} from ..` as a declared variable
1 parent 5c08e93 commit ce3679e

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

packages/svelte2tsx/src/svelte2tsx/processInstanceScriptContent.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import MagicString from 'magic-string';
22
import { Node } from 'estree-walker';
33
import * as ts from 'typescript';
4-
import { findExportKeyword, getBinaryAssignmentExpr } from './utils/tsAst';
4+
import {
5+
findExportKeyword,
6+
getBinaryAssignmentExpr,
7+
isNotPropertyNameOfImport,
8+
} from './utils/tsAst';
59
import { ExportedNames } from './nodes/ExportedNames';
610
import { ImplicitTopLevelNames } from './nodes/ImplicitTopLevelNames';
711
import { ComponentEvents } from './nodes/ComponentEvents';
@@ -231,7 +235,10 @@ export function processInstanceScriptContent(
231235
}
232236

233237
if (isDeclaration || ts.isParameter(parent)) {
234-
if (!ts.isBindingElement(ident.parent) || ident.parent.name == ident) {
238+
if (
239+
isNotPropertyNameOfImport(ident) &&
240+
(!ts.isBindingElement(ident.parent) || ident.parent.name == ident)
241+
) {
235242
// we are a key, not a name, so don't care
236243
if (ident.text.startsWith('$') || scope == rootScope) {
237244
// track all top level declared identifiers and all $ prefixed identifiers

packages/svelte2tsx/src/svelte2tsx/utils/tsAst.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,13 @@ export function getLeadingDoc(node: ts.Node): string | undefined {
134134
return nodeText.substring(comment.pos, comment.end);
135135
}
136136
}
137+
138+
/**
139+
* Returns true if given identifier is not the property name of an aliased import.
140+
* In other words: It is not `a` in `import {a as b} from ..`
141+
*/
142+
export function isNotPropertyNameOfImport(identifier: ts.Identifier): boolean {
143+
return (
144+
!ts.isImportSpecifier(identifier.parent) || identifier.parent.propertyName !== identifier
145+
);
146+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
///<reference types="svelte" />
2+
<></>;
3+
import { b as c } from "foo";
4+
function render() {
5+
6+
7+
let b = __sveltets_invalidate(() => 7);
8+
9+
let a;
10+
$: a = __sveltets_invalidate(() => 5);
11+
;
12+
() => (<></>);
13+
return { props: {}, slots: {}, getters: {}, events: {} }}
14+
15+
export default class Input__SvelteComponent_ extends createSvelte2TsxComponent(__sveltets_partial(__sveltets_with_any_event(render))) {
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { b as c } from "foo";
3+
$: b = 7;
4+
5+
let a;
6+
$: a = 5;
7+
</script>

0 commit comments

Comments
 (0)