more TS fixes

pull/736/head
Rich Harris 7 years ago
parent bd9c59aa5a
commit aa59dafb81

@ -16,6 +16,7 @@ import DomBlock from './dom/Block';
import SsrBlock from './server-side-rendering/Block'; import SsrBlock from './server-side-rendering/Block';
import Stylesheet from '../css/Stylesheet'; import Stylesheet from '../css/Stylesheet';
import { Node, GenerateOptions, Parsed, CompileOptions } from '../interfaces'; import { Node, GenerateOptions, Parsed, CompileOptions } from '../interfaces';
import { Computation, TemplateProperties } from './interfaces';
const test = typeof global !== 'undefined' && global.__svelte_test; const test = typeof global !== 'undefined' && global.__svelte_test;
@ -26,12 +27,18 @@ export default class Generator {
name: string; name: string;
options: CompileOptions; options: CompileOptions;
defaultExport: Node[];
imports: Node[]; imports: Node[];
helpers: Set<string>; helpers: Set<string>;
components: Set<string>; components: Set<string>;
events: Set<string>; events: Set<string>;
transitions: Set<string>; transitions: Set<string>;
importedComponents: Map<string, string>; importedComponents: Map<string, string>;
namespace: string;
hasComponents: boolean;
hasJs: boolean;
computations: Computation[];
templateProperties: TemplateProperties;
code: MagicString; code: MagicString;
@ -379,9 +386,9 @@ export default class Generator {
return alias; return alias;
} }
getUniqueNameMaker(params) { getUniqueNameMaker(params: string[]) {
const localUsedNames = new Set(params); const localUsedNames = new Set(params);
return name => { return (name: string) => {
if (test) name = `${name}$`; if (test) name = `${name}$`;
let alias = name; let alias = name;
for ( for (
@ -402,8 +409,8 @@ export default class Generator {
const { js } = this.parsed; const { js } = this.parsed;
const imports = this.imports; const imports = this.imports;
const computations = []; const computations: Computation[] = [];
const templateProperties = {}; const templateProperties: TemplateProperties = {};
let namespace = null; let namespace = null;
let hasJs = !!js; let hasJs = !!js;
@ -459,7 +466,7 @@ export default class Generator {
const visited = new Set(); const visited = new Set();
function visit(key) { function visit(key: string) {
if (!dependencies.has(key)) return; // not a computation if (!dependencies.has(key)) return; // not a computation
if (visited.has(key)) return; if (visited.has(key)) return;

@ -317,7 +317,7 @@ export default function dom(
let scope = annotateWithScopes(expression); let scope = annotateWithScopes(expression);
walk(expression, { walk(expression, {
enter(node, parent) { enter(node: Node, parent: Node) {
if (node._scope) scope = node._scope; if (node._scope) scope = node._scope;
if ( if (
@ -337,7 +337,7 @@ export default function dom(
} }
}, },
leave(node) { leave(node: Node) {
if (node._scope) scope = scope.parent; if (node._scope) scope = scope.parent;
}, },
}); });

@ -1,5 +1,5 @@
export interface State { export interface State {
name: string; name?: string;
namespace: string; namespace: string;
parentNode: string; parentNode: string;
parentNodes: string; parentNodes: string;

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

@ -34,8 +34,10 @@ export interface Warning {
toString: () => string; toString: () => string;
} }
export type ModuleFormat = 'es' | 'amd' | 'cjs' | 'iife' | 'umd' | 'eval';
export interface CompileOptions { export interface CompileOptions {
format?: string; format?: ModuleFormat;
name?: string; name?: string;
filename?: string; filename?: string;
generate?: string; generate?: string;
@ -56,8 +58,6 @@ export interface CompileOptions {
onwarn?: (warning: Warning) => void; onwarn?: (warning: Warning) => void;
} }
export type ModuleFormat = 'es' | 'amd' | 'cjs' | 'iife' | 'umd' | 'eval';
export interface GenerateOptions { export interface GenerateOptions {
name: string; name: string;
format: ModuleFormat; format: ModuleFormat;

@ -2,7 +2,7 @@ import { parseExpressionAt } from 'acorn';
import spaces from '../../utils/spaces'; import spaces from '../../utils/spaces';
import { Parser } from '../index'; import { Parser } from '../index';
function readExpression(parser: Parser, start: number, quoteMark) { function readExpression(parser: Parser, start: number, quoteMark: string|null) {
let str = ''; let str = '';
let escaped = false; let escaped = false;

@ -1,10 +1,11 @@
import { parse } from 'acorn'; import { parse } from 'acorn';
import spaces from '../../utils/spaces'; import spaces from '../../utils/spaces';
import { Parser } from '../index'; import { Parser } from '../index';
import { Node } from '../../interfaces';
const scriptClosingTag = '</script>'; const scriptClosingTag = '</script>';
export default function readScript(parser: Parser, start: number, attributes) { export default function readScript(parser: Parser, start: number, attributes: Node[]) {
const scriptStart = parser.index; const scriptStart = parser.index;
const scriptEnd = parser.template.indexOf(scriptClosingTag, scriptStart); const scriptEnd = parser.template.indexOf(scriptClosingTag, scriptStart);

@ -1,8 +1,9 @@
import parse from 'css-tree/lib/parser/index.js'; import parse from 'css-tree/lib/parser/index.js';
import walk from 'css-tree/lib/utils/walk.js'; import walk from 'css-tree/lib/utils/walk.js';
import { Parser } from '../index'; 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 contentStart = parser.index;
const styles = parser.readUntil(/<\/style>/); const styles = parser.readUntil(/<\/style>/);
const contentEnd = parser.index; const contentEnd = parser.index;
@ -23,7 +24,7 @@ export default function readStyle(parser: Parser, start: number, attributes) {
} }
// tidy up AST // tidy up AST
walk.all(ast, node => { walk.all(ast, (node: Node) => {
if (node.loc) { if (node.loc) {
node.start = node.loc.start.offset; node.start = node.loc.start.offset;
node.end = node.loc.end.offset; node.end = node.loc.end.offset;

@ -6,7 +6,7 @@ import { Node } from '../../interfaces';
const validIdentifier = /[a-zA-Z_$][a-zA-Z0-9_$]*/; 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 firstChild = block.children[0];
const lastChild = block.children[block.children.length - 1]; const lastChild = block.children[block.children.length - 1];
@ -220,6 +220,4 @@ export default function mustache(parser: Parser) {
expression, expression,
}); });
} }
return null;
} }

@ -78,7 +78,7 @@ export default function tag(parser: Parser) {
data, data,
}); });
return null; return;
} }
const isClosingTag = parser.eat('/'); const isClosingTag = parser.eat('/');
@ -133,7 +133,7 @@ export default function tag(parser: Parser) {
parent.end = parser.index; parent.end = parser.index;
parser.stack.pop(); parser.stack.pop();
return null; return;
} else if (disallowedContents.has(parent.name)) { } else if (disallowedContents.has(parent.name)) {
// can this be a child of the parent element, or does it implicitly // can this be a child of the parent element, or does it implicitly
// close it, like `<li>one<li>two`? // close it, like `<li>one<li>two`?
@ -200,8 +200,6 @@ export default function tag(parser: Parser) {
// don't push self-closing elements onto the stack // don't push self-closing elements onto the stack
parser.stack.push(element); parser.stack.push(element);
} }
return null;
} }
function readTagName(parser: Parser) { function readTagName(parser: Parser) {
@ -242,7 +240,7 @@ function readTagName(parser: Parser) {
return name; return name;
} }
function readAttribute(parser: Parser, uniqueNames) { function readAttribute(parser: Parser, uniqueNames: Set<string>) {
const start = parser.index; const start = parser.index;
let name = parser.readUntil(/(\s|=|\/|>)/); let name = parser.readUntil(/(\s|=|\/|>)/);

@ -20,6 +20,4 @@ export default function text(parser: Parser) {
type: 'Text', type: 'Text',
data: decodeCharacterReferences(data), data: decodeCharacterReferences(data),
}); });
return null;
} }

Loading…
Cancel
Save