improve parser typings

pull/2838/head
Bogdan Savluk 6 years ago
parent b1a24c853b
commit 6fdaa803c7

@ -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;
}
}

@ -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);
}

@ -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<string>) {
}
if (value[0]) {
if (value.length > 1 || value[0].type === 'Text') {
if ((value as Array<any>).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<string>) {
}
}
const directive = {
const directive: Directive = {
start,
end,
type,
@ -445,7 +445,7 @@ function read_attribute(parser: Parser, unique_names: Set<string>) {
};
}
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;

Loading…
Cancel
Save