|
|
@ -24,10 +24,10 @@ import TemplateScope from './nodes/shared/TemplateScope';
|
|
|
|
import fuzzymatch from '../utils/fuzzymatch';
|
|
|
|
import fuzzymatch from '../utils/fuzzymatch';
|
|
|
|
import get_object from './utils/get_object';
|
|
|
|
import get_object from './utils/get_object';
|
|
|
|
import Slot from './nodes/Slot';
|
|
|
|
import Slot from './nodes/Slot';
|
|
|
|
import { Node, ImportDeclaration, ExportNamedDeclaration, Identifier, Program, ExpressionStatement, AssignmentExpression, Literal, ExportDefaultDeclaration, ExportAllDeclaration } from 'estree';
|
|
|
|
import { Node, ImportDeclaration, ExportNamedDeclaration, Identifier, ExpressionStatement, AssignmentExpression, Literal, Property, RestElement, ExportDefaultDeclaration, ExportAllDeclaration } from 'estree';
|
|
|
|
import add_to_set from './utils/add_to_set';
|
|
|
|
import add_to_set from './utils/add_to_set';
|
|
|
|
import check_graph_for_cycles from './utils/check_graph_for_cycles';
|
|
|
|
import check_graph_for_cycles from './utils/check_graph_for_cycles';
|
|
|
|
import { print, x, b } from 'code-red';
|
|
|
|
import { print, b } from 'code-red';
|
|
|
|
import { is_reserved_keyword } from './utils/reserved_keywords';
|
|
|
|
import { is_reserved_keyword } from './utils/reserved_keywords';
|
|
|
|
import { apply_preprocessor_sourcemap } from '../utils/mapped_code';
|
|
|
|
import { apply_preprocessor_sourcemap } from '../utils/mapped_code';
|
|
|
|
import Element from './nodes/Element';
|
|
|
|
import Element from './nodes/Element';
|
|
|
@ -943,7 +943,7 @@ export default class Component {
|
|
|
|
let scope = instance_scope;
|
|
|
|
let scope = instance_scope;
|
|
|
|
|
|
|
|
|
|
|
|
walk(this.ast.instance.content, {
|
|
|
|
walk(this.ast.instance.content, {
|
|
|
|
enter(node: Node, parent, key, index) {
|
|
|
|
enter(node: Node) {
|
|
|
|
if (/Function/.test(node.type)) {
|
|
|
|
if (/Function/.test(node.type)) {
|
|
|
|
return this.skip();
|
|
|
|
return this.skip();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -952,75 +952,130 @@ export default class Component {
|
|
|
|
scope = map.get(node);
|
|
|
|
scope = map.get(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (node.type === 'ExportNamedDeclaration' && node.declaration) {
|
|
|
|
|
|
|
|
return this.replace(node.declaration);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (node.type === 'VariableDeclaration') {
|
|
|
|
if (node.type === 'VariableDeclaration') {
|
|
|
|
|
|
|
|
// NOTE: `var` does not follow block scoping
|
|
|
|
if (node.kind === 'var' || scope === instance_scope) {
|
|
|
|
if (node.kind === 'var' || scope === instance_scope) {
|
|
|
|
node.declarations.forEach(declarator => {
|
|
|
|
|
|
|
|
if (declarator.id.type !== 'Identifier') {
|
|
|
|
|
|
|
|
const inserts = [];
|
|
|
|
const inserts = [];
|
|
|
|
|
|
|
|
const props = [];
|
|
|
|
|
|
|
|
|
|
|
|
extract_names(declarator.id).forEach(name => {
|
|
|
|
function add_new_props(exported, local, default_value) {
|
|
|
|
const variable = component.var_lookup.get(name);
|
|
|
|
props.push({
|
|
|
|
|
|
|
|
type: 'Property',
|
|
|
|
if (variable.export_name) {
|
|
|
|
method: false,
|
|
|
|
// TODO is this still true post-#3539?
|
|
|
|
shorthand: false,
|
|
|
|
return component.error(declarator as any, compiler_errors.destructured_prop);
|
|
|
|
computed: false,
|
|
|
|
|
|
|
|
kind: 'init',
|
|
|
|
|
|
|
|
key: exported,
|
|
|
|
|
|
|
|
value: default_value
|
|
|
|
|
|
|
|
? {
|
|
|
|
|
|
|
|
type: 'AssignmentPattern',
|
|
|
|
|
|
|
|
left: local,
|
|
|
|
|
|
|
|
right: default_value
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
: local
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// transform
|
|
|
|
|
|
|
|
// ```
|
|
|
|
|
|
|
|
// export let { x, y = 123 } = OBJ, z = 456
|
|
|
|
|
|
|
|
// ```
|
|
|
|
|
|
|
|
// into
|
|
|
|
|
|
|
|
// ```
|
|
|
|
|
|
|
|
// let { x: x$, y: y$ = 123 } = OBJ;
|
|
|
|
|
|
|
|
// let { x = x$, y = y$, z = 456 } = $$props;
|
|
|
|
|
|
|
|
// ```
|
|
|
|
|
|
|
|
for (let index = 0; index < node.declarations.length; index++) {
|
|
|
|
|
|
|
|
const declarator = node.declarations[index];
|
|
|
|
|
|
|
|
if (declarator.id.type !== 'Identifier') {
|
|
|
|
|
|
|
|
function get_new_name(local) {
|
|
|
|
|
|
|
|
const variable = component.var_lookup.get(local.name);
|
|
|
|
if (variable.subscribable) {
|
|
|
|
if (variable.subscribable) {
|
|
|
|
inserts.push(get_insert(variable));
|
|
|
|
inserts.push(get_insert(variable));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (inserts.length) {
|
|
|
|
if (variable.export_name && variable.writable) {
|
|
|
|
parent[key].splice(index + 1, 0, ...inserts);
|
|
|
|
const alias_name = component.get_unique_name(local.name);
|
|
|
|
|
|
|
|
add_new_props({ type: 'Identifier', name: variable.export_name }, local, alias_name);
|
|
|
|
|
|
|
|
return alias_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return local;
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const { name } = declarator.id;
|
|
|
|
function rename_identifiers(param: Node) {
|
|
|
|
const variable = component.var_lookup.get(name);
|
|
|
|
switch (param.type) {
|
|
|
|
|
|
|
|
case 'ObjectPattern': {
|
|
|
|
|
|
|
|
const handle_prop = (prop: Property | RestElement) => {
|
|
|
|
|
|
|
|
if (prop.type === 'RestElement') {
|
|
|
|
|
|
|
|
rename_identifiers(prop);
|
|
|
|
|
|
|
|
} else if (prop.value.type === 'Identifier') {
|
|
|
|
|
|
|
|
prop.value = get_new_name(prop.value);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
rename_identifiers(prop.value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (variable.export_name && variable.writable) {
|
|
|
|
param.properties.forEach(handle_prop);
|
|
|
|
declarator.id = {
|
|
|
|
break;
|
|
|
|
type: 'ObjectPattern',
|
|
|
|
}
|
|
|
|
properties: [{
|
|
|
|
case 'ArrayPattern': {
|
|
|
|
type: 'Property',
|
|
|
|
const handle_element = (element: Node, index: number, array: Node[]) => {
|
|
|
|
method: false,
|
|
|
|
if (element) {
|
|
|
|
shorthand: false,
|
|
|
|
if (element.type === 'Identifier') {
|
|
|
|
computed: false,
|
|
|
|
array[index] = get_new_name(element);
|
|
|
|
kind: 'init',
|
|
|
|
} else {
|
|
|
|
key: { type: 'Identifier', name: variable.export_name },
|
|
|
|
rename_identifiers(element);
|
|
|
|
value: declarator.init
|
|
|
|
}
|
|
|
|
? {
|
|
|
|
|
|
|
|
type: 'AssignmentPattern',
|
|
|
|
|
|
|
|
left: declarator.id,
|
|
|
|
|
|
|
|
right: declarator.init
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
: declarator.id
|
|
|
|
|
|
|
|
}]
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
declarator.init = x`$$props`;
|
|
|
|
param.elements.forEach(handle_element);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (variable.subscribable && declarator.init) {
|
|
|
|
case 'RestElement':
|
|
|
|
const insert = get_insert(variable);
|
|
|
|
param.argument = get_new_name(param.argument);
|
|
|
|
parent[key].splice(index + 1, 0, ...insert);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'AssignmentPattern':
|
|
|
|
|
|
|
|
param.left = get_new_name(param.left);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rename_identifiers(declarator.id);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
const { name } = declarator.id;
|
|
|
|
|
|
|
|
const variable = component.var_lookup.get(name);
|
|
|
|
|
|
|
|
const is_props = variable.export_name && variable.writable;
|
|
|
|
|
|
|
|
if (is_props) {
|
|
|
|
|
|
|
|
add_new_props({ type: 'Identifier', name: variable.export_name }, declarator.id, declarator.init);
|
|
|
|
|
|
|
|
node.declarations.splice(index--, 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (variable.subscribable && (is_props || declarator.init)) {
|
|
|
|
|
|
|
|
inserts.push(get_insert(variable));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.replace(b`
|
|
|
|
|
|
|
|
${node.declarations.length ? node : null}
|
|
|
|
|
|
|
|
${ props.length > 0 && b`let { ${ props } } = $$props;`}
|
|
|
|
|
|
|
|
${inserts}
|
|
|
|
|
|
|
|
` as any);
|
|
|
|
|
|
|
|
return this.skip();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
leave(node: Node, parent, _key, index) {
|
|
|
|
leave(node: Node) {
|
|
|
|
if (map.has(node)) {
|
|
|
|
if (map.has(node)) {
|
|
|
|
scope = scope.parent;
|
|
|
|
scope = scope.parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (node.type === 'ExportNamedDeclaration' && node.declaration) {
|
|
|
|
|
|
|
|
(parent as Program).body[index] = node.declaration;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|