Skip to content

Commit

Permalink
Merge pull request #2838 from zxbodya/improve-typings
Browse files Browse the repository at this point in the history
Improve typings
  • Loading branch information
Rich-Harris committed May 26, 2019
2 parents bebed18 + 3a4bfe4 commit ca8c856
Show file tree
Hide file tree
Showing 69 changed files with 408 additions and 199 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.DS_Store
.nyc_output
node_modules
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"pretest": "npm run build",
"posttest": "agadoo src/internal/index.js",
"prepublishOnly": "export PUBLISH=true && npm run lint && npm test",
"tsd": "tsc -d src/store.ts --outDir ."
"tsd": "tsc -d src/store.ts --outDir .",
"typecheck": "tsc --noEmit"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -84,7 +85,7 @@
"tiny-glob": "^0.2.1",
"ts-node": "^8.0.2",
"tslib": "^1.8.0",
"typescript": "^3.0.1"
"typescript": "^3.4.0"
},
"nyc": {
"include": [
Expand Down
10 changes: 6 additions & 4 deletions src/compile/Component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import MagicString, { Bundle } from 'magic-string';
// @ts-ignore
import { walk, childKeys } from 'estree-walker';
import { getLocator } from 'locate-character';
import Stats from '../Stats';
Expand All @@ -21,6 +22,7 @@ import { remove_indentation, add_indentation } from '../utils/indentation';
import get_object from './utils/get_object';
import unwrap_parens from './utils/unwrap_parens';
import Slot from './nodes/Slot';
import { Node as ESTreeNode } from 'estree';

type ComponentOptions = {
namespace?: string;
Expand Down Expand Up @@ -758,7 +760,7 @@ export default class Component {
});
}

if (is_reference(node, parent)) {
if (is_reference(node as ESTreeNode, parent as ESTreeNode)) {
const object = get_object(node);
const { name } = object;

Expand All @@ -776,7 +778,7 @@ export default class Component {
});
}

invalidate(name, value) {
invalidate(name, value?) {
const variable = this.var_lookup.get(name);

if (variable && (variable.subscribable && variable.reassigned)) {
Expand Down Expand Up @@ -1022,7 +1024,7 @@ export default class Component {
scope = map.get(node);
}

if (is_reference(node, parent)) {
if (is_reference(node as ESTreeNode, parent as ESTreeNode)) {
const { name } = flatten_reference(node);
const owner = scope.find_owner(name);

Expand Down Expand Up @@ -1113,7 +1115,7 @@ export default class Component {
} else if (node.type === 'UpdateExpression') {
const identifier = get_object(node.argument);
assignees.add(identifier.name);
} else if (is_reference(node, parent)) {
} else if (is_reference(node as ESTreeNode, parent as ESTreeNode)) {
const identifier = get_object(node);
if (!assignee_nodes.has(identifier)) {
const { name } = identifier;
Expand Down
4 changes: 2 additions & 2 deletions src/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function validate_options(options: CompileOptions, warnings: Warning[]) {
}
}

function get_name(filename) {
function get_name(filename: string) {
if (!filename) return null;
const parts = filename.split(/[\/\\]/);

Expand Down Expand Up @@ -105,4 +105,4 @@ export default function compile(source: string, options: CompileOptions = {}) {
: render_dom(component, options);

return component.generate(js);
}
}
9 changes: 7 additions & 2 deletions src/compile/nodes/Attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default class Attribute extends Node {

this.should_cache = this.is_dynamic
? this.chunks.length === 1
// @ts-ignore todo: probably error
? this.chunks[0].node.type !== 'Identifier' || scope.names.has(this.chunks[0].node.name)
: true
: false;
Expand All @@ -91,8 +92,10 @@ export default class Attribute extends Node {
if (this.chunks.length === 0) return `""`;

if (this.chunks.length === 1) {

return this.chunks[0].type === 'Text'
? stringify(this.chunks[0].data)
? stringify((this.chunks[0] as Text).data)
// @ts-ignore todo: probably error
: this.chunks[0].render(block);
}

Expand All @@ -102,6 +105,7 @@ export default class Attribute extends Node {
if (chunk.type === 'Text') {
return stringify(chunk.data);
} else {
// @ts-ignore todo: probably error
return chunk.get_precedence() <= 13 ? `(${chunk.render()})` : chunk.render();
}
})
Expand All @@ -114,7 +118,8 @@ export default class Attribute extends Node {
return this.is_true
? true
: this.chunks[0]
? this.chunks[0].data
// method should be called only when `is_static = true`
? (this.chunks[0] as Text).data
: '';
}
}
1 change: 1 addition & 0 deletions src/compile/nodes/AwaitBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CatchBlock from './CatchBlock';
import Expression from './shared/Expression';

export default class AwaitBlock extends Node {
type: 'AwaitBlock';
expression: Expression;
value: string;
error: string;
Expand Down
1 change: 1 addition & 0 deletions src/compile/nodes/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Component from '../Component';
import TemplateScope from './shared/TemplateScope';

export default class Binding extends Node {
type: 'Binding';
name: string;
expression: Expression;
is_contextual: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/compile/nodes/CatchBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import TemplateScope from './shared/TemplateScope';
import AbstractBlock from './shared/AbstractBlock';

export default class CatchBlock extends AbstractBlock {
type: 'CatchBlock';
scope: TemplateScope;

constructor(component, parent, scope, info) {
Expand Down
3 changes: 2 additions & 1 deletion src/compile/nodes/DebugTag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Node from './shared/Node';
import Expression from './shared/Expression';

export default class DebugTag extends Node {
type: 'DebugTag';
expressions: Expression[];

constructor(component, parent, scope, info) {
Expand All @@ -11,4 +12,4 @@ export default class DebugTag extends Node {
return new Expression(component, parent, scope, node);
});
}
}
}
17 changes: 12 additions & 5 deletions src/compile/nodes/EachBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ import TemplateScope from './shared/TemplateScope';
import AbstractBlock from './shared/AbstractBlock';
import { Node as INode } from '../../interfaces';
import { new_tail } from '../utils/tail';
import Element from './Element';

function unpack_destructuring(contexts: Array<{ name: string, tail: string }>, node: INode, tail: string) {
type Context = {
key: INode,
name?: string,
tail: string
};

function unpack_destructuring(contexts: Array<Context>, node: INode, tail: string) {
if (!node) return;

if (node.type === 'Identifier' || node.type === 'RestIdentifier') {
Expand Down Expand Up @@ -53,7 +60,7 @@ export default class EachBlock extends AbstractBlock {
context: string;
key: Expression;
scope: TemplateScope;
contexts: Array<{ name: string, tail: string }>;
contexts: Array<Context>;
has_animation: boolean;
has_binding = false;

Expand Down Expand Up @@ -82,7 +89,7 @@ export default class EachBlock extends AbstractBlock {

if (this.index) {
// index can only change if this is a keyed each block
const dependencies = this.key ? this.expression.dependencies : [];
const dependencies = this.key ? this.expression.dependencies : new Set([]);
this.scope.add(this.index, dependencies, this);
}

Expand All @@ -92,8 +99,8 @@ export default class EachBlock extends AbstractBlock {

if (this.has_animation) {
if (this.children.length !== 1) {
const child = this.children.find(child => !!child.animation);
component.error(child.animation, {
const child = this.children.find(child => !!(child as Element).animation);
component.error((child as Element).animation, {
code: `invalid-animation`,
message: `An element that use the animate directive must be the sole child of a keyed each block`
});
Expand Down
12 changes: 8 additions & 4 deletions src/compile/nodes/Element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import fuzzymatch from '../../utils/fuzzymatch';
import list from '../../utils/list';
import Let from './Let';
import TemplateScope from './shared/TemplateScope';
import { INode } from './interfaces';

const svg = /^(?:altGlyph|altGlyphDef|altGlyphItem|animate|animateColor|animateMotion|animateTransform|circle|clipPath|color-profile|cursor|defs|desc|discard|ellipse|feBlend|feColorMatrix|feComponentTransfer|feComposite|feConvolveMatrix|feDiffuseLighting|feDisplacementMap|feDistantLight|feDropShadow|feFlood|feFuncA|feFuncB|feFuncG|feFuncR|feGaussianBlur|feImage|feMerge|feMergeNode|feMorphology|feOffset|fePointLight|feSpecularLighting|feSpotLight|feTile|feTurbulence|filter|font|font-face|font-face-format|font-face-name|font-face-src|font-face-uri|foreignObject|g|glyph|glyphRef|hatch|hatchpath|hkern|image|line|linearGradient|marker|mask|mesh|meshgradient|meshpatch|meshrow|metadata|missing-glyph|mpath|path|pattern|polygon|polyline|radialGradient|rect|set|solidcolor|stop|svg|switch|symbol|text|textPath|tref|tspan|unknown|use|view|vkern)$/;

Expand Down Expand Up @@ -101,7 +102,7 @@ export default class Element extends Node {
intro?: Transition = null;
outro?: Transition = null;
animation?: Animation = null;
children: Node[];
children: INode[];
namespace: string;

constructor(component, parent, scope, info: any) {
Expand Down Expand Up @@ -136,7 +137,7 @@ export default class Element extends Node {
// Special case — treat these the same way:
// <option>{foo}</option>
// <option value={foo}>{foo}</option>
const value_attribute = info.attributes.find((attribute: Node) => attribute.name === 'value');
const value_attribute = info.attributes.find(attribute => attribute.name === 'value');

if (!value_attribute) {
info.attributes.push({
Expand Down Expand Up @@ -228,7 +229,7 @@ export default class Element extends Node {
let is_figure_parent = false;

while (parent) {
if (parent.name === 'figure') {
if ((parent as Element).name === 'figure') {
is_figure_parent = true;
break;
}
Expand All @@ -253,7 +254,7 @@ export default class Element extends Node {
return true;
});

const index = children.findIndex(child => child.name === 'figcaption');
const index = children.findIndex(child => (child as Element).name === 'figcaption');

if (index !== -1 && (index !== 0 && index !== children.length - 1)) {
this.component.warn(children[index], {
Expand Down Expand Up @@ -320,7 +321,9 @@ export default class Element extends Node {
}

const value = attribute.get_static_value();
// @ts-ignore
if (value && !aria_role_set.has(value)) {
// @ts-ignore
const match = fuzzymatch(value, aria_roles);
let message = `A11y: Unknown role '${value}'`;
if (match) message += ` (did you mean '${match}'?)`;
Expand Down Expand Up @@ -359,6 +362,7 @@ export default class Element extends Node {
// tabindex-no-positive
if (name === 'tabindex') {
const value = attribute.get_static_value();
// @ts-ignore todo is tabindex=true correct case?
if (!isNaN(value) && +value > 0) {
component.warn(attribute, {
code: `a11y-positive-tabindex`,
Expand Down
3 changes: 2 additions & 1 deletion src/compile/nodes/ElseBlock.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import map_children from './shared/map_children';
import AbstractBlock from './shared/AbstractBlock';
import Component from '../Component';

export default class ElseBlock extends AbstractBlock {
type: 'ElseBlock';

constructor(component, parent, scope, info) {
constructor(component: Component, parent, scope, info) {
super(component, parent, scope, info);
this.children = map_children(component, this, scope, info.children);

Expand Down
3 changes: 2 additions & 1 deletion src/compile/nodes/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import deindent from '../utils/deindent';
import Block from '../render-dom/Block';

export default class EventHandler extends Node {
type: 'EventHandler';
name: string;
modifiers: Set<string>;
expression: Expression;
Expand Down Expand Up @@ -65,4 +66,4 @@ export default class EventHandler extends Node {
// this.component.add_reference(this.handler_name);
return `ctx.${this.handler_name}`;
}
}
}
6 changes: 4 additions & 2 deletions src/compile/nodes/Fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import Component from '../Component';
import map_children from './shared/map_children';
import Block from '../render-dom/Block';
import TemplateScope from './shared/TemplateScope';
import { INode } from './interfaces';

export default class Fragment extends Node {
type: 'Fragment';
block: Block;
children: Node[];
children: INode[];
scope: TemplateScope;

constructor(component: Component, info: any) {
Expand All @@ -16,4 +18,4 @@ export default class Fragment extends Node {
this.scope = scope;
this.children = map_children(component, this, scope, info.children);
}
}
}
1 change: 0 additions & 1 deletion src/compile/nodes/Head.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Node from './shared/Node';
import Block from '../render-dom/Block';
import map_children from './shared/map_children';

export default class Head extends Node {
Expand Down
3 changes: 2 additions & 1 deletion src/compile/nodes/InlineComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Expression from './shared/Expression';
import Component from '../Component';
import Let from './Let';
import TemplateScope from './shared/TemplateScope';
import { INode } from './interfaces';

export default class InlineComponent extends Node {
type: 'InlineComponent';
Expand All @@ -16,7 +17,7 @@ export default class InlineComponent extends Node {
bindings: Binding[] = [];
handlers: EventHandler[] = [];
lets: Let[] = [];
children: Node[];
children: INode[];
scope: TemplateScope;

constructor(component: Component, parent, scope, info) {
Expand Down
4 changes: 3 additions & 1 deletion src/compile/nodes/MustacheTag.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Tag from './shared/Tag';

export default class MustacheTag extends Tag {}
export default class MustacheTag extends Tag {
type: 'MustacheTag';
}
2 changes: 1 addition & 1 deletion src/compile/nodes/PendingBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import map_children from './shared/map_children';
import AbstractBlock from './shared/AbstractBlock';

export default class PendingBlock extends AbstractBlock {

type: 'PendingBlock';
constructor(component, parent, scope, info) {
super(component, parent, scope, info);
this.children = map_children(component, parent, scope, info.children);
Expand Down
4 changes: 3 additions & 1 deletion src/compile/nodes/RawMustacheTag.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Tag from './shared/Tag';

export default class RawMustacheTag extends Tag {}
export default class RawMustacheTag extends Tag {
type: 'RawMustacheTag'
}
Loading

0 comments on commit ca8c856

Please sign in to comment.