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

Own version of typescript #76

Merged
merged 24 commits into from
Mar 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
node_modules/*
!node_modules/typescript-dev
.DS_Store
release-2
test/output
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "node_modules/typescript-dev"]
path = node_modules/typescript-dev
url = https://github.com/Microsoft/TypeScript.git
2 changes: 1 addition & 1 deletion definitions/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ declare module "typescript" {
pos: number;
end: number;
}
const enum SyntaxKind {
enum SyntaxKind {
Unknown = 0,
EndOfFileToken = 1,
SingleLineCommentTrivia = 2,
Expand Down
88 changes: 70 additions & 18 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ var concat = require('gulp-concat');
var header = require('gulp-header');
var diff = require('gulp-diff');

var tsProject = ts.createProject({
var tsOptions = {
target: 'es5',
module: 'commonjs',
noExternalResolve: true
});
noExternalResolve: true,
preserveConstEnums: true
};
var tsProject = ts.createProject(tsOptions);

var paths = {
scripts: ['lib/**.ts', 'definitions/**.ts'],
Expand All @@ -26,19 +28,18 @@ var tests = fs.readdirSync(path.join(__dirname, 'test')).filter(function(dir) {
return dir !== 'baselines' && dir !== 'output' && dir.substr(0, 1) !== '.';
});

// Clean
gulp.task('clean', function(cb) {
rimraf(paths.releaseBeta, cb);
// gulp.src([paths.releaseBeta + '/*'], { read: false }).pipe(clean());
});
gulp.task('clean-test', function(cb) {
rimraf('test/output', cb);
// gulp.src(['test/output/*'], { read: false }).pipe(clean());
});
gulp.task('clean-release', function(cb) {
rimraf(paths.release, cb);
// gulp.src([paths.release + '/*'], { read: false }).pipe(clean());
});

// Compile sources
gulp.task('scripts', ['clean'], function() {
var tsResult = gulp.src(paths.scripts)
.pipe(ts(tsProject));
Expand All @@ -47,24 +48,74 @@ gulp.task('scripts', ['clean'], function() {
.pipe(gulp.dest(paths.releaseBeta));
});

// Type checking against multiple versions of TypeScript:
// - master of TypeScript (typescript-dev)
// - jsx-typescript (a fork of TypeScript with JSX support)
// Checking against the current release of TypeScript on NPM can be done using `gulp scripts`.

gulp.task('typecheck-dev', function() {
return gulp.src(paths.scripts.concat([
'!defintions/typescript.d.ts',
path.resolve(require.resolve('typescript-dev'), './typescript.d.ts')
])).pipe(ts(tsOptions));
});

gulp.task('typecheck-jsx', function() {
return gulp.src(paths.scripts.concat([
'!defintions/typescript.d.ts',
path.resolve(require.resolve('jsx-typescript'), './typescript.d.ts')
])).pipe(ts(tsOptions));
});

gulp.task('typecheck', ['typecheck-dev', 'typecheck-jsx']);

// Tests

// helper function for running a test.
function runTest(name, callback) {
var newTS = require('./release-2/main');
// We run every test on multiple typescript versions: current release on NPM and the master from GitHub (typescript-dev).
var libs = [
['default', undefined],
['ts-next', require('typescript-dev')],
// ['jsx', require('jsx-typescript')] // TODO: Add jsx-typescript here. It currently throws an error when adding it.
];
var test = require('./test/' + name + '/gulptask.js');

test(newTS).on('finish', function() {
function onError(error) {
console.error('Test ' + name + ' failed: ' + error.message);
}
gulp.src('test/output/' + name + '/**')
.pipe(diff('test/baselines/' + name))
.on('error', onError)
.pipe(diff.reporter({ fail: true }))
.on('error', onError)
.on('finish', callback);
});
fs.mkdirSync('test/output/' + name);
for (var i = 0; i < libs.length; i++) {
(function(i) {
var lib = libs[i];
var output = 'test/output/' + name + '/' + lib[0] + '/';
var errors = [];
var reporter = {
error: function(err) {
errors.push(err);
}
};
fs.mkdirSync(output);
test(newTS, lib[1], output, reporter).on('finish', function() {
fs.writeFileSync(output + 'errors.txt', errors);
function onError(error) {
console.error('Test ' + name + ' failed: ' + error.message);
}
gulp.src('test/output/' + name + '/**')
.pipe(diff('test/baselines/' + name))
.on('error', onError)
.pipe(diff.reporter({ fail: true }))
.on('error', onError)
.on('finish', callback);
});
})(i);
}
}

gulp.task('test', ['clean-test', 'scripts'], function(cb) {
// Use `gulp test --tests [...]` to run specific test(s).
// Example: `gulp test --tests basic,errorReporting`

fs.mkdirSync('test/output/');

var currentTests = tests;
if (argv.tests !== undefined) {
currentTests = argv.tests.split(',');
Expand All @@ -84,6 +135,7 @@ gulp.task('test', ['clean-test', 'scripts'], function(cb) {
}
});

// Accept new baselines
gulp.task('test-baselines-accept', function(cb) {
rimraf(paths.releaseBeta, function() {
gulp.src('test/output/**').pipe(gulp.dest('test/baselines')).on('finish', cb);
Expand All @@ -98,4 +150,4 @@ gulp.task('watch', ['scripts'], function() {
gulp.watch(paths.scripts, ['scripts']);
});

gulp.task('default', ['scripts']);
gulp.task('default', ['scripts', 'typecheck', 'test']);
4 changes: 2 additions & 2 deletions lib/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class Filter {
this.referencedFromAll.push(file.filename);

for (var i = 0; i < file.ts.referencedFiles.length; i++) {
var ref = file.ts.referencedFiles[i].filename;
ref = project.Project.normalizePath(path.join(path.dirname(file.ts.filename), ref));
var ref = project.Project.getFileName(file.ts.referencedFiles[i]);
ref = project.Project.normalizePath(path.join(path.dirname(project.Project.getFileName(file.ts)), ref));

var refFile = this.project.currentFiles[ref];
if (refFile) addReference(refFile);
Expand Down
44 changes: 33 additions & 11 deletions lib/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,37 @@ import fs = require('fs');
import path = require('path');

export class Host implements ts.CompilerHost {
static libDefault: ts.SourceFile;
static initLibDefault() {
var content = fs.readFileSync(path.resolve(path.dirname(require.resolve('typescript')) + '/lib.d.ts')).toString('utf8');
this.libDefault = ts.createSourceFile('__lib.d.ts', content, ts.ScriptTarget.ES3, "0"); // Will also work for ES5 & 6
static libDefault: project.Map<ts.SourceFile> = {};
static getLibDefault(typescript: typeof ts) {
var filename: string;
for (var i in require.cache) {
if (!Object.prototype.hasOwnProperty.call(require.cache, i)) continue;

if (require.cache[i].exports === typescript) {
filename = i;
}
}
if (filename === undefined) {
return undefined; // Not found
}
if (this.libDefault[filename]) {
return this.libDefault[filename]; // Already loaded
}

var content = fs.readFileSync(path.resolve(path.dirname(filename) + '/lib.d.ts')).toString('utf8');
return this.libDefault[filename] = typescript.createSourceFile('__lib.d.ts', content, typescript.ScriptTarget.ES3, "0"); // Will also work for ES5 & 6
}

typescript: typeof ts;

private currentDirectory: string;
private files: project.Map<project.FileData>;
private externalResolve: boolean;
output: project.Map<string>;

constructor(currentDirectory: string, files: project.Map<project.FileData>, externalResolve: boolean) {
constructor(typescript: typeof ts, currentDirectory: string, files: project.Map<project.FileData>, externalResolve: boolean) {
this.typescript = typescript;

this.currentDirectory = currentDirectory;
this.files = files;

Expand All @@ -38,7 +57,7 @@ export class Host implements ts.CompilerHost {
return false;
}

getCurrentDirectory() {
getCurrentDirectory = () => {
return this.currentDirectory;
}
getCanonicalFileName(filename: string) {
Expand All @@ -47,12 +66,15 @@ export class Host implements ts.CompilerHost {
getDefaultLibFilename() {
return '__lib.d.ts';
}
getDefaultLibFileName() {
return '__lib.d.ts';
}

writeFile(filename: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) {
writeFile = (filename: string, data: string, writeByteOrderMark: boolean, onError?: (message: string) => void) => {
this.output[filename] = data;
}

getSourceFile(filename: string, languageVersion: ts.ScriptTarget, onError?: (message: string) => void): ts.SourceFile {
getSourceFile = (filename: string, languageVersion: ts.ScriptTarget, onError?: (message: string) => void): ts.SourceFile => {
var text: string;

var normalizedFilename = project.Project.normalizePath(filename);
Expand All @@ -64,7 +86,7 @@ export class Host implements ts.CompilerHost {
return this.files[normalizedFilename].ts;
}
} else if (normalizedFilename === '__lib.d.ts') {
return Host.libDefault;
return Host.getLibDefault(this.typescript);
} else {
if (this.externalResolve) {
try {
Expand All @@ -77,7 +99,8 @@ export class Host implements ts.CompilerHost {

if (typeof text !== 'string') return undefined;

var file = ts.createSourceFile(filename, text, languageVersion, "0");
var file = this.typescript.createSourceFile(filename, text, languageVersion, "0");

this.files[normalizedFilename] = {
filename: normalizedFilename,
originalFilename: filename,
Expand All @@ -91,4 +114,3 @@ export class Host implements ts.CompilerHost {
return this.files[project.Project.normalizePath(filename)];
}
}
Host.initLibDefault();
11 changes: 8 additions & 3 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function compile(param?: any, filters?: compile.FilterSettings, theReporter?: _r
if (param instanceof project.Project) {
proj = param;
} else {
proj = new project.Project(getCompilerOptions(param || {}), (param && param.noExternalResolve) || false, (param && param.sortOutput) || false);
proj = new project.Project(getCompilerOptions(param || {}), (param && param.noExternalResolve) || false, (param && param.sortOutput) || false, (param && param.typescript) || undefined);
}

proj.reset();
Expand All @@ -124,6 +124,9 @@ var moduleMap: project.Map<ts.ModuleKind> = {
function getCompilerOptions(settings: compile.Settings): ts.CompilerOptions {
var tsSettings: ts.CompilerOptions = {};

if (settings.preserveConstEnums !== undefined) {
tsSettings.preserveConstEnums = settings.preserveConstEnums;
}
if (settings.removeComments !== undefined) {
tsSettings.removeComments = settings.removeComments;
}
Expand Down Expand Up @@ -172,7 +175,7 @@ function getCompilerOptions(settings: compile.Settings): ts.CompilerOptions {

module compile {
export interface Settings {
//propagateEnumConstants?: boolean;
preserveConstEnums?: boolean;
removeComments?: boolean;

//allowAutomaticSemicolonInsertion?: boolean;
Expand All @@ -189,14 +192,16 @@ module compile {

noExternalResolve?: boolean;
sortOutput?: boolean;

typescript?: typeof ts;
}
export interface FilterSettings {
referencedFrom: string[];
}
export import Project = project.Project;
export import reporter = _reporter;
export function createProject(settings: Settings): Project {
return new Project(getCompilerOptions(settings), settings.noExternalResolve ? true : false, settings.sortOutput ? true : false);
return new Project(getCompilerOptions(settings), settings.noExternalResolve ? true : false, settings.sortOutput ? true : false, settings.typescript);
}

export function filter(project: Project, filters: FilterSettings): NodeJS.ReadWriteStream {
Expand Down
Loading