-
-
Notifications
You must be signed in to change notification settings - Fork 223
Slot props reference block level variable #458
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f56c12e
basic await and each
jasonlyu123 9e27746
basic support for destructuring
jasonlyu123 113c5e1
fix TemplateScope reference code url
jasonlyu123 f930623
indent
jasonlyu123 5552750
fix typos
jasonlyu123 819f99a
camelCase
jasonlyu123 d859755
Merge branch 'master' of https://github.com/sveltejs/language-tools i…
jasonlyu123 50d83c7
test for nest
jasonlyu123 b826904
var shadowing
jasonlyu123 2e950d1
catch block
jasonlyu123 6c4d89e
Merge branch 'master' of https://github.com/sveltejs/language-tools i…
jasonlyu123 2824b12
fix test
jasonlyu123 3e69807
test for member aceess, object key and object shorthand
jasonlyu123 32928ac
fix non object shorthand property
jasonlyu123 95f3bcd
support for component let:prop
jasonlyu123 ffca00c
lint
jasonlyu123 8a872b6
leave component scope
jasonlyu123 6998186
more test about scope
jasonlyu123 039e1fb
TemplateScope optional chaining
jasonlyu123 9f1f871
null check for resolving and getOwner
jasonlyu123 54a77dc
better typing for ast nodes
jasonlyu123 7aa64f6
(refactor) move handle scope and resolve to new file
jasonlyu123 6473e58
Merge branch 'slot-props-2' of https://github.com/jasonlyu123/languag…
jasonlyu123 b5c4af1
cleanup
jasonlyu123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { Node } from 'estree-walker'; | ||
| import { WithName } from '../interfaces'; | ||
|
|
||
| /** | ||
| * adopted from https://github.com/sveltejs/svelte/blob/master/src/compiler/compile/nodes/shared/TemplateScope.ts | ||
| */ | ||
| export default class TemplateScope { | ||
| names: Set<string>; | ||
| owners: Map<string, Node> = new Map(); | ||
| inits: Map<string, WithName> = new Map(); | ||
| parent?: TemplateScope; | ||
|
|
||
| constructor(parent?: TemplateScope) { | ||
| this.parent = parent; | ||
| this.names = new Set(parent ? parent.names : []); | ||
| } | ||
|
|
||
| addMany(inits: WithName[], owner: Node) { | ||
| inits.forEach((item) => this.add(item, owner)); | ||
| return this; | ||
| } | ||
|
|
||
| add(init: WithName, owner: Node) { | ||
| const { name } = init; | ||
| this.names.add(name); | ||
| this.inits.set(name, init); | ||
| this.owners.set(name, owner); | ||
| return this; | ||
| } | ||
|
|
||
| child() { | ||
| const child = new TemplateScope(this); | ||
| return child; | ||
| } | ||
|
|
||
| getOwner(name: string): Node { | ||
| return this.owners.get(name) || this.parent?.getOwner(name); | ||
| } | ||
|
|
||
| getInit(name: string): WithName { | ||
| return this.inits.get(name) || this.parent?.getInit(name); | ||
| } | ||
|
|
||
| isLet(name: string) { | ||
| const owner = this.getOwner(name); | ||
| return owner && (owner.type === 'Element' || owner.type === 'InlineComponent'); | ||
| } | ||
| } | ||
84 changes: 84 additions & 0 deletions
84
packages/svelte2tsx/src/nodes/handleScopeAndResolveForSlot.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { Node } from 'estree-walker'; | ||
| import { BaseDirective, SvelteIdentifier } from '../interfaces'; | ||
| import TemplateScope from './TemplateScope'; | ||
| import { SlotHandler } from './slot'; | ||
| import { isIdentifier, isDestructuringPatterns } from '../utils/svelteAst'; | ||
| import { extract_identifiers as extractIdentifiers } from 'periscopic'; | ||
|
|
||
| export function handleScopeAndResolveForSlot({ | ||
| identifierDef, | ||
| initExpression, | ||
| owner, | ||
| slotHandler, | ||
| templateScope, | ||
| }: { | ||
| identifierDef: Node; | ||
| initExpression: Node; | ||
| owner: Node; | ||
| slotHandler: SlotHandler; | ||
| templateScope: TemplateScope; | ||
| }) { | ||
| if (isIdentifier(identifierDef)) { | ||
| templateScope.add(identifierDef, owner); | ||
|
|
||
| slotHandler.resolve(identifierDef, initExpression, templateScope); | ||
| } | ||
| if (isDestructuringPatterns(identifierDef)) { | ||
| // the node object is returned as-it with no mutation | ||
| const identifiers = extractIdentifiers(identifierDef) as SvelteIdentifier[]; | ||
| templateScope.addMany(identifiers, owner); | ||
|
|
||
| slotHandler.resolveDestructuringAssignment( | ||
| identifierDef, | ||
| identifiers, | ||
| initExpression, | ||
| templateScope, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| export function handleScopeAndResolveLetVarForSlot({ | ||
| letNode, | ||
| component, | ||
| slotName, | ||
| templateScope, | ||
| slotHandler, | ||
| }: { | ||
| letNode: BaseDirective; | ||
| slotName: string; | ||
| component: Node; | ||
| templateScope: TemplateScope; | ||
| slotHandler: SlotHandler; | ||
| }) { | ||
| const { expression } = letNode; | ||
| // <Component let:a> | ||
| if (!expression) { | ||
| templateScope.add(letNode, component); | ||
| slotHandler.resolveLet(letNode, letNode, component, slotName); | ||
| } else { | ||
| if (isIdentifier(expression)) { | ||
| templateScope.add(expression, component); | ||
| slotHandler.resolveLet(letNode, expression, component, slotName); | ||
| } | ||
| const expForExtract = { ...expression }; | ||
|
|
||
| // https://github.com/sveltejs/svelte/blob/3a37de364bfbe75202d8e9fcef9e76b9ce6faaa2/src/compiler/compile/nodes/Let.ts#L37 | ||
| if (expression.type === 'ArrayExpression') { | ||
| expForExtract.type = 'ArrayPattern'; | ||
| } else if (expression.type === 'ObjectExpression') { | ||
| expForExtract.type = 'ObjectPattern'; | ||
| } | ||
| if (isDestructuringPatterns(expForExtract)) { | ||
| const identifiers = extractIdentifiers(expForExtract) as SvelteIdentifier[]; | ||
| templateScope.addMany(identifiers, component); | ||
|
|
||
| slotHandler.resolveDestructuringAssignmentForLet( | ||
| expForExtract, | ||
| identifiers, | ||
| letNode, | ||
| component, | ||
| slotName, | ||
| ); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.