-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Fix overly aggressive function hoisting (#3125) #4000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
697e733
a1337fd
0d9d8dc
0fb0f16
7703e22
32768fa
f6060ed
91de025
6773aff
718d17e
14310b2
0f1879b
923c89b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,6 +161,7 @@ export interface Var { | |
hoistable?: boolean; | ||
subscribable?: boolean; | ||
is_reactive_dependency?: boolean; | ||
aliased?: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea was to have We'd still have to track all forms of aliasing though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tracking all forms of aliasing doesn't sound like it would be possible. Is this another situation where we just need to document how the compiler works - like how only certain types of assignments trigger reactivity, rather than embarking on a never-ending journey to catch more cases? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, why not? We wouldn't need perfect tracking, just knowing whether the variable escapes a single name -- the possibility it's used elsewhere. The other option would be to disable function hoisting (like currently for objects), since the way it stands functions that shouldn't be hoisted, are. |
||
} | ||
|
||
export interface CssResult { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* generated by Svelte vX.Y.Z */ | ||
import { SvelteComponent, init, safe_not_equal } from "svelte/internal"; | ||
|
||
function instance($$self) { | ||
function foo() { | ||
|
||
} | ||
|
||
const foo_alias = foo; | ||
foo_alias.nasty_but_legal(); | ||
return []; | ||
} | ||
|
||
class Component extends SvelteComponent { | ||
constructor(options) { | ||
super(); | ||
init(this, options, instance, null, safe_not_equal, {}); | ||
} | ||
} | ||
|
||
export default Component; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script> | ||
function foo() {} | ||
const foo_alias = foo; | ||
foo_alias.nasty_but_legal(); | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* generated by Svelte vX.Y.Z */ | ||
import { | ||
SvelteComponent, | ||
detach, | ||
element, | ||
init, | ||
insert, | ||
noop, | ||
safe_not_equal | ||
} from "svelte/internal"; | ||
|
||
import { bar } from "./utils.js"; | ||
|
||
function create_fragment(ctx) { | ||
let p; | ||
|
||
return { | ||
c() { | ||
p = element("p"); | ||
p.textContent = `You'll be pleased to know the answer is ${/*foo*/ ctx[0]()}.`; | ||
}, | ||
m(target, anchor) { | ||
insert(target, p, anchor); | ||
}, | ||
p: noop, | ||
i: noop, | ||
o: noop, | ||
d(detaching) { | ||
if (detaching) detach(p); | ||
} | ||
}; | ||
} | ||
|
||
function instance($$self) { | ||
function foo() { | ||
return 42; | ||
} | ||
|
||
bar(foo); | ||
return [foo]; | ||
} | ||
|
||
class Component extends SvelteComponent { | ||
constructor(options) { | ||
super(); | ||
init(this, options, instance, create_fragment, safe_not_equal, {}); | ||
} | ||
} | ||
|
||
export default Component; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<script> | ||
import { bar } from './utils.js'; | ||
|
||
function foo() { | ||
return 42; | ||
} | ||
|
||
bar(foo); | ||
</script> | ||
|
||
<p>You'll be pleased to know the answer is {foo()}.</p> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function bar(fn) { | ||
console.log(`I'm safe but you'd never know, ${fn}!`); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is the right place for this. This is marking the function not hoistable if it references aliased variables, instead of marking the function not hoistable if itself is aliased.