TypeScriptの中のこと(浅
め)
自己紹介
• @k_maru
• 主にMS系エンジニア
• grunt-typescript作者
typescript.js
grunt-typescript
各種クラス
> tsc
tsc.js
各種クラス
BatchCompiler
BatchCompiler ”モドキ”
Grunt Task
> grunt
grunt-typescript の場合tsc の場合
tsc.js
typescript.js
typescriptService.js
parserとかscannerとかASTとかコンパ
イルするための基本的なものがすべて
入ってる。
コンパイラーの基本セット
typescript.jsの内容に加えて、ファ
イル出力を担当するioとかコマンドライ
ン引数をパースするやつとか、
BatchCompilerとかが入ってる。
tscコマンドライン用
typescript.jsの内容に加えて、外部
から利用する用のインターフェイスとか
コードフォーマットとか括弧のマッチン
グとかが入ってる。
主にエディタサポート用
typescriptService.jsの手に入れ方
• 自分でソースからコンパイルしましょう。
> git clone https://git01.codeplex.com/typescript TypeScript
> cd TypeScript
> npm install
> node_modules/.bin/jake local
> cd built/local
> ls
jquery.d.ts tsc.d.ts typescript.d.ts typescriptServices.d.ts winjs.d.ts
lib.d.ts tsc.js typescript.js typescriptServices.js winrt.d.ts
• もしくはLKG(Last Known Good)を使いましょう。d.tsはないけど…
> git clone https://git01.codeplex.com/typescript TypeScript
> cd TypeScript/bin
> ls
jquery.d.ts lib.d.ts resources tsc tsc.js typescript.js typescriptServices.js winjs.d.ts
winrt.d.ts
BatchCompilerがやっている動きをざっく
り追いながら中のお話を浅ーくしていきま
す。
参照関係の解決
コマンドライン引数の
パース
コンパイル
参照関係の解決
コマンドライン引数の
パース
コンパイル
nodeで実行
-w / --watchオプションがある
wshで実行
--codepage オプションがある
コマンドラインで指定可能なオプション
• OptionParserでパースしてCompilationSettingsを作成
-documented out outDir sourcemap mapRoot sourceRoot declaration
removeComments
target module help version noImplicitAny
-documented(node only) watch
-documented(wsh only) codepage
-undocumented diagnostics locale noResolve noLib logFile
propagateEnumConstants
useCaseSensitiveFileResoltionコンパイル詳細情報を表示してくれる
メッセージのロケールを設定する
コンパイル結果をファイルに出力してくれる
内部的なオプションの設定
export class CompilationSettings {
public propagateEnumConstants: boolean = false;
public removeComments: boolean = false;
public watch: boolean = false;
public noResolve: boolean = false;
public allowAutomaticSemicolonInsertion: boolean = true;
public noImplicitAny: boolean = false;
public noLib: boolean = false;
public codeGenTarget: LanguageVersion = LanguageVersion.EcmaScript3;
public moduleGenTarget: ModuleGenTarget =
ModuleGenTarget.Unspecified;
public outFileOption: string = "";
public outDirOption: string = "";
public mapSourceFiles: boolean = false;
public mapRoot: string = "";
public sourceRoot: string = "";
public generateDeclarationFiles: boolean = false;
public useCaseSensitiveFileResolution: boolean = false;
public gatherDiagnostics: boolean = false;
public codepage: number = null
public createFileLog: boolean = false;
}
settings.ts
勝手にセミコロンを設定してくれる
参照関係の解決
コマンドライン引数の
パース
コンパイル
///<reference path="hello.ts" />
import log = require("./log");
var result = hello("TypeScript");
log.message(result);
index.ts
hello.tsを対象に追加
参照を確認してコンパイル対象に自動で設定する
log.tsを対象に追加
> ls
index.ts hello.ts log.ts
> tsc index.ts --module commonjs
参照の確認の流れ
tokenを作る
importキーワードを探す
いろいろ条件指定して一致したら追加
referenceコメントを探す
いろいろ条件指定して一致したら追加
--noResolveオプション
--module指定が必要な構成だとエラーになる。
ただし、上記ではなくかつオプションに全ファ
イルを指定できるのならコンパイルは速くなる。
副作用の詳細は不明
参照関係の解決
コマンドライン引数の
パース
コンパイル
コンパイルの流れ
TypeScriptCompilerオブジェクトの作成
コンパイル対象のファイルを全部コンパイ
ラーオブジェクトに追加
コンパイルの実行
シンタックスの確認
セマンティックの確認
オプションの整合性の確認
JavaScriptの生成(Emit)
型定義の生成(EmitDeclaration)
ファイルの出力
ポイントその1
ASTは要求された時に初めて生成される。
つまりコンパイル内のステップを飛ばし
ても問題ない。
ポイントその2
セマンティックに問題があってもEmit
は実行される。
→エラーが出てるのにファイルが作成さ
れるって現象はこれが理由
おまけ
Undocumented TripleSlashComment
///<implicit-import />
///<amd-dependency path=“” />
///<reference no-default-
lib="true"/>
--module amd でコンパイルした時に
pathで指定した値が生成される
JavaScriptのdefineに組み込まれる。
domreadyとかによさげ
外部モジュールとして取り扱われる。
使いどころ不明
参照関係の解決時にこれが指定された
ファイルが見つかるとlib.d.tsが読み
込まれなくなる。
ご利用は計画的に
ご清聴ありがとうございました。

Typescriptの中のこと(浅め)