From dc82db609c92ee65993fe4abb9495c66812288c1 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sun, 21 May 2017 19:18:21 -0400 Subject: [PATCH] more TS --- src/generators/Generator.ts | 20 +++++---- src/generators/dom/Block.ts | 41 +++++++++++++++---- .../dom/visitors/Component/Attribute.ts | 6 ++- .../dom/visitors/Component/Binding.ts | 5 ++- .../dom/visitors/Component/Component.ts | 5 ++- .../dom/visitors/Component/EventHandler.ts | 5 ++- src/generators/dom/visitors/Component/Ref.ts | 5 ++- src/generators/dom/visitors/EachBlock.ts | 13 +++--- .../dom/visitors/Element/Attribute.ts | 5 ++- .../dom/visitors/Element/Binding.ts | 5 ++- .../dom/visitors/Element/Element.ts | 7 +++- .../dom/visitors/Element/EventHandler.ts | 5 ++- src/generators/dom/visitors/Element/Ref.ts | 5 ++- .../dom/visitors/Element/addTransitions.ts | 5 ++- .../Element/getStaticAttributeValue.ts | 6 ++- .../dom/visitors/Element/meta/Window.ts | 9 ++-- src/generators/dom/visitors/IfBlock.ts | 19 +++++---- src/generators/dom/visitors/MustacheTag.ts | 5 ++- src/generators/dom/visitors/RawMustacheTag.ts | 5 ++- src/generators/dom/visitors/Text.ts | 6 ++- 20 files changed, 132 insertions(+), 50 deletions(-) diff --git a/src/generators/Generator.ts b/src/generators/Generator.ts index 9e6d2c0a76..ccc31d8aec 100644 --- a/src/generators/Generator.ts +++ b/src/generators/Generator.ts @@ -10,7 +10,9 @@ import getIntro from './shared/utils/getIntro'; import getOutro from './shared/utils/getOutro'; import processCss from './shared/processCss'; import annotateWithScopes from '../utils/annotateWithScopes'; -import { Node, Parsed, CompileOptions } from '../../interfaces'; +import DomBlock from './dom/Block'; +import SsrBlock from './server-side-rendering/Block'; +import { Node, Parsed, CompileOptions } from '../interfaces'; const test = typeof global !== 'undefined' && global.__svelte_test; @@ -85,7 +87,7 @@ export default class Generator { return this.aliases.get( name ); } - contextualise ( block, expression: Node, context, isEventHandler ) { + contextualise ( block: DomBlock | SsrBlock, expression: Node, context: string, isEventHandler: boolean ) { this.addSourcemapLocations( expression ); const usedContexts: string[] = []; @@ -219,7 +221,7 @@ export default class Generator { generate ( result, options, { name, format } ) { if ( this.imports.length ) { - const statements = []; + const statements: string[] = []; this.imports.forEach( ( declaration, i ) => { if ( format === 'es' ) { @@ -227,14 +229,14 @@ export default class Generator { return; } - const defaultImport = declaration.specifiers.find( x => x.type === 'ImportDefaultSpecifier' || x.type === 'ImportSpecifier' && x.imported.name === 'default' ); - const namespaceImport = declaration.specifiers.find( x => x.type === 'ImportNamespaceSpecifier' ); - const namedImports = declaration.specifiers.filter( x => x.type === 'ImportSpecifier' && x.imported.name !== 'default' ); + const defaultImport = declaration.specifiers.find( ( x: Node ) => x.type === 'ImportDefaultSpecifier' || x.type === 'ImportSpecifier' && x.imported.name === 'default' ); + const namespaceImport = declaration.specifiers.find( ( x: Node ) => x.type === 'ImportNamespaceSpecifier' ); + const namedImports = declaration.specifiers.filter( ( x: Node ) => x.type === 'ImportSpecifier' && x.imported.name !== 'default' ); const name = ( defaultImport || namespaceImport ) ? ( defaultImport || namespaceImport ).local.name : `__import${i}`; declaration.name = name; // hacky but makes life a bit easier later - namedImports.forEach( specifier => { + namedImports.forEach( ( specifier: Node ) => { statements.push( `var ${specifier.local.name} = ${name}.${specifier.imported.name}` ); }); @@ -253,7 +255,7 @@ export default class Generator { const compiled = new Bundle({ separator: '' }); - function addString ( str ) { + function addString ( str: string ) { compiled.addSource({ content: new MagicString( str ) }); @@ -273,7 +275,7 @@ export default class Generator { }); } - parts.forEach( str => { + parts.forEach( ( str: string ) => { const chunk = str.replace( pattern, '' ); if ( chunk ) addString( chunk ); diff --git a/src/generators/dom/Block.ts b/src/generators/dom/Block.ts index 0711d76344..9683cf6556 100644 --- a/src/generators/dom/Block.ts +++ b/src/generators/dom/Block.ts @@ -4,16 +4,43 @@ import { DomGenerator } from './index'; import { Node } from '../../interfaces'; export interface BlockOptions { - generator: DomGenerator - name: string - expression: Node - context: string + generator: DomGenerator; + name: string; + expression: Node; + context: string; + key: string; } export default class Block { generator: DomGenerator; name: string; expression: Node; + context: string; + key: string; + + builders: { + create: CodeBuilder; + mount: CodeBuilder; + update: CodeBuilder; + intro: CodeBuilder; + outro: CodeBuilder; + detach: CodeBuilder; + detachRaw: CodeBuilder; + destroy: CodeBuilder; + } + + hasIntroMethod: boolean; + hasOutroMethod: boolean; + outros: number; + + aliases: Map; + variables: Map; + getUniqueName: (name: string) => string; + + component: string; + target: string; + + hasUpdateMethod: boolean; constructor ( options: BlockOptions ) { this.generator = options.generator; @@ -85,7 +112,7 @@ export default class Block { } } - addVariable ( name: string, init ) { + addVariable ( name: string, init?: string ) { if ( this.variables.has( name ) && this.variables.get( name ) !== init ) { throw new Error( `Variable '${name}' already initialised with a different value` ); } @@ -101,11 +128,11 @@ export default class Block { return this.aliases.get( name ); } - child ( options ) { + child ( options: BlockOptions ) { return new Block( Object.assign( {}, this, options, { parent: this } ) ); } - contextualise ( expression, context, isEventHandler ) { + contextualise ( expression: Node, context?: string, isEventHandler?: boolean ) { return this.generator.contextualise( this, expression, context, isEventHandler ); } diff --git a/src/generators/dom/visitors/Component/Attribute.ts b/src/generators/dom/visitors/Component/Attribute.ts index 8458eeb866..0055c75e0a 100644 --- a/src/generators/dom/visitors/Component/Attribute.ts +++ b/src/generators/dom/visitors/Component/Attribute.ts @@ -1,4 +1,8 @@ -export default function visitAttribute ( generator, block, state, node, attribute, local ) { +import { DomGenerator } from '../../index'; +import Block from '../../Block'; +import { Node } from '../../../../interfaces'; + +export default function visitAttribute ( generator: DomGenerator, block: Block, state, node: Node, attribute, local ) { if ( attribute.value === true ) { // attributes without values, e.g.