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; start: number;
end: number; end: number;
type: string; type: string;
children?: Node[];
[prop_name: string]: any; [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 { export interface Parser {
readonly template: string; readonly template: string;
readonly filename?: string; readonly filename?: string;

@ -1,9 +1,10 @@
import { parse_expression_at } from '../acorn'; import { parse_expression_at } from '../acorn';
import { Parser } from '../index'; import { Parser } from '../index';
import { Identifier, Node, SimpleLiteral } from 'estree';
const literals = new Map([['true', true], ['false', false], ['null', null]]); 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 start = parser.index;
const name = parser.read_until(/\s*}/); const name = parser.read_until(/\s*}/);
@ -17,7 +18,7 @@ export default function read_expression(parser: Parser) {
end, end,
value: literals.get(name), value: literals.get(name),
raw: name, raw: name,
}; } as SimpleLiteral;
} }
return { return {
@ -25,7 +26,7 @@ export default function read_expression(parser: Parser) {
start, start,
end: start + name.length, end: start + name.length,
name, name,
}; } as Identifier;
} }
parser.index = start; parser.index = start;
@ -34,7 +35,7 @@ export default function read_expression(parser: Parser) {
const node = parse_expression_at(parser.template, parser.index); const node = parse_expression_at(parser.template, parser.index);
parser.index = node.end; parser.index = node.end;
return node; return node as Node;
} catch (err) { } catch (err) {
parser.acorn_error(err); parser.acorn_error(err);
} }

@ -4,7 +4,7 @@ import read_style from '../read/style';
import { decode_character_references } from '../utils/html'; import { decode_character_references } from '../utils/html';
import { is_void } from '../../utils/names'; import { is_void } from '../../utils/names';
import { Parser } from '../index'; import { Parser } from '../index';
import { Node } from '../../interfaces'; import { Directive, DirectiveType, Node, Text } from '../../interfaces';
import fuzzymatch from '../../utils/fuzzymatch'; import fuzzymatch from '../../utils/fuzzymatch';
import list from '../../utils/list'; import list from '../../utils/list';
@ -401,7 +401,7 @@ function read_attribute(parser: Parser, unique_names: Set<string>) {
} }
if (value[0]) { if (value[0]) {
if (value.length > 1 || value[0].type === 'Text') { if ((value as Array<any>).length > 1 || value[0].type === 'Text') {
parser.error({ parser.error({
code: `invalid-directive-value`, code: `invalid-directive-value`,
message: `Directive value must be a JavaScript expression enclosed in curly braces` 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, start,
end, end,
type, 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 === 'use') return 'Action';
if (name === 'animate') return 'Animation'; if (name === 'animate') return 'Animation';
if (name === 'bind') return 'Binding'; if (name === 'bind') return 'Binding';
@ -471,15 +471,15 @@ function read_attribute_value(parser: Parser) {
return value; return value;
} }
function read_sequence(parser: Parser, done: () => boolean) { function read_sequence(parser: Parser, done: () => boolean): Node[] {
let current_chunk: Node = { let current_chunk: Text = {
start: parser.index, start: parser.index,
end: null, end: null,
type: 'Text', type: 'Text',
data: '', data: '',
}; };
const chunks = []; const chunks: Node[] = [];
while (parser.index < parser.template.length) { while (parser.index < parser.template.length) {
const index = parser.index; const index = parser.index;

Loading…
Cancel
Save