diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 48491e410c..330f824333 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -16,6 +16,7 @@ import DomBlock from './dom/Block'; import SsrBlock from './server-side-rendering/Block'; import Stylesheet from '../css/Stylesheet'; import { Node, GenerateOptions, Parsed, CompileOptions } from '../interfaces'; +import { Computation, TemplateProperties } from './interfaces'; const test = typeof global !== 'undefined' && global.__svelte_test; @@ -26,12 +27,18 @@ export default class Generator { name: string; options: CompileOptions; + defaultExport: Node[]; imports: Node[]; helpers: Set; components: Set; events: Set; transitions: Set; importedComponents: Map; + namespace: string; + hasComponents: boolean; + hasJs: boolean; + computations: Computation[]; + templateProperties: TemplateProperties; code: MagicString; @@ -379,9 +386,9 @@ export default class Generator { return alias; } - getUniqueNameMaker(params) { + getUniqueNameMaker(params: string[]) { const localUsedNames = new Set(params); - return name => { + return (name: string) => { if (test) name = `${name}$`; let alias = name; for ( @@ -402,8 +409,8 @@ export default class Generator { const { js } = this.parsed; const imports = this.imports; - const computations = []; - const templateProperties = {}; + const computations: Computation[] = []; + const templateProperties: TemplateProperties = {}; let namespace = null; let hasJs = !!js; @@ -459,7 +466,7 @@ export default class Generator { const visited = new Set(); - function visit(key) { + function visit(key: string) { if (!dependencies.has(key)) return; // not a computation if (visited.has(key)) return; diff --git a/src/generators/dom/index.ts b/src/generators/dom/index.ts index 073471e5b0..03cdd0d7bd 100644 --- a/src/generators/dom/index.ts +++ b/src/generators/dom/index.ts @@ -317,7 +317,7 @@ export default function dom( let scope = annotateWithScopes(expression); walk(expression, { - enter(node, parent) { + enter(node: Node, parent: Node) { if (node._scope) scope = node._scope; if ( @@ -337,7 +337,7 @@ export default function dom( } }, - leave(node) { + leave(node: Node) { if (node._scope) scope = scope.parent; }, }); diff --git a/src/generators/dom/interfaces.ts b/src/generators/dom/interfaces.ts index 03923b79bf..858680b7df 100644 --- a/src/generators/dom/interfaces.ts +++ b/src/generators/dom/interfaces.ts @@ -1,5 +1,5 @@ export interface State { - name: string; + name?: string; namespace: string; parentNode: string; parentNodes: string; diff --git a/src/generators/interfaces.ts b/src/generators/interfaces.ts new file mode 100644 index 0000000000..795122a8b5 --- /dev/null +++ b/src/generators/interfaces.ts @@ -0,0 +1,23 @@ +import { Node } from '../interfaces'; + +export interface Computation { + key: string; + deps: string[] +} + +export interface TemplateProperties { + computed?: Node; + components?: Node; + data?: Node; + events?: Node; + helpers?: Node; + methods?: Node; + namespace?: Node; + oncreate?: Node; + ondestroy?: Node; + transitions?: Node; + + // TODO remove in v2 + onrender?: Node; + onteardown?: Node; +} \ No newline at end of file diff --git a/src/interfaces.ts b/src/interfaces.ts index 4944bf8c46..fc0a6b1147 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -34,8 +34,10 @@ export interface Warning { toString: () => string; } +export type ModuleFormat = 'es' | 'amd' | 'cjs' | 'iife' | 'umd' | 'eval'; + export interface CompileOptions { - format?: string; + format?: ModuleFormat; name?: string; filename?: string; generate?: string; @@ -56,8 +58,6 @@ export interface CompileOptions { onwarn?: (warning: Warning) => void; } -export type ModuleFormat = 'es' | 'amd' | 'cjs' | 'iife' | 'umd' | 'eval'; - export interface GenerateOptions { name: string; format: ModuleFormat; diff --git a/src/parse/read/directives.ts b/src/parse/read/directives.ts index 0b61ad6350..c0c46d6af9 100644 --- a/src/parse/read/directives.ts +++ b/src/parse/read/directives.ts @@ -2,7 +2,7 @@ import { parseExpressionAt } from 'acorn'; import spaces from '../../utils/spaces'; import { Parser } from '../index'; -function readExpression(parser: Parser, start: number, quoteMark) { +function readExpression(parser: Parser, start: number, quoteMark: string|null) { let str = ''; let escaped = false; diff --git a/src/parse/read/script.ts b/src/parse/read/script.ts index 4d4a8559a1..42e8729e06 100644 --- a/src/parse/read/script.ts +++ b/src/parse/read/script.ts @@ -1,10 +1,11 @@ import { parse } from 'acorn'; import spaces from '../../utils/spaces'; import { Parser } from '../index'; +import { Node } from '../../interfaces'; const scriptClosingTag = ''; -export default function readScript(parser: Parser, start: number, attributes) { +export default function readScript(parser: Parser, start: number, attributes: Node[]) { const scriptStart = parser.index; const scriptEnd = parser.template.indexOf(scriptClosingTag, scriptStart); diff --git a/src/parse/read/style.ts b/src/parse/read/style.ts index d1882d67df..d12cd0ab41 100644 --- a/src/parse/read/style.ts +++ b/src/parse/read/style.ts @@ -1,8 +1,9 @@ import parse from 'css-tree/lib/parser/index.js'; import walk from 'css-tree/lib/utils/walk.js'; import { Parser } from '../index'; +import { Node } from '../../interfaces'; -export default function readStyle(parser: Parser, start: number, attributes) { +export default function readStyle(parser: Parser, start: number, attributes: Node[]) { const contentStart = parser.index; const styles = parser.readUntil(/<\/style>/); const contentEnd = parser.index; @@ -23,7 +24,7 @@ export default function readStyle(parser: Parser, start: number, attributes) { } // tidy up AST - walk.all(ast, node => { + walk.all(ast, (node: Node) => { if (node.loc) { node.start = node.loc.start.offset; node.end = node.loc.end.offset; diff --git a/src/parse/state/mustache.ts b/src/parse/state/mustache.ts index 716f20d288..88107af750 100644 --- a/src/parse/state/mustache.ts +++ b/src/parse/state/mustache.ts @@ -6,7 +6,7 @@ import { Node } from '../../interfaces'; const validIdentifier = /[a-zA-Z_$][a-zA-Z0-9_$]*/; -function trimWhitespace(block, trimBefore, trimAfter) { +function trimWhitespace(block: Node, trimBefore: boolean, trimAfter: boolean) { const firstChild = block.children[0]; const lastChild = block.children[block.children.length - 1]; @@ -220,6 +220,4 @@ export default function mustache(parser: Parser) { expression, }); } - - return null; } diff --git a/src/parse/state/tag.ts b/src/parse/state/tag.ts index cfa0561365..c77127b53f 100644 --- a/src/parse/state/tag.ts +++ b/src/parse/state/tag.ts @@ -78,7 +78,7 @@ export default function tag(parser: Parser) { data, }); - return null; + return; } const isClosingTag = parser.eat('/'); @@ -133,7 +133,7 @@ export default function tag(parser: Parser) { parent.end = parser.index; parser.stack.pop(); - return null; + return; } else if (disallowedContents.has(parent.name)) { // can this be a child of the parent element, or does it implicitly // close it, like `
  • one
  • two`? @@ -200,8 +200,6 @@ export default function tag(parser: Parser) { // don't push self-closing elements onto the stack parser.stack.push(element); } - - return null; } function readTagName(parser: Parser) { @@ -242,7 +240,7 @@ function readTagName(parser: Parser) { return name; } -function readAttribute(parser: Parser, uniqueNames) { +function readAttribute(parser: Parser, uniqueNames: Set) { const start = parser.index; let name = parser.readUntil(/(\s|=|\/|>)/); diff --git a/src/parse/state/text.ts b/src/parse/state/text.ts index 0f85040c21..bdd625569c 100644 --- a/src/parse/state/text.ts +++ b/src/parse/state/text.ts @@ -20,6 +20,4 @@ export default function text(parser: Parser) { type: 'Text', data: decodeCharacterReferences(data), }); - - return null; }