Skip to content
Merged
Show file tree
Hide file tree
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 Aug 15, 2020
9e27746
basic support for destructuring
jasonlyu123 Aug 16, 2020
113c5e1
fix TemplateScope reference code url
jasonlyu123 Aug 16, 2020
f930623
indent
jasonlyu123 Aug 16, 2020
5552750
fix typos
jasonlyu123 Aug 16, 2020
819f99a
camelCase
jasonlyu123 Aug 16, 2020
d859755
Merge branch 'master' of https://github.com/sveltejs/language-tools i…
jasonlyu123 Aug 17, 2020
50d83c7
test for nest
jasonlyu123 Aug 17, 2020
b826904
var shadowing
jasonlyu123 Aug 17, 2020
2e950d1
catch block
jasonlyu123 Aug 18, 2020
6c4d89e
Merge branch 'master' of https://github.com/sveltejs/language-tools i…
jasonlyu123 Aug 22, 2020
2824b12
fix test
jasonlyu123 Aug 22, 2020
3e69807
test for member aceess, object key and object shorthand
jasonlyu123 Aug 22, 2020
32928ac
fix non object shorthand property
jasonlyu123 Aug 22, 2020
95f3bcd
support for component let:prop
jasonlyu123 Aug 23, 2020
ffca00c
lint
jasonlyu123 Aug 23, 2020
8a872b6
leave component scope
jasonlyu123 Aug 23, 2020
6998186
more test about scope
jasonlyu123 Aug 23, 2020
039e1fb
TemplateScope optional chaining
jasonlyu123 Aug 24, 2020
9f1f871
null check for resolving and getOwner
jasonlyu123 Aug 24, 2020
54a77dc
better typing for ast nodes
jasonlyu123 Aug 24, 2020
7aa64f6
(refactor) move handle scope and resolve to new file
jasonlyu123 Aug 24, 2020
6473e58
Merge branch 'slot-props-2' of https://github.com/jasonlyu123/languag…
jasonlyu123 Aug 24, 2020
b5c4af1
cleanup
jasonlyu123 Aug 25, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/svelte2tsx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@types/vfile": "^3.0.2",
"magic-string": "^0.25.4",
"mocha": "^6.2.2",
"periscopic": "^2.0.2",
"rollup": "^1.12.0",
"rollup-plugin-commonjs": "^10.0.0",
"rollup-plugin-delete": "^1.1.0",
Expand Down
10 changes: 4 additions & 6 deletions packages/svelte2tsx/src/htmlxtojsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { parseHtmlx } from '../htmlxparser';
import svgAttributes from './svgattributes';
import { getTypeForComponent } from './nodes/component-type';
import { handleAwait } from './nodes/await-block';
import { getSlotName } from '../utils/svelteAst';

type ElementType = string;
const oneWayBindingAttributes: Map<string, ElementType> = new Map(
Expand Down Expand Up @@ -344,12 +345,9 @@ export function convertHtmlxToJsx(
//we could lean on leave/enter, but I am lazy
if (!el.children) return;
for (const child of el.children) {
if (!child.attributes) continue;
const slot = child.attributes.find((a) => a.name == 'slot');
if (slot) {
if (slot.value && slot.value.length) {
handleSlot(child, el.name, slot.value[0].raw);
}
const slotName = getSlotName(child);
if (slotName) {
handleSlot(child, el.name, slotName);
}
}
};
Expand Down
34 changes: 34 additions & 0 deletions packages/svelte2tsx/src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import MagicString from 'magic-string';
import { Node } from 'estree-walker';
import { ArrayPattern, ObjectPattern, Identifier } from 'estree';
import { ExportedNames } from './nodes/ExportedNames';
import { ComponentEvents } from './nodes/ComponentEvents';

Expand All @@ -21,6 +22,39 @@ export interface CreateRenderFunctionPara extends InstanceScriptProcessResult {
isTsFile: boolean;
}

export interface NodeRange {
start: number;
end: number;
}

export interface SvelteIdentifier extends Identifier, NodeRange {}

export interface SvelteArrayPattern extends ArrayPattern, NodeRange {}

export interface SvelteObjectPattern extends ObjectPattern, NodeRange {}

export interface WithName {
type: string;
name: string;
}

export type DirectiveType =
| 'Action'
| 'Animation'
| 'Binding'
| 'Class'
| 'EventHandler'
| 'Let'
| 'Ref'
| 'Transition';

export interface BaseDirective extends Node {
type: DirectiveType;
expression: null | Node;
name: string;
modifiers: string[];
}

export interface AddComponentExportPara {
str: MagicString;
uses$$propsOr$$restProps: boolean;
Expand Down
48 changes: 48 additions & 0 deletions packages/svelte2tsx/src/nodes/TemplateScope.ts
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 packages/svelte2tsx/src/nodes/handleScopeAndResolveForSlot.ts
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,
);
}
}
}
Loading