Skip to content

Commit d5fb470

Browse files
committed
feat(dts) lots of code for DTS. Pausing to catch my breath + starting code changes on dts-generator
1 parent ec0f4da commit d5fb470

26 files changed

+14972
-23
lines changed

Diff for: dist/example.d.ts

+14,441
Large diffs are not rendered by default.

Diff for: dist/main/atom/commands/commands.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ function registerCommands() {
245245
old_pane.destroyItem(old_pane.itemForUri(uri));
246246
}
247247
atom.workspace.open(uri, {
248-
text: atom.workspace.getActiveEditor().getText(),
248+
text: atom.workspace.getActiveTextEditor().getText(),
249249
filePath: atomUtils.getCurrentPath()
250250
});
251251
});
@@ -258,7 +258,7 @@ function registerCommands() {
258258
old_pane.destroyItem(old_pane.itemForUri(uri));
259259
}
260260
atom.workspace.open(uri, {
261-
text: atom.workspace.getActiveEditor().getText(),
261+
text: atom.workspace.getActiveTextEditor().getText(),
262262
filePath: atomUtils.getCurrentPath()
263263
});
264264
});

Diff for: dist/main/atom/gotoHistory.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ exports.buildOutput = { members: [] };
33
exports.referencesOutput = { members: [] };
44
exports.activeList = exports.errorsInOpenFiles;
55
function gotoLine(filePath, line, col, list) {
6-
var activeFile, activeEditor = atom.workspace.getActiveEditor();
6+
var activeFile, activeEditor = atom.workspace.getActiveTextEditor();
77
if (activeEditor !== undefined && activeEditor !== null) {
88
activeFile = activeEditor.getPath();
99
}
@@ -14,7 +14,7 @@ function gotoLine(filePath, line, col, list) {
1414
});
1515
}
1616
else {
17-
atom.workspace.getActiveEditor().cursors[0].setBufferPosition([line - 1, col]);
17+
atom.workspace.getActiveTextEditor().cursors[0].setBufferPosition([line - 1, col]);
1818
}
1919
list.lastPosition = { filePath: filePath, line: line, col: col };
2020
}

Diff for: dist/main/atom/views/documentationView.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var DocumentationView = (function (_super) {
3333
this.documentation.html(content.documentation);
3434
};
3535
DocumentationView.prototype.autoPosition = function () {
36-
var editor = atom.workspace.getActiveEditor();
36+
var editor = atom.workspace.getActiveTextEditor();
3737
var cursor = editor.getCursor();
3838
var cursorTop = cursor.getPixelRect().top - editor.getScrollTop();
3939
var editorHeight = editor.getHeight();

Diff for: dist/main/atom/views/renameView.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ var RenameView = (function (_super) {
5757
};
5858
RenameView.prototype.renameThis = function (options) {
5959
this.options = options;
60-
this.editorAtRenameStart = atom.workspace.getActiveEditor();
60+
this.editorAtRenameStart = atom.workspace.getActiveTextEditor();
6161
panel.show();
6262
this.newNameEditor.model.setText(options.text);
6363
if (this.options.autoSelect) {

Diff for: dist/main/atomts.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var atomConfig = require('./atom/atomConfig');
2424
exports.config = atomConfig.schema;
2525
var utils_1 = require("./lang/utils");
2626
var hideIfNotActiveOnStart = utils_1.debounce(function () {
27-
var editor = atom.workspace.getActiveEditor();
27+
var editor = atom.workspace.getActiveTextEditor();
2828
if (editor && editor.getGrammar() && editor.getGrammar().name !== 'TypeScript') {
2929
mainPanelView.hide();
3030
}

Diff for: dist/main/lang/modules/building.js

+23
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,26 @@ function getRawOutput(proj, filePath) {
5151
return output;
5252
}
5353
exports.getRawOutput = getRawOutput;
54+
var dts = require("../../tsconfig/dts-generator");
55+
function emitDts(proj) {
56+
if (!proj.projectFile.project)
57+
return;
58+
if (!proj.projectFile.project.package)
59+
return;
60+
if (!proj.projectFile.project.package.directory)
61+
return;
62+
if (!proj.projectFile.project.package.definition)
63+
return;
64+
var outFile = path.resolve(proj.projectFile.project.package.directory, './', proj.projectFile.project.package.definition);
65+
var baseDir = proj.projectFile.project.package.directory;
66+
var name = proj.projectFile.project.package.name;
67+
var name = proj.projectFile.project.package.name;
68+
dts.generate({
69+
baseDir: baseDir,
70+
files: proj.projectFile.project.files,
71+
name: name,
72+
target: proj.projectFile.project.compilerOptions.target,
73+
out: outFile,
74+
});
75+
}
76+
exports.emitDts = emitDts;

Diff for: dist/main/lang/projectService.js

+1
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ function build(query) {
220220
});
221221
return output;
222222
});
223+
building.emitDts(proj);
223224
return resolve({
224225
buildOutput: {
225226
outputs: outputs,

Diff for: dist/main/tsconfig/dts-generator.js

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/**
2+
* All rights : https://github.com/SitePen/dts-generator
3+
* I needed to rework it to work with package.json + tsconfig.json
4+
*/
5+
var ts = require("typescript");
6+
var pathUtil = require("path");
7+
var os = require("os");
8+
var fs = require("fs");
9+
var mkdirp = require("mkdirp");
10+
var filenameToMid = (function () {
11+
if (pathUtil.sep === '/') {
12+
return function (filename) {
13+
return filename;
14+
};
15+
}
16+
else {
17+
var separatorExpression = new RegExp(pathUtil.sep.replace('\\', '\\\\'), 'g');
18+
return function (filename) {
19+
return filename.replace(separatorExpression, '/');
20+
};
21+
}
22+
})();
23+
function getError(diagnostics) {
24+
var message = 'Declaration generation failed';
25+
diagnostics.forEach(function (diagnostic) {
26+
var position = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
27+
message +=
28+
("\n" + diagnostic.file.fileName + "(" + (position.line + 1) + "," + (position.character + 1) + "): ") +
29+
("error TS" + diagnostic.code + ": " + diagnostic.messageText);
30+
});
31+
var error = new Error(message);
32+
error.name = 'EmitterError';
33+
return error;
34+
}
35+
function getFilenames(baseDir, files) {
36+
return files.map(function (filename) {
37+
var resolvedFilename = pathUtil.resolve(filename);
38+
if (resolvedFilename.indexOf(baseDir) === 0) {
39+
return resolvedFilename;
40+
}
41+
return pathUtil.resolve(baseDir, filename);
42+
});
43+
}
44+
function processTree(sourceFile, replacer) {
45+
var code = '';
46+
var cursorPosition = 0;
47+
function skip(node) {
48+
cursorPosition = node.end;
49+
}
50+
function readThrough(node) {
51+
code += sourceFile.text.slice(cursorPosition, node.pos);
52+
cursorPosition = node.pos;
53+
}
54+
function visit(node) {
55+
readThrough(node);
56+
var replacement = replacer(node);
57+
if (replacement != null) {
58+
code += replacement;
59+
skip(node);
60+
}
61+
else {
62+
ts.forEachChild(node, visit);
63+
}
64+
}
65+
visit(sourceFile);
66+
code += sourceFile.text.slice(cursorPosition);
67+
return code;
68+
}
69+
function generate(options, sendMessage) {
70+
if (sendMessage === void 0) { sendMessage = function () { }; }
71+
var baseDir = pathUtil.resolve(options.baseDir);
72+
var eol = options.eol || os.EOL;
73+
var nonEmptyLineStart = new RegExp(eol + '(?!' + eol + '|$)', 'g');
74+
var indent = options.indent === undefined ? '\t' : options.indent;
75+
var target = options.target || 2;
76+
var compilerOptions = {
77+
declaration: true,
78+
module: 1,
79+
target: target
80+
};
81+
var filenames = getFilenames(baseDir, options.files);
82+
var excludesMap = {};
83+
options.excludes && options.excludes.forEach(function (filename) {
84+
excludesMap[pathUtil.resolve(baseDir, filename)] = true;
85+
});
86+
mkdirp.sync(pathUtil.dirname(options.out));
87+
var output = fs.createWriteStream(options.out, { mode: parseInt('644', 8) });
88+
var host = ts.createCompilerHost(compilerOptions);
89+
var program = ts.createProgram(filenames, compilerOptions, host);
90+
var checker = ts.createTypeChecker(program, true);
91+
function writeFile(filename, data, writeByteOrderMark) {
92+
if (filename.slice(-5) !== '.d.ts') {
93+
return;
94+
}
95+
writeDeclaration(ts.createSourceFile(filename, data, target, true));
96+
}
97+
return new Promise(function (resolve, reject) {
98+
output.on('close', function () { resolve(undefined); });
99+
output.on('error', reject);
100+
if (options.externs) {
101+
options.externs.forEach(function (path) {
102+
sendMessage("Writing external dependency " + path);
103+
output.write(("/// <reference path=\"" + path + "\" />") + eol);
104+
});
105+
}
106+
program.getSourceFiles().some(function (sourceFile) {
107+
if (pathUtil.normalize(sourceFile.fileName).indexOf(baseDir) !== 0) {
108+
return;
109+
}
110+
if (excludesMap[sourceFile.fileName]) {
111+
return;
112+
}
113+
sendMessage("Processing " + sourceFile.fileName);
114+
if (sourceFile.fileName.slice(-5) === '.d.ts') {
115+
writeDeclaration(sourceFile);
116+
return;
117+
}
118+
var emitOutput = program.emit(sourceFile, writeFile);
119+
if (emitOutput.emitSkipped) {
120+
reject(getError(emitOutput.diagnostics
121+
.concat(program.getSemanticDiagnostics(sourceFile))
122+
.concat(program.getSyntacticDiagnostics(sourceFile))
123+
.concat(program.getDeclarationDiagnostics(sourceFile))));
124+
return true;
125+
}
126+
});
127+
if (options.main) {
128+
output.write(("declare module '" + options.name + "' {") + eol + indent);
129+
output.write(("import main = require('" + options.main + "');") + eol + indent);
130+
output.write('export = main;' + eol);
131+
output.write('}' + eol);
132+
sendMessage("Aliased main module " + options.name + " to " + options.main);
133+
}
134+
output.end();
135+
});
136+
function writeDeclaration(declarationFile) {
137+
var filename = declarationFile.fileName;
138+
var sourceModuleId = options.name + filenameToMid(filename.slice(baseDir.length, -5));
139+
if (declarationFile.externalModuleIndicator) {
140+
output.write('declare module \'' + sourceModuleId + '\' {' + eol + indent);
141+
var content = processTree(declarationFile, function (node) {
142+
if (node.kind === 219) {
143+
var expression = node.expression;
144+
if (expression.text.charAt(0) === '.') {
145+
return ' require(\'' + pathUtil.join(pathUtil.dirname(sourceModuleId), expression.text) + '\')';
146+
}
147+
}
148+
else if (node.kind === 115) {
149+
return '';
150+
}
151+
else if (node.kind === 8 &&
152+
(node.parent.kind === 215 || node.parent.kind === 209)) {
153+
var text = node.text;
154+
if (text.charAt(0) === '.') {
155+
return " '" + pathUtil.join(pathUtil.dirname(sourceModuleId), text) + "'";
156+
}
157+
}
158+
});
159+
output.write(content.replace(nonEmptyLineStart, '$&' + indent));
160+
output.write(eol + '}' + eol);
161+
}
162+
else {
163+
output.write(declarationFile.text);
164+
}
165+
}
166+
}
167+
exports.generate = generate;

Diff for: dist/main/tsconfig/tsconfig.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function getDefaultProject(srcFile) {
152152
return {
153153
projectFileDirectory: dir,
154154
projectFilePath: dir + '/' + projectFileName,
155-
project: project
155+
project: project,
156156
};
157157
}
158158
exports.getDefaultProject = getDefaultProject;
@@ -204,12 +204,24 @@ function getProjectSync(pathOrSrcFile) {
204204
}
205205
}
206206
projectSpec.files = projectSpec.files.map(function (file) { return path.resolve(projectFileDirectory, file); });
207+
var packagePath = projectSpec.package;
208+
var package = null;
209+
if (packagePath) {
210+
var packageJSONPath = getPotentiallyRelativeFile(projectFileDirectory, packagePath);
211+
var parsedPackage = JSON.parse(fs.readFileSync(packageJSONPath).toString());
212+
package = {
213+
name: parsedPackage.name,
214+
directory: path.dirname(packageJSONPath),
215+
definition: parsedPackage.typescript && parsedPackage.typescript.definition
216+
};
217+
}
207218
var project = {
208219
compilerOptions: {},
209220
files: projectSpec.files,
210221
filesGlob: projectSpec.filesGlob,
211222
formatCodeOptions: formatting.makeFormatCodeOptions(projectSpec.formatCodeOptions),
212-
compileOnSave: projectSpec.compileOnSave == undefined ? true : projectSpec.compileOnSave
223+
compileOnSave: projectSpec.compileOnSave == undefined ? true : projectSpec.compileOnSave,
224+
package: package
213225
};
214226
var validationResult = validator.validate(projectSpec.compilerOptions);
215227
if (validationResult.errorMessage) {
@@ -378,3 +390,10 @@ function travelUpTheDirectoryTreeTillYouFindFile(dir, fileName) {
378390
}
379391
}
380392
exports.travelUpTheDirectoryTreeTillYouFindFile = travelUpTheDirectoryTreeTillYouFindFile;
393+
function getPotentiallyRelativeFile(basePath, filePath) {
394+
if (pathIsRelative(filePath)) {
395+
return consistentPath(path.resolve(basePath, filePath));
396+
}
397+
return consistentPath(filePath);
398+
}
399+
exports.getPotentiallyRelativeFile = getPotentiallyRelativeFile;

Diff for: dist/worker/debug.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.debug = false;

Diff for: dist/worker/parent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///ts:ref=globals
22
/// <reference path="../globals.ts"/> ///ts:ref:generated
3-
var debug = false;
3+
var debug_1 = require("./debug");
44
var childprocess = require('child_process');
55
var exec = childprocess.exec;
66
var spawn = childprocess.spawn;
@@ -12,7 +12,7 @@ parent.pendingRequestsChanged = function (pending) {
1212
return;
1313
mainPanel.panelView.updatePendingRequests(pending);
1414
};
15-
if (debug) {
15+
if (debug_1.debug) {
1616
parent.sendToIpc = function (x) { return x; };
1717
}
1818
function startWorker() {

Diff for: lib/main/atom/commands/commands.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ export function registerCommands() {
305305
old_pane.destroyItem(old_pane.itemForUri(uri));
306306
}
307307
atom.workspace.open(uri, {
308-
text: atom.workspace.getActiveEditor().getText(),
308+
text: atom.workspace.getActiveTextEditor().getText(),
309309
filePath: atomUtils.getCurrentPath()
310310
});
311311
});
@@ -319,7 +319,7 @@ export function registerCommands() {
319319
old_pane.destroyItem(old_pane.itemForUri(uri));
320320
}
321321
atom.workspace.open(uri, {
322-
text: atom.workspace.getActiveEditor().getText(),
322+
text: atom.workspace.getActiveTextEditor().getText(),
323323
filePath: atomUtils.getCurrentPath()
324324
});
325325
});

Diff for: lib/main/atom/commands/outputFileCommands.ts

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ export function register() {
4343
return;
4444
}
4545
else {
46+
// spawn('cmd', ['/C', 'start ' + "node " + res.output.outputFiles[0].name]);
47+
4648
exec("node " + res.output.outputFiles[0].name, (err, stdout, stderr) => {
4749
console.log(stdout);
4850
if (stderr.toString().trim().length) {

Diff for: lib/main/atom/gotoHistory.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export var activeList: TabWithGotoPositions = errorsInOpenFiles;
88

99
export function gotoLine(filePath: string, line: number, col: number, list: TabWithGotoPositions) {
1010
var activeFile,
11-
activeEditor = atom.workspace.getActiveEditor();
11+
activeEditor = atom.workspace.getActiveTextEditor();
1212
if (activeEditor !== undefined && activeEditor !== null) {
1313
activeFile = activeEditor.getPath();
1414
}
@@ -19,7 +19,7 @@ export function gotoLine(filePath: string, line: number, col: number, list: TabW
1919
initialColumn: col
2020
});
2121
} else {
22-
atom.workspace.getActiveEditor().cursors[0].setBufferPosition([line - 1, col]);
22+
atom.workspace.getActiveTextEditor().cursors[0].setBufferPosition([line - 1, col]);
2323
}
2424

2525
list.lastPosition = { filePath, line, col };

Diff for: lib/main/atom/views/documentationView.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class DocumentationView extends view.View<any> {
2828
}
2929

3030
autoPosition() {
31-
var editor = atom.workspace.getActiveEditor();
31+
var editor = atom.workspace.getActiveTextEditor();
3232
var cursor = editor.getCursor();
3333
var cursorTop = cursor.getPixelRect().top - editor.getScrollTop();
3434
var editorHeight = editor.getHeight();

Diff for: lib/main/atom/views/renameView.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class RenameView
7575

7676
public renameThis(options: RenameViewOptions) {
7777
this.options = options;
78-
this.editorAtRenameStart = atom.workspace.getActiveEditor();
78+
this.editorAtRenameStart = atom.workspace.getActiveTextEditor();
7979
panel.show();
8080

8181
this.newNameEditor.model.setText(options.text);

0 commit comments

Comments
 (0)