Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Finished] TypeScript conversion #1806

Merged
merged 35 commits into from
Dec 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
665f98c
lay foundations for TypeScript conversion
Rich-Harris Aug 21, 2017
0c0edee
initial naive typscript conversion
guybedford Dec 19, 2017
aa961e5
deep dive into ast folder!
guybedford Dec 19, 2017
b461965
ast folder types, complete estree definitions
guybedford Dec 20, 2017
f6cd52a
finish porting Rich Harris branch
guybedford Dec 20, 2017
fb3a34c
moar types
guybedford Dec 20, 2017
63d38d4
Add editorconfig file
lukastaegert Dec 20, 2017
f4619a0
Add some types for Module.ts (WIP)
lukastaegert Dec 20, 2017
4b448e5
ast type fixing
guybedford Dec 20, 2017
b561a63
More types for Module.ts (WIP)
lukastaegert Dec 20, 2017
3e4519d
add bin, browser, utils, initial watch types
guybedford Dec 20, 2017
c5da226
attempt finalisers, scopes, variables
guybedford Dec 20, 2017
9891e4a
Make JSDoc a little happier
lukastaegert Dec 21, 2017
b2210b9
finish up bin types, attempt further scopes and variables
guybedford Dec 21, 2017
70e565b
More Module typing
lukastaegert Dec 21, 2017
f36de4c
Finish typing Module.js
lukastaegert Dec 21, 2017
2a9db67
Mark private methods and variables
lukastaegert Dec 21, 2017
2d1b871
Type ExternalModule
lukastaegert Dec 21, 2017
58b92d9
Type Bundle (WIP)
lukastaegert Dec 21, 2017
265d646
complete types on bin, finalisers
guybedford Dec 21, 2017
cf49dc9
further attempts on Bundle
guybedford Dec 21, 2017
e1615cf
type tweaks
guybedford Dec 21, 2017
e71c9ec
module, bundle, finalisers polishing
guybedford Dec 21, 2017
f10abfb
A little more work on Bundle
lukastaegert Dec 21, 2017
26ba833
Type transformBundle
lukastaegert Dec 21, 2017
8079970
options refactor, bin and watch types complete
guybedford Dec 22, 2017
2d7f38e
more progress on variables and scopes
guybedford Dec 22, 2017
2d61040
node, variable polishes
guybedford Dec 22, 2017
aa5a63e
mergeOptions rename
guybedford Dec 22, 2017
bea8b7e
options types
guybedford Dec 22, 2017
ba25afe
Unify forEach callback signatures + more types
lukastaegert Dec 22, 2017
e4d1e38
Use UNKNOWN_ASSIGNMENT instead of UNDEFINED_ASSIGNMENT as it is basic…
lukastaegert Dec 22, 2017
39cce93
Finish typing!
lukastaegert Dec 22, 2017
3a70b47
remove callable expression abstraction, was only necessary to make ty…
guybedford Dec 23, 2017
ad8484b
downlevel iteration not necessary
guybedford Dec 23, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import ImportNamespaceSpecifier from './ast/nodes/ImportNamespaceSpecifier';
import { RollupWarning } from './rollup/index';
import ExternalModule from './ExternalModule';
import Import from './ast/nodes/Import';
import TemplateLiteral from './ast/nodes/TemplateLiteral';
import Literal from './ast/nodes/Literal';

const setModuleDynamicImportsReturnBinding = wrapDynamicImportPlugin(acorn);

Expand Down Expand Up @@ -494,15 +496,15 @@ export default class Module {

processDynamicImports (resolveDynamicImport: ResolveDynamicImportHandler) {
return Promise.all(this.dynamicImports.map(node => {
const importArgument = node.parent.arguments[0];
const importArgument = <Node> node.parent.arguments[0];
let dynamicImportSpecifier: string | Node;
if (importArgument.type === 'TemplateLiteral') {
if (importArgument.expressions.length === 0 && importArgument.quasis.length === 1) {
dynamicImportSpecifier = importArgument.quasis[0].value.cooked;
if ((<TemplateLiteral> importArgument).expressions.length === 0 && (<TemplateLiteral> importArgument).quasis.length === 1) {
dynamicImportSpecifier = (<TemplateLiteral> importArgument).quasis[0].value.cooked;
}
} else if (importArgument.type === 'Literal') {
if (typeof importArgument.value === 'string') {
dynamicImportSpecifier = importArgument.value;
if (typeof (<Literal> importArgument).value === 'string') {
dynamicImportSpecifier = <string | Node>(<Literal> importArgument).value;
}
} else {
dynamicImportSpecifier = importArgument;
Expand Down
14 changes: 7 additions & 7 deletions src/ast/ExecutionPathOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export default class ExecutionPathOptions {
* @param {Node} node
* @return {ExecutionPathOptions}
*/
addAccessedNodeAtPath (path: string[], node: Node | UnknownAssignment) {
addAccessedNodeAtPath (path: ObjectPath, node: Node | UnknownAssignment) {
return this.setIn([OPTION_ACCESSED_NODES, node, ...path, RESULT_KEY], true);
}

Expand All @@ -93,7 +93,7 @@ export default class ExecutionPathOptions {
* @param {CallExpression|Property} callExpression
* @return {ExecutionPathOptions}
*/
addAccessedReturnExpressionAtPath (path: string[], callExpression: CallExpression | Property) {
addAccessedReturnExpressionAtPath (path: ObjectPath, callExpression: CallExpression | Property) {
return this.setIn(
[
OPTION_RETURN_EXPRESSIONS_ACCESSED_AT_PATH,
Expand All @@ -110,7 +110,7 @@ export default class ExecutionPathOptions {
* @param {Node} node
* @return {ExecutionPathOptions}
*/
addAssignedNodeAtPath (path: string[], node: Node | Variable) {
addAssignedNodeAtPath (path: ObjectPath, node: Node | Variable | UnknownAssignment) {
return this.setIn([OPTION_ASSIGNED_NODES, node, ...path, RESULT_KEY], true);
}

Expand All @@ -119,7 +119,7 @@ export default class ExecutionPathOptions {
* @param {CallExpression|Property} callExpression
* @return {ExecutionPathOptions}
*/
addAssignedReturnExpressionAtPath (path: string[], callExpression: CallExpression | Property) {
addAssignedReturnExpressionAtPath (path: ObjectPath, callExpression: CallExpression | Property) {
return this.setIn(
[
OPTION_RETURN_EXPRESSIONS_ASSIGNED_AT_PATH,
Expand All @@ -137,7 +137,7 @@ export default class ExecutionPathOptions {
* @param {CallOptions} callOptions
* @return {ExecutionPathOptions}
*/
addCalledNodeAtPathWithOptions (path: string[], node: Node | UnknownAssignment, callOptions: CallOptions) {
addCalledNodeAtPathWithOptions (path: ObjectPath, node: Node | UnknownAssignment, callOptions: CallOptions) {
return this.setIn(
[
OPTION_NODES_CALLED_AT_PATH_WITH_OPTIONS,
Expand All @@ -155,7 +155,7 @@ export default class ExecutionPathOptions {
* @param {CallExpression|Property} callExpression
* @return {ExecutionPathOptions}
*/
addCalledReturnExpressionAtPath (path: string[], callExpression: CallExpression | Property) {
addCalledReturnExpressionAtPath (path: ObjectPath, callExpression: CallExpression | Property) {
return this.setIn(
[
OPTION_RETURN_EXPRESSIONS_CALLED_AT_PATH,
Expand Down Expand Up @@ -196,7 +196,7 @@ export default class ExecutionPathOptions {
* @param {Node} node
* @return {boolean}
*/
hasNodeBeenAccessedAtPath (path: string[], node: Node | Variable | UnknownAssignment): boolean {
hasNodeBeenAccessedAtPath (path: ObjectPath, node: Node | Variable | UnknownAssignment): boolean {
return this._optionValues.getIn([
OPTION_ACCESSED_NODES,
node,
Expand Down
15 changes: 8 additions & 7 deletions src/ast/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import MagicString from 'magic-string';
import CallOptions from './CallOptions';
import Expression from './nodes/Expression';
import Declaration from './nodes/Declaration';
import { ObjectPath } from './variables/VariableReassignmentTracker';

export type ForEachReturnExpressionCallback = (options: ExecutionPathOptions) => (node: Node) => void
export type ForEachReturnExpressionCallback = (options: ExecutionPathOptions) => (node: Node | UnknownAssignment) => void

export default class Node {
type: string;
Expand Down Expand Up @@ -51,7 +52,7 @@ export default class Node {
* @param {String[]} _path
* @param {ExecutionPathOptions} _options
*/
reassignPath (_path: string[], _options: ExecutionPathOptions) { }
reassignPath (_path: ObjectPath, _options: ExecutionPathOptions) { }

/**
* Override to control on which children "bind" is called.
Expand Down Expand Up @@ -87,7 +88,7 @@ export default class Node {
* @param {ExecutionPathOptions} _options
*/
forEachReturnExpressionWhenCalledAtPath (
_path: string[],
_path: ObjectPath,
_callOptions: CallOptions,
_callback: ForEachReturnExpressionCallback,
_options: ExecutionPathOptions
Expand All @@ -114,7 +115,7 @@ export default class Node {
* @param {ExecutionPathOptions} _options
* @return {boolean}
*/
hasEffectsWhenAccessedAtPath (path: string[], _options: ExecutionPathOptions) {
hasEffectsWhenAccessedAtPath (path: ObjectPath, _options: ExecutionPathOptions) {
return path.length > 0;
}

Expand All @@ -123,7 +124,7 @@ export default class Node {
* @param {ExecutionPathOptions} _options
* @return {boolean}
*/
hasEffectsWhenAssignedAtPath (_path: string[], _options: ExecutionPathOptions) {
hasEffectsWhenAssignedAtPath (_path: ObjectPath, _options: ExecutionPathOptions) {
return true;
}

Expand All @@ -133,7 +134,7 @@ export default class Node {
* @param {ExecutionPathOptions} _options
* @return {boolean}
*/
hasEffectsWhenCalledAtPath (_path: string[], _callOptions: CallOptions, _options: ExecutionPathOptions) {
hasEffectsWhenCalledAtPath (_path: ObjectPath, _callOptions: CallOptions, _options: ExecutionPathOptions) {
return true;
}

Expand Down Expand Up @@ -270,7 +271,7 @@ export default class Node {
* @returns {boolean}
*/
someReturnExpressionWhenCalledAtPath (
_path: string[],
_path: ObjectPath,
_callOptions: CallOptions,
predicateFunction: (options: ExecutionPathOptions) => PredicateFunction,
options: ExecutionPathOptions
Expand Down
5 changes: 3 additions & 2 deletions src/ast/nodes/ArrayExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import Node from '../Node';
import Expression from './Expression';
import SpreadElement from './SpreadElement';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ObjectPath } from '../variables/VariableReassignmentTracker';

export default class ArrayExpression extends Node {
type: 'ArrayExpression';
elements: (Expression | SpreadElement | null)[]
elements: (Expression | SpreadElement | null)[];

hasEffectsWhenAccessedAtPath (path: string[], _options: ExecutionPathOptions) {
hasEffectsWhenAccessedAtPath (path: ObjectPath, _options: ExecutionPathOptions) {
return path.length > 1;
}
}
5 changes: 3 additions & 2 deletions src/ast/nodes/ArrayPattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import ExecutionPathOptions from '../ExecutionPathOptions';
import Pattern from './Pattern';
import Expression from './Expression';
import Declaration from './Declaration';
import { ObjectPath } from '../variables/VariableReassignmentTracker';

export default class ArrayPattern extends Node {
type: 'ArrayPattern';
elements: (Pattern | null)[];

reassignPath (path: string[], options: ExecutionPathOptions) {
reassignPath (path: ObjectPath, options: ExecutionPathOptions) {
path.length === 0 &&
this.eachChild(child => child.reassignPath([], options));
}

hasEffectsWhenAssignedAtPath (path: string[], options: ExecutionPathOptions) {
hasEffectsWhenAssignedAtPath (path: ObjectPath, options: ExecutionPathOptions) {
return (
path.length > 0 ||
this.someChild(child => child.hasEffectsWhenAssignedAtPath([], options))
Expand Down
11 changes: 6 additions & 5 deletions src/ast/nodes/ArrowFunctionExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Expression from './Expression';
import CallOptions from '../CallOptions';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { PredicateFunction } from '../values';
import { ObjectPath } from '../variables/VariableReassignmentTracker';

export default class ArrowFunctionExpression extends Node {
type: 'ArrowFunctionExpression';
Expand All @@ -21,7 +22,7 @@ export default class ArrowFunctionExpression extends Node {
}

forEachReturnExpressionWhenCalledAtPath (
path: string[],
path: ObjectPath,
callOptions: CallOptions,
callback: ForEachReturnExpressionCallback,
options: ExecutionPathOptions
Expand All @@ -34,15 +35,15 @@ export default class ArrowFunctionExpression extends Node {
return false;
}

hasEffectsWhenAccessedAtPath (path: string[], _options: ExecutionPathOptions) {
hasEffectsWhenAccessedAtPath (path: ObjectPath, _options: ExecutionPathOptions) {
return path.length > 1;
}

hasEffectsWhenAssignedAtPath (path: string[], _options: ExecutionPathOptions) {
hasEffectsWhenAssignedAtPath (path: ObjectPath, _options: ExecutionPathOptions) {
return path.length > 1;
}

hasEffectsWhenCalledAtPath (path: string[], _callOptions: CallOptions, options: ExecutionPathOptions): boolean {
hasEffectsWhenCalledAtPath (path: ObjectPath, _callOptions: CallOptions, options: ExecutionPathOptions): boolean {
if (path.length > 0) {
return true;
}
Expand All @@ -68,7 +69,7 @@ export default class ArrowFunctionExpression extends Node {
}

someReturnExpressionWhenCalledAtPath (
path: string[],
path: ObjectPath,
callOptions: CallOptions,
predicateFunction: (options: ExecutionPathOptions) => PredicateFunction,
options: ExecutionPathOptions
Expand Down
3 changes: 2 additions & 1 deletion src/ast/nodes/AssignmentExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import disallowIllegalReassignment from './shared/disallowIllegalReassignment';
import ExecutionPathOptions from '../ExecutionPathOptions';
import Pattern from './Pattern';
import Expression from './Expression';
import { ObjectPath } from '../variables/VariableReassignmentTracker';

export default class AssignmentExpression extends Node {
type: 'AssignmentExpression';
Expand All @@ -21,7 +22,7 @@ export default class AssignmentExpression extends Node {
);
}

hasEffectsWhenAccessedAtPath (path: string[], options: ExecutionPathOptions): boolean {
hasEffectsWhenAccessedAtPath (path: ObjectPath, options: ExecutionPathOptions): boolean {
return (
path.length > 0 && this.right.hasEffectsWhenAccessedAtPath(path, options)
);
Expand Down
5 changes: 3 additions & 2 deletions src/ast/nodes/AssignmentPattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Expression from './Expression';
import Scope from '../Scopes/Scope';
import { UnknownAssignment } from '../values';
import Declaration from './Declaration';
import { ObjectPath } from '../variables/VariableReassignmentTracker';

export default class AssignmentPattern extends Node {
type: 'AssignmentPattern';
Expand All @@ -15,11 +16,11 @@ export default class AssignmentPattern extends Node {
this.left.reassignPath([], ExecutionPathOptions.create());
}

reassignPath (path: string[], options: ExecutionPathOptions) {
reassignPath (path: ObjectPath, options: ExecutionPathOptions) {
path.length === 0 && this.left.reassignPath(path, options);
}

hasEffectsWhenAssignedAtPath (path: string[], options: ExecutionPathOptions): boolean {
hasEffectsWhenAssignedAtPath (path: ObjectPath, options: ExecutionPathOptions): boolean {
return (
path.length > 0 || this.left.hasEffectsWhenAssignedAtPath([], options)
);
Expand Down
27 changes: 25 additions & 2 deletions src/ast/nodes/BinaryExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,31 @@ import Node from '../Node';
import { UNKNOWN_VALUE } from '../values';
import Expression from './Expression';
import ExecutionPathOptions from '../ExecutionPathOptions';
import { ObjectPath } from '../variables/VariableReassignmentTracker';

type BinaryOperator = '==' | '!=' | '===' | '!==' | '<' | '<=' | '>' | '>=' | '<<' | '>>' | '>>>' | '+' | '-' | '*' | '/' | '%' | ' |' | '^' | '&' | '**' | 'in' | 'instanceof';
type BinaryOperator =
'=='
| '!='
| '==='
| '!=='
| '<'
| '<='
| '>'
| '>='
| '<<'
| '>>'
| '>>>'
| '+'
| '-'
| '*'
| '/'
| '%'
| ' |'
| '^'
| '&'
| '**'
| 'in'
| 'instanceof';

const operators: {
[operator: string]: (left: any, right: any) => any
Expand Down Expand Up @@ -51,7 +74,7 @@ export default class BinaryExpression extends Node {
return operatorFn(leftValue, rightValue);
}

hasEffectsWhenAccessedAtPath (path: string[], _options: ExecutionPathOptions) {
hasEffectsWhenAccessedAtPath (path: ObjectPath, _options: ExecutionPathOptions) {
return path.length > 1;
}
}
13 changes: 7 additions & 6 deletions src/ast/nodes/CallExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ExecutionPathOptions from '../ExecutionPathOptions';
import SpreadElement from './SpreadElement';
import { PredicateFunction } from '../values';
import GlobalVariable from '../variables/GlobalVariable';
import { ObjectPath } from '../variables/VariableReassignmentTracker';

// TODO: 3 typing failures because AwaitExpression has no forEachReturnExpressionWhenCalledAtPath
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this case resolved now? If so the comment here can be removed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed this now.


Expand All @@ -15,7 +16,7 @@ export default class CallExpression extends Node {

private _callOptions: CallOptions;

reassignPath (path: string[], options: ExecutionPathOptions) {
reassignPath (path: ObjectPath, options: ExecutionPathOptions) {
!options.hasReturnExpressionBeenAssignedAtPath(path, this) &&
this.callee.forEachReturnExpressionWhenCalledAtPath(
[],
Expand Down Expand Up @@ -58,7 +59,7 @@ export default class CallExpression extends Node {
}

forEachReturnExpressionWhenCalledAtPath (
path: string[],
path: ObjectPath,
callOptions: CallOptions,
callback: ForEachReturnExpressionCallback,
options: ExecutionPathOptions
Expand Down Expand Up @@ -88,7 +89,7 @@ export default class CallExpression extends Node {
);
}

hasEffectsWhenAccessedAtPath (path: string[], options: ExecutionPathOptions): boolean {
hasEffectsWhenAccessedAtPath (path: ObjectPath, options: ExecutionPathOptions): boolean {
return (
path.length > 0 &&
!options.hasReturnExpressionBeenAccessedAtPath(path, this) &&
Expand All @@ -105,7 +106,7 @@ export default class CallExpression extends Node {
);
}

hasEffectsWhenAssignedAtPath (path: string[], options: ExecutionPathOptions): boolean {
hasEffectsWhenAssignedAtPath (path: ObjectPath, options: ExecutionPathOptions): boolean {
return (
!options.hasReturnExpressionBeenAssignedAtPath(path, this) &&
this.callee.someReturnExpressionWhenCalledAtPath(
Expand All @@ -121,7 +122,7 @@ export default class CallExpression extends Node {
);
}

hasEffectsWhenCalledAtPath (path: string[], callOptions: CallOptions, options: ExecutionPathOptions): boolean {
hasEffectsWhenCalledAtPath (path: ObjectPath, callOptions: CallOptions, options: ExecutionPathOptions): boolean {
return (
!options.hasReturnExpressionBeenCalledAtPath(path, this) &&
this.callee.someReturnExpressionWhenCalledAtPath(
Expand All @@ -147,7 +148,7 @@ export default class CallExpression extends Node {
}

someReturnExpressionWhenCalledAtPath (
path: string[],
path: ObjectPath,
callOptions: CallOptions,
predicateFunction: (options: ExecutionPathOptions) => PredicateFunction,
options: ExecutionPathOptions
Expand Down
3 changes: 2 additions & 1 deletion src/ast/nodes/ClassBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import Node from '../Node';
import ExecutionPathOptions from '../ExecutionPathOptions';
import CallOptions from '../CallOptions';
import MethodDefinition from './MethodDefinition';
import { ObjectPath } from '../variables/VariableReassignmentTracker';

export default class ClassBody extends Node {
type: 'ClassBody';
body: MethodDefinition[];
classConstructor: MethodDefinition | null;

hasEffectsWhenCalledAtPath (path: string[], callOptions: CallOptions, options: ExecutionPathOptions) {
hasEffectsWhenCalledAtPath (path: ObjectPath, callOptions: CallOptions, options: ExecutionPathOptions) {
if (path.length > 0) {
return true;
}
Expand Down