diff --git a/src/interfaces.ts b/src/interfaces.ts index 68fef7472e..90ee60d1be 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -1,10 +1,51 @@ -export interface Node { +interface BaseNode { start: number; end: number; type: string; + children?: Node[]; [prop_name: string]: any; } +export interface Text extends BaseNode { + type: 'Text', + data: string; +} + +export interface MustacheTag extends BaseNode { + type: 'MustacheTag', + expresion: Node; +} + +export type DirectiveType = 'Action' + | 'Animation' + | 'Binding' + | 'Class' + | 'EventHandler' + | 'Let' + | 'Ref' + | 'Transition'; + +interface BaseDirective extends BaseNode { + type: DirectiveType; + expression: null|Node; + name: string; + modifiers: string[] +} + +export interface Transition extends BaseDirective{ + type: 'Transition', + intro: boolean; + outro: boolean; +} + +export type Directive = BaseDirective | Transition; + +export type Node = Text + | MustacheTag + | BaseNode + | Directive + | Transition; + export interface Parser { readonly template: string; readonly filename?: string; @@ -92,4 +133,4 @@ export interface Var { initialised?: boolean; hoistable?: boolean; subscribable?: boolean; -} \ No newline at end of file +} diff --git a/src/parse/read/expression.ts b/src/parse/read/expression.ts index 4d1de89fe7..615f270112 100644 --- a/src/parse/read/expression.ts +++ b/src/parse/read/expression.ts @@ -1,9 +1,10 @@ import { parse_expression_at } from '../acorn'; import { Parser } from '../index'; +import { Identifier, Node, SimpleLiteral } from 'estree'; const literals = new Map([['true', true], ['false', false], ['null', null]]); -export default function read_expression(parser: Parser) { +export default function read_expression(parser: Parser): Node { const start = parser.index; const name = parser.read_until(/\s*}/); @@ -17,7 +18,7 @@ export default function read_expression(parser: Parser) { end, value: literals.get(name), raw: name, - }; + } as SimpleLiteral; } return { @@ -25,7 +26,7 @@ export default function read_expression(parser: Parser) { start, end: start + name.length, name, - }; + } as Identifier; } parser.index = start; @@ -34,7 +35,7 @@ export default function read_expression(parser: Parser) { const node = parse_expression_at(parser.template, parser.index); parser.index = node.end; - return node; + return node as Node; } catch (err) { parser.acorn_error(err); } diff --git a/src/parse/state/tag.ts b/src/parse/state/tag.ts index 56195549d8..f7f5a93574 100644 --- a/src/parse/state/tag.ts +++ b/src/parse/state/tag.ts @@ -4,7 +4,7 @@ import read_style from '../read/style'; import { decode_character_references } from '../utils/html'; import { is_void } from '../../utils/names'; import { Parser } from '../index'; -import { Node } from '../../interfaces'; +import { Directive, DirectiveType, Node, Text } from '../../interfaces'; import fuzzymatch from '../../utils/fuzzymatch'; import list from '../../utils/list'; @@ -401,7 +401,7 @@ function read_attribute(parser: Parser, unique_names: Set) { } if (value[0]) { - if (value.length > 1 || value[0].type === 'Text') { + if ((value as Array).length > 1 || value[0].type === 'Text') { parser.error({ code: `invalid-directive-value`, message: `Directive value must be a JavaScript expression enclosed in curly braces` @@ -409,7 +409,7 @@ function read_attribute(parser: Parser, unique_names: Set) { } } - const directive = { + const directive: Directive = { start, end, type, @@ -445,7 +445,7 @@ function read_attribute(parser: Parser, unique_names: Set) { }; } -function get_directive_type(name) { +function get_directive_type(name: string):DirectiveType { if (name === 'use') return 'Action'; if (name === 'animate') return 'Animation'; if (name === 'bind') return 'Binding'; @@ -471,15 +471,15 @@ function read_attribute_value(parser: Parser) { return value; } -function read_sequence(parser: Parser, done: () => boolean) { - let current_chunk: Node = { +function read_sequence(parser: Parser, done: () => boolean): Node[] { + let current_chunk: Text = { start: parser.index, end: null, type: 'Text', data: '', }; - const chunks = []; + const chunks: Node[] = []; while (parser.index < parser.template.length) { const index = parser.index;