Skip to content

Commit 2ddf38b

Browse files
authored
Merge pull request #1 from jumperchen/keikai
"Get latest changes from dart-archive/ts2dart master #1"
2 parents c43031f + 65073d2 commit 2ddf38b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+5300
-733
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
phutil_register_library('npm-test', __FILE__);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/**
4+
* This file is automatically generated. Use 'arc liberate' to rebuild it.
5+
*
6+
* @generated
7+
* @phutil-library-version 2
8+
*/
9+
phutil_register_library_map(array(
10+
'__library_version__' => 2,
11+
'class' => array(
12+
'NpmUnitTestEngine' => 'src/NpmUnitTestEngine.php',
13+
),
14+
'function' => array(),
15+
'xmap' => array(
16+
'NpmUnitTestEngine' => 'ArcanistUnitTestEngine',
17+
),
18+
));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
class NpmUnitTestEngine extends ArcanistUnitTestEngine {
3+
public function run() {
4+
$retval = 0;
5+
system('npm test', $retval);
6+
$result = new ArcanistUnitTestResult();
7+
$result->setName('npm test');
8+
if ($retval == 0) {
9+
$result->setResult(ArcanistUnitTestResult::RESULT_PASS);
10+
} else {
11+
$result->setResult(ArcanistUnitTestResult::RESULT_FAIL);
12+
}
13+
return array($result);
14+
}
15+
16+
public function shouldEchoTestResults() {
17+
return false;
18+
}
19+
}
20+
?>

.arcconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"base": "git:origin/master",
3+
"phabricator.uri" : "https://reviews.angular.io/",
4+
"load": [
5+
".arc/npm-test"
6+
],
7+
"unit.engine": "NpmUnitTestEngine"
8+
}

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Compiled JS
2-
build
32

43
# Logs
54
logs
@@ -9,6 +8,7 @@ logs
98
pids
109
*.pid
1110
*.seed
11+
.phutil_module_cache
1212

1313
# Type definitions installed with tsd
1414
typings
@@ -30,3 +30,7 @@ node_modules
3030

3131
# Users Environment Variables
3232
.lock-wscript
33+
34+
# IDEs
35+
.idea/
36+
/ts2dart.iml

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# TypeScript to Dart transpiler [![Build Status](https://travis-ci.org/angular/ts2dart.svg?branch=master)](https://travis-ci.org/angular/ts2dart)
22

3+
ts2dart is a TypeScript to Dart transpiler. It's mainly used to translate Angular 2 from TypeScript
4+
to Dart for its Dart user base.
35

4-
ts2dart is a TypeScript to Dart transpiler. It's in its very early days and under heavy development,
5-
not ready for production use.
6+
## Usage
7+
8+
- To install as Command Line Tool execute: `npm i -g ts2dart`
9+
- Once installed you could run it doing: `ts2dart inputFile.ts`
610

711
## Installation
812

@@ -15,3 +19,15 @@ not ready for production use.
1519
- `gulp test.e2e` executes the e2e tests,
1620
- `gulp test.check-format` checks the source code formatting using `clang-format`,
1721
- `gulp test` runs unit tests, e2e tests and checks the source code formatting.
22+
23+
## Phabricator Reviews
24+
25+
You can send pull requests via Github, or by creating a Phabricator diff on
26+
https://reviews.angular.io. Both are fine, though Phabricator has a nicer code review UI.
27+
28+
To create a Phabricator diff:
29+
30+
- create an account on https://reviews.angular.io
31+
- install [Arcanist](https://secure.phabricator.com/book/phabricator/article/arcanist/)
32+
- run `arc diff` to upload a diff (= pull request). This will also run all tests.
33+
- get it reviewed by entering a "Reviewer", e.g. "mprobst", "alexeagle", "viks", ...

build/definitions/base.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as ts from 'typescript';
2+
import { Transpiler } from './main';
3+
export declare type ClassLike = ts.ClassDeclaration | ts.InterfaceDeclaration;
4+
export declare function ident(n: ts.Node): string;
5+
export declare class TranspilerBase {
6+
private transpiler;
7+
private idCounter;
8+
constructor(transpiler: Transpiler);
9+
visit(n: ts.Node): void;
10+
emit(s: string): void;
11+
emitBefore(s: string, search: string): void;
12+
emitNoSpace(s: string): void;
13+
reportError(n: ts.Node, message: string): void;
14+
visitNode(n: ts.Node): boolean;
15+
visitEach(nodes: ts.Node[]): void;
16+
visitEachIfPresent(nodes?: ts.Node[]): void;
17+
visitList(nodes: ts.Node[], separator?: string): void;
18+
uniqueId(name: string): string;
19+
assert(c: ts.Node, condition: boolean, reason: string): void;
20+
getAncestor(n: ts.Node, kind: ts.SyntaxKind): ts.Node;
21+
hasAncestor(n: ts.Node, kind: ts.SyntaxKind): boolean;
22+
hasAnnotation(decorators: ts.NodeArray<ts.Decorator>, name: string): boolean;
23+
hasFlag(n: {
24+
flags: number;
25+
}, flag: ts.NodeFlags): boolean;
26+
maybeDestructureIndexType(node: ts.TypeLiteralNode): [ts.TypeNode, ts.TypeNode];
27+
getRelativeFileName(fileName: string): string;
28+
maybeVisitTypeArguments(n: {
29+
typeArguments?: ts.NodeArray<ts.TypeNode>;
30+
}): void;
31+
}

build/definitions/call.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import ts = require('typescript');
2+
import base = require('./base');
3+
import ts2dart = require('./main');
4+
import { FacadeConverter } from './facade_converter';
5+
export default class CallTranspiler extends base.TranspilerBase {
6+
private fc;
7+
constructor(tr: ts2dart.Transpiler, fc: FacadeConverter);
8+
visitNode(node: ts.Node): boolean;
9+
private visitCall(c);
10+
private handleNamedParamsCall(c);
11+
/**
12+
* Handles constructor initializer lists and bodies.
13+
*
14+
* <p>Dart's super() ctor calls have to be moved to the constructors initializer list, and `const`
15+
* constructors must be completely empty, only assigning into fields through the initializer list.
16+
* The code below finds super() calls and handles const constructors, marked with the special
17+
* `@CONST` annotation on the class.
18+
*
19+
* <p>Not emitting super() calls when traversing the ctor body is handled by maybeHandleSuperCall
20+
* below.
21+
*/
22+
private visitConstructorBody(ctor);
23+
/**
24+
* Checks whether `callExpr` is a super() call that should be ignored because it was already
25+
* handled by `maybeEmitSuperInitializer` above.
26+
*/
27+
private maybeHandleSuperCall(callExpr);
28+
}

build/definitions/declaration.d.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as ts from 'typescript';
2+
import * as base from './base';
3+
import { FacadeConverter } from './facade_converter';
4+
import { Transpiler } from './main';
5+
export default class DeclarationTranspiler extends base.TranspilerBase {
6+
private fc;
7+
private enforceUnderscoreConventions;
8+
constructor(tr: Transpiler, fc: FacadeConverter, enforceUnderscoreConventions: boolean);
9+
visitNode(node: ts.Node): boolean;
10+
private visitVariableDeclarationType(varDecl);
11+
private visitFunctionLike(fn, accessor?);
12+
private visitParameters(parameters);
13+
/**
14+
* Visit a property declaration.
15+
* In the special case of property parameters in a constructor, we also allow a parameter to be
16+
* emitted as a property.
17+
*/
18+
private visitProperty(decl, isParameter?);
19+
private visitClassLike(keyword, decl);
20+
private visitDecorators(decorators);
21+
private visitDeclarationMetadata(decl);
22+
private visitNamedParameter(paramDecl);
23+
private getInitializers(paramDecl);
24+
/**
25+
* Handles a function typedef-like interface, i.e. an interface that only declares a single
26+
* call signature, by translating to a Dart `typedef`.
27+
*/
28+
private visitFunctionTypedefInterface(name, signature, typeParameters);
29+
}

build/definitions/expression.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as ts from 'typescript';
2+
import * as base from './base';
3+
import { FacadeConverter } from './facade_converter';
4+
import { Transpiler } from './main';
5+
export default class ExpressionTranspiler extends base.TranspilerBase {
6+
private fc;
7+
constructor(tr: Transpiler, fc: FacadeConverter);
8+
visitNode(node: ts.Node): boolean;
9+
visitAndWrapAsInt(n: ts.Expression): void;
10+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import * as ts from 'typescript';
2+
import * as base from './base';
3+
import { Transpiler } from './main';
4+
export declare class FacadeConverter extends base.TranspilerBase {
5+
private tc;
6+
private defaultLibLocation;
7+
private candidateProperties;
8+
private candidateTypes;
9+
private genericMethodDeclDepth;
10+
constructor(transpiler: Transpiler);
11+
initializeTypeBasedConversion(tc: ts.TypeChecker, opts: ts.CompilerOptions, host: ts.CompilerHost): void;
12+
private extractPropertyNames(m, candidates);
13+
private resolveModuleNames(opts, host, m);
14+
/**
15+
* To avoid strongly referencing the Provider class (which could bloat binary size), Angular 2
16+
* write providers as object literals. However the Dart transformers don't recognize this, so
17+
* ts2dart translates the special syntax `/* @ts2dart_Provider * / {provide: Class, param1: ...}`
18+
* into `const Provider(Class, param1: ...)`.
19+
*/
20+
maybeHandleProvider(ole: ts.ObjectLiteralExpression): boolean;
21+
maybeHandleCall(c: ts.CallExpression): boolean;
22+
handlePropertyAccess(pa: ts.PropertyAccessExpression): boolean;
23+
/**
24+
* Searches for type references that require extra imports and emits the imports as necessary.
25+
*/
26+
emitExtraImports(sourceFile: ts.SourceFile): void;
27+
private emitImports(n, libraries, emitted, sourceFile);
28+
pushTypeParameterNames(n: ts.FunctionLikeDeclaration): void;
29+
popTypeParameterNames(n: ts.FunctionLikeDeclaration): void;
30+
resolvePropertyTypes(tn: ts.TypeNode): ts.Map<ts.PropertyDeclaration>;
31+
/**
32+
* The Dart Development Compiler (DDC) has a syntax extension that uses comments to emulate
33+
* generic methods in Dart. ts2dart has to hack around this and keep track of which type names
34+
* in the current scope are actually DDC type parameters and need to be emitted in comments.
35+
*
36+
* TODO(martinprobst): Remove this once the DDC hack has made it into Dart proper.
37+
*/
38+
private isGenericMethodTypeParameterName(name);
39+
visitTypeName(typeName: ts.EntityName): void;
40+
shouldEmitNew(c: ts.CallExpression): boolean;
41+
private getCallInformation(c);
42+
private getHandler<T>(n, symbol, m);
43+
private getFileAndName(n, originalSymbol);
44+
private isNamedDefaultLibType(node, qname);
45+
private reportMissingType(n, ident);
46+
private static DECLARATIONS;
47+
isInsideConstExpr(node: ts.Node): boolean;
48+
isConstClass(decl: base.ClassLike): boolean;
49+
/**
50+
* isConstExpr returns true if the passed in expression itself is a const expression. const
51+
* expressions are marked by the special comment @ts2dart_const (expr), or by the special
52+
* function call CONST_EXPR.
53+
*/
54+
isConstExpr(node: ts.Node): boolean;
55+
hasConstComment(node: ts.Node): boolean;
56+
private hasMarkerComment(node, markerText);
57+
private emitMethodCall(name, args?);
58+
private emitCall(name, args?);
59+
private stdlibTypeReplacements;
60+
private tsToDartTypeNames;
61+
private es6Promises;
62+
private es6Collections;
63+
private stdlibHandlers;
64+
private callHandlerReplaceNew;
65+
private callHandlers;
66+
private es6CollectionsProp;
67+
private es6PromisesProp;
68+
private propertyHandlers;
69+
}

build/definitions/literal.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as ts from 'typescript';
2+
import * as base from './base';
3+
import { FacadeConverter } from './facade_converter';
4+
import { Transpiler } from './main';
5+
export default class LiteralTranspiler extends base.TranspilerBase {
6+
private fc;
7+
constructor(tr: Transpiler, fc: FacadeConverter);
8+
visitNode(node: ts.Node): boolean;
9+
private shouldBeConst(n);
10+
private escapeTextForTemplateString(n);
11+
private handleReifiedArray(node);
12+
private handleReifiedMap(node);
13+
}

build/definitions/main.d.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as ts from 'typescript';
2+
export interface TranspilerOptions {
3+
/**
4+
* Fail on the first error, do not collect multiple. Allows easier debugging as stack traces lead
5+
* directly to the offending line.
6+
*/
7+
failFast?: boolean;
8+
/** Whether to generate 'library a.b.c;' names from relative file paths. */
9+
generateLibraryName?: boolean;
10+
/** Whether to generate source maps. */
11+
generateSourceMap?: boolean;
12+
/** A tsconfig.json to use to configure TypeScript compilation. */
13+
tsconfig?: string;
14+
/**
15+
* A base path to relativize absolute file paths against. This is useful for library name
16+
* generation (see above) and nicer file names in error messages.
17+
*/
18+
basePath?: string;
19+
/**
20+
* Translate calls to builtins, i.e. seemlessly convert from `Array` to `List`, and convert the
21+
* corresponding methods. Requires type checking.
22+
*/
23+
translateBuiltins?: boolean;
24+
/**
25+
* Enforce conventions of public/private keyword and underscore prefix
26+
*/
27+
enforceUnderscoreConventions?: boolean;
28+
}
29+
export declare const COMPILER_OPTIONS: ts.CompilerOptions;
30+
export declare class Transpiler {
31+
private options;
32+
private output;
33+
private currentFile;
34+
private lastCommentIdx;
35+
private errors;
36+
private transpilers;
37+
private fc;
38+
constructor(options?: TranspilerOptions);
39+
/**
40+
* Transpiles the given files to Dart.
41+
* @param fileNames The input files.
42+
* @param destination Location to write files to. Creates files next to their sources if absent.
43+
*/
44+
transpile(fileNames: string[], destination?: string): void;
45+
translateProgram(program: ts.Program, host: ts.CompilerHost): {
46+
[path: string]: string;
47+
};
48+
private getCompilerOptions();
49+
private createCompilerHost();
50+
getOutputPath(filePath: string, destinationRoot: string): string;
51+
private translate(sourceFile);
52+
private formatCode(code, context);
53+
private checkForErrors(program);
54+
private diagnosticToString(diagnostic);
55+
/**
56+
* Returns `filePath`, relativized to the program's `basePath`.
57+
* @param filePath path to relativize.
58+
*/
59+
getRelativeFileName(filePath: string): string;
60+
emit(s: string): void;
61+
emitBefore(s: string, search: string): void;
62+
emitNoSpace(s: string): void;
63+
reportError(n: ts.Node, message: string): void;
64+
visit(node: ts.Node): void;
65+
private normalizeSlashes(path);
66+
private translateComment(comment);
67+
}
68+
export declare function getModuleResolver(compilerHost: ts.CompilerHost): (moduleNames: string[], containingFile: string) => ts.ResolvedModule[];

build/definitions/mkdirp.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default function mkdirP(p: string): void;

build/definitions/module.d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as ts from 'typescript';
2+
import * as base from './base';
3+
import { FacadeConverter } from './facade_converter';
4+
import { Transpiler } from './main';
5+
export default class ModuleTranspiler extends base.TranspilerBase {
6+
private fc;
7+
private generateLibraryName;
8+
constructor(tr: Transpiler, fc: FacadeConverter, generateLibraryName: boolean);
9+
visitNode(node: ts.Node): boolean;
10+
private static isIgnoredImport(e);
11+
private visitExternalModuleReferenceExpr(expr);
12+
private isEmptyImport(n);
13+
private filterImports(ns);
14+
private static DART_RESERVED_WORDS;
15+
getLibraryName(fileName: string): string;
16+
}

build/definitions/statement.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as ts from 'typescript';
2+
import * as base from './base';
3+
import { Transpiler } from './main';
4+
export default class StatementTranspiler extends base.TranspilerBase {
5+
constructor(tr: Transpiler);
6+
visitNode(node: ts.Node): boolean;
7+
}

build/definitions/type.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as ts from 'typescript';
2+
import * as base from './base';
3+
import { FacadeConverter } from './facade_converter';
4+
import { Transpiler } from './main';
5+
export default class TypeTranspiler extends base.TranspilerBase {
6+
private fc;
7+
constructor(tr: Transpiler, fc: FacadeConverter);
8+
visitNode(node: ts.Node): boolean;
9+
isReifiedTypeLiteral(node: ts.TypeAssertion): boolean;
10+
}

0 commit comments

Comments
 (0)