|
|
@ -15,6 +15,7 @@ import { Node, Ast, CompileOptions, CustomElementOptions } from '../interfaces';
|
|
|
|
import error from '../utils/error';
|
|
|
|
import error from '../utils/error';
|
|
|
|
import getCodeFrame from '../utils/getCodeFrame';
|
|
|
|
import getCodeFrame from '../utils/getCodeFrame';
|
|
|
|
import flattenReference from '../utils/flattenReference';
|
|
|
|
import flattenReference from '../utils/flattenReference';
|
|
|
|
|
|
|
|
import addToSet from '../utils/addToSet';
|
|
|
|
|
|
|
|
|
|
|
|
// We need to tell estree-walker that it should always
|
|
|
|
// We need to tell estree-walker that it should always
|
|
|
|
// look for an `else` block, otherwise it might get
|
|
|
|
// look for an `else` block, otherwise it might get
|
|
|
@ -44,31 +45,32 @@ export default class Component {
|
|
|
|
|
|
|
|
|
|
|
|
properties: Map<string, Node>;
|
|
|
|
properties: Map<string, Node>;
|
|
|
|
|
|
|
|
|
|
|
|
imports: Node[];
|
|
|
|
imports: Node[] = [];
|
|
|
|
namespace: string;
|
|
|
|
namespace: string;
|
|
|
|
hasComponents: boolean;
|
|
|
|
hasComponents: boolean;
|
|
|
|
javascript: string;
|
|
|
|
javascript: string;
|
|
|
|
|
|
|
|
|
|
|
|
declarations: string[];
|
|
|
|
declarations: string[] = [];
|
|
|
|
exports: Array<{ name: string, as: string }>;
|
|
|
|
writable_declarations: Set<string> = new Set();
|
|
|
|
event_handlers: Array<{ name: string, body: string }>;
|
|
|
|
initialised_declarations: Set<string> = new Set();
|
|
|
|
props: string[];
|
|
|
|
exports: Array<{ name: string, as: string }> = [];
|
|
|
|
|
|
|
|
event_handlers: Array<{ name: string, body: string }> = [];
|
|
|
|
|
|
|
|
|
|
|
|
code: MagicString;
|
|
|
|
code: MagicString;
|
|
|
|
|
|
|
|
|
|
|
|
indirectDependencies: Map<string, Set<string>>;
|
|
|
|
indirectDependencies: Map<string, Set<string>> = new Map();
|
|
|
|
expectedProperties: Set<string>;
|
|
|
|
expectedProperties: Set<string> = new Set();
|
|
|
|
refs: Set<string>;
|
|
|
|
refs: Set<string> = new Set();
|
|
|
|
|
|
|
|
|
|
|
|
file: string;
|
|
|
|
file: string;
|
|
|
|
locate: (c: number) => { line: number, column: number };
|
|
|
|
locate: (c: number) => { line: number, column: number };
|
|
|
|
|
|
|
|
|
|
|
|
stylesheet: Stylesheet;
|
|
|
|
stylesheet: Stylesheet;
|
|
|
|
|
|
|
|
|
|
|
|
userVars: Set<string>;
|
|
|
|
userVars: Set<string> = new Set();
|
|
|
|
templateVars: Map<string, string>;
|
|
|
|
templateVars: Map<string, string> = new Map();
|
|
|
|
aliases: Map<string, string>;
|
|
|
|
aliases: Map<string, string> = new Map();
|
|
|
|
usedNames: Set<string>;
|
|
|
|
usedNames: Set<string> = new Set();
|
|
|
|
init_uses_self = false;
|
|
|
|
init_uses_self = false;
|
|
|
|
|
|
|
|
|
|
|
|
locator: (search: number, startIndex?: number) => {
|
|
|
|
locator: (search: number, startIndex?: number) => {
|
|
|
@ -89,38 +91,17 @@ export default class Component {
|
|
|
|
this.source = source;
|
|
|
|
this.source = source;
|
|
|
|
this.options = options;
|
|
|
|
this.options = options;
|
|
|
|
|
|
|
|
|
|
|
|
this.imports = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.declarations = [];
|
|
|
|
|
|
|
|
this.exports = [];
|
|
|
|
|
|
|
|
this.event_handlers = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.refs = new Set();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.indirectDependencies = new Map();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.file = options.filename && (
|
|
|
|
this.file = options.filename && (
|
|
|
|
typeof process !== 'undefined' ? options.filename.replace(process.cwd(), '').replace(/^[\/\\]/, '') : options.filename
|
|
|
|
typeof process !== 'undefined' ? options.filename.replace(process.cwd(), '').replace(/^[\/\\]/, '') : options.filename
|
|
|
|
);
|
|
|
|
);
|
|
|
|
this.locate = getLocator(this.source);
|
|
|
|
this.locate = getLocator(this.source);
|
|
|
|
|
|
|
|
|
|
|
|
// track which properties are needed, so we can provide useful info
|
|
|
|
|
|
|
|
// in dev mode
|
|
|
|
|
|
|
|
this.expectedProperties = new Set();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.code = new MagicString(source);
|
|
|
|
this.code = new MagicString(source);
|
|
|
|
|
|
|
|
|
|
|
|
// styles
|
|
|
|
// styles
|
|
|
|
this.stylesheet = new Stylesheet(source, ast, options.filename, options.dev);
|
|
|
|
this.stylesheet = new Stylesheet(source, ast, options.filename, options.dev);
|
|
|
|
this.stylesheet.validate(this);
|
|
|
|
this.stylesheet.validate(this);
|
|
|
|
|
|
|
|
|
|
|
|
// allow compiler to deconflict user's `import { flush } from 'whatever'` and
|
|
|
|
|
|
|
|
// Svelte's builtin `import { flush, ... } from 'svelte/internal.ts'`;
|
|
|
|
|
|
|
|
this.userVars = new Set();
|
|
|
|
|
|
|
|
this.templateVars = new Map();
|
|
|
|
|
|
|
|
this.aliases = new Map();
|
|
|
|
|
|
|
|
this.usedNames = new Set();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.properties = new Map();
|
|
|
|
this.properties = new Map();
|
|
|
|
|
|
|
|
|
|
|
|
this.walkJs();
|
|
|
|
this.walkJs();
|
|
|
@ -150,6 +131,7 @@ export default class Component {
|
|
|
|
|
|
|
|
|
|
|
|
if (!this.ast.js) {
|
|
|
|
if (!this.ast.js) {
|
|
|
|
this.declarations = Array.from(this.expectedProperties);
|
|
|
|
this.declarations = Array.from(this.expectedProperties);
|
|
|
|
|
|
|
|
addToSet(this.writable_declarations, this.expectedProperties);
|
|
|
|
|
|
|
|
|
|
|
|
this.exports = this.declarations.map(name => ({
|
|
|
|
this.exports = this.declarations.map(name => ({
|
|
|
|
name,
|
|
|
|
name,
|
|
|
@ -390,6 +372,9 @@ export default class Component {
|
|
|
|
this.declarations.push(name);
|
|
|
|
this.declarations.push(name);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.writable_declarations = scope.writable_declarations;
|
|
|
|
|
|
|
|
this.initialised_declarations = scope.initialised_declarations;
|
|
|
|
|
|
|
|
|
|
|
|
globals.forEach(name => {
|
|
|
|
globals.forEach(name => {
|
|
|
|
this.userVars.add(name);
|
|
|
|
this.userVars.add(name);
|
|
|
|
});
|
|
|
|
});
|
|
|
@ -401,7 +386,7 @@ export default class Component {
|
|
|
|
this.error(node, {
|
|
|
|
this.error(node, {
|
|
|
|
code: `default-export`,
|
|
|
|
code: `default-export`,
|
|
|
|
message: `A component cannot have a default export`
|
|
|
|
message: `A component cannot have a default export`
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (node.type === 'ExportNamedDeclaration') {
|
|
|
|
if (node.type === 'ExportNamedDeclaration') {
|
|
|
|