UPDATE : we have offical docs now! : https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API
A work in progress documentation of how the TypeScript compiler works.
Best way I have found is to :
./node_modules/mocha/bin/mocha -R dot -g nodeModules --colors -t 20000 built/local/run.js
The official greping jake runtests test=nodeModules
didn't work (though test=project
does work).
Also see : https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md
Baselines
And use jake diff
with beyond compare.
set DIFF=C:\Program Files (x86)\Beyond Compare 3\BComp.exe
Its just a folder compare between baselines/local and baselines/reference
https://github.com/Microsoft/TypeScript/pull/262/files
Parser : Gives information about the isStatement
function. If that function returns then parseStatement
will get called which must return some type of Statement
. In this case we are going to return a TryStatement
from a utility parseCatchOrFinallyBlocksMissingTryStatement
function. These utility (parse-
) functions apparently prepare the AST for the emitter by creating (createNode
function) and finishing Nodes (finishNode
function).
https://github.com/Microsoft/TypeScript/pull/273/files
Parser : parseObjectLiteral
is used to parse object literals. parseDelimitedList
is used to parse the properties of object literals.
https://github.com/Microsoft/TypeScript/pull/365/files Changed to load extensionless reference tags (.d.ts
and .ts
)
microsoft/TypeScript#324
Basically create a copy of the program
and update it based on changes. Mostly inside tc.ts
the recompile
function.
microsoft/TypeScript#330
getContextualType
function by anders
https://github.com/Microsoft/TypeScript/pull/331/files
change the order of this
emit in class body. emitClassDeclaration
microsoft/TypeScript#414 Declaration file needs to always emit typeof function/static function instead of emitting signature
Ability to modify the output filename extension microsoft/TypeScript#425
interface A { (x: string): void }
interface B { (x: 'foo'): string }
var b: B;
b('foo') // <- here overloads should be processed as [(x:'foo'): string, (x: string): void]
hacking it to add a documentation emitter http://www.codeproject.com/Articles/635280/TypeScript-Compiler-Documentation-Output
An overview of a file in terms of lexical structure : microsoft/TypeScript#534
microsoft/TypeScript#1477 (comment)
Uses the ts.forEachChild
exported from the parser
. Helpful information from this comment. Note how the Node
is augmented by services
here
Demo : http://blog.ctaggart.com/2015/01/typescript-14-ast-from-nodejs.html
microsoft/TypeScript#1514 (comment)
They can be "rehydrated" on demand using the sourcetext by calling getChildren()
microsoft/TypeScript#1514 (comment)
In the language service microsoft/TypeScript#1651 (comment)
getFormattingEditsForRange
getFormattingEditsAfterKeystroke
getFormattingEditsForDocument
forgetFormattingEditsForDocument
once you've gotten the appropriate edits ranges, you can easily apply them in reverse and fix up the original source text.
Once you have them you can apply them in reverse : microsoft/TypeScript#1651 (comment)
function formatCode(orig: string, changes: TextChange[]): string {
var result = orig;
for (var i = changes.length - 1; i >= 0; i--) {
var change = changes[i];
var head = result.slice(0, change.span.start);
var tail = result.slice(change.span.start + change.span.length)
result = head + change.newText + tail;
}
return result;
}
Demo : http://blog.ctaggart.com/2015/01/format-typescript-with-v14-language.html There is also smart indentation alone: microsoft/TypeScript#1754
You can convert JSX to JavaScript using the API from : https://www.npmjs.com/package/react-tools JSX Specification : http://facebook.github.io/jsx/ How to augment: facebook/react#759 (comment)
microsoft/TypeScript#1728 (comment)
languageService
and program
are very similar in principal. Which you use depends on your scenario.
program
is great if you just want emit / linting, the os related features can be provided with a compilerHost
generated with a simple call to createCompilerHost
. Examples : https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler and https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#traversing-the-ast-with-a-little-linter
languageService
is great if want something like a full blown IDE support.
More on the difference:
microsoft/TypeScript#1786 (comment)
a singe LanguageService object does not map to a single Program object, it actually maps to a set of them. A Program is an immutable object, the LanguageService on the other hand handles changes. under the covers, the LanguageService, on every change, creates a new Program instance to map to the current context, but uses the previous Program instance's unchanged SourceFiles to save computation. If the LanguageService was just an extension over Program, you would need to create a new LanguageService object on every change.
LanguageService.getProgram().getSourceFiles()
More https://github.com/Microsoft/TypeScript/commit/9628191a1476bc0dbdb28bfd30b840656ffc26a3#commitcomment-9490325
e.g. getQuickInfoAtPosition
and getCompletionsAtPosition
microsoft/TypeScript#2105 (comment)
http://www.scottlogic.com/blog/2015/01/20/typescript-compiler-api.html
microsoft/TypeScript#1690 (comment)
https://github.com/Microsoft/TypeScript/pull/4330/files smartformatter.ts
+ test
microsoft/TypeScript#7310
about currentNode
and canReuseNode
https://github.com/Microsoft/TypeScript/blob/release-1.8/src/compiler/parser.ts#L1463