forked from jashkenas/coffeescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add script to check typescript against jsdoc and map back to coffee
- Loading branch information
1 parent
817c39a
commit 30798b6
Showing
5 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
### | ||
jsdoc: | ||
/** | ||
* @type {(x: number) => number} | ||
* / | ||
var f; | ||
/** | ||
* @param {number} x | ||
* @returns {number} | ||
* / | ||
f = function(x) { | ||
return x + 3; | ||
} | ||
.d.ts: | ||
/** | ||
* @type {(x: number) => number} | ||
* / | ||
declare var f: (x: number) => number; | ||
### | ||
# f<[=> number]> = (x<[number]>) -> x + 3 | ||
# f<[(x: number) => number]> = (x) -> x + 3 | ||
f = (x) -> x + 3 | ||
|
||
### | ||
jsdoc: | ||
/** | ||
* @type {{a: number}} | ||
* / | ||
var x; | ||
x = { | ||
a: 3 | ||
}; | ||
.d.ts: | ||
/** | ||
* @type {{a: number}} | ||
* / | ||
declare var x: { | ||
a: number; | ||
}; | ||
### | ||
# x = {a<[number]>: 3} | ||
# x<[{a: number}]> = {a: 3} | ||
x = {a: 3} | ||
|
||
### | ||
jsdoc: | ||
/** | ||
* @type {({a}: {a: string}) => string} | ||
* / | ||
var g; | ||
/** | ||
* @param {{a: string}} _ | ||
* @returns {string} | ||
* / | ||
g = function({a: x}) { | ||
return x; | ||
} | ||
.d.ts: | ||
/** | ||
* @type {({a}: {a: string}) => string} | ||
* / | ||
declare var g: ({ a }: { | ||
a: string; | ||
}) => string; | ||
### | ||
# g<[=> string]> = ({a<[string]>: x}) -> x | ||
# this one does not rename the field: | ||
# g<[=> string]> = ({a<[string]>}) -> a | ||
g = ({a: x}) -> x | ||
|
||
### | ||
jsdoc: | ||
/** | ||
* @type {({a, b, c, e}: {a: number, b?: number, c: {d?: number}, e: [f: number]}) => number} | ||
* / | ||
var h; | ||
/** | ||
* @param {{a: number, b?: number, c: {d?: number}, e: [f: number]}} _ | ||
* @returns number | ||
* / | ||
h = function({a, b = 3, c: {d = 3}, e: [f]}) { | ||
return a + b + d; | ||
}; | ||
.d.ts: | ||
/** | ||
* @type {({a, b, c, e}: {a: number, b?: number, c: {d?: number}, e: [f: number]}) => number} | ||
* / | ||
declare var h: ({ a, b, c, e }: { | ||
a: number; | ||
b?: number; | ||
c: { | ||
d?: number; | ||
}; | ||
e: [f: number]; | ||
}) => number; | ||
### | ||
# h<[=> number]> = ({a<[number]>, b<[?number]> = 3, c: {d<[?number]> = 3}, e: [f<[number]>]}) -> | ||
# a + b + d | ||
h = ({a, b = 3, c: {d = 3}, e: [f]}) -> | ||
a + b + d + f |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
assert = require 'assert' | ||
fs = require 'fs' | ||
path = require 'path' | ||
|
||
coffee = require './lib/coffeescript/coffeescript.js' | ||
ts = require 'typescript' | ||
|
||
coffeeCompileWithSourceMap = (fileNames) -> | ||
for f in fileNames | ||
assert f.endsWith '.coffee' | ||
out = f.replace /\.coffee$/, '.js' | ||
mapOut = "#{out}.map" | ||
code = fs.readFileSync f, 'utf8' | ||
{js, v3SourceMap, sourceMap} = coffee.compile code, | ||
filename: f | ||
generatedFile: out | ||
bare: yes | ||
header: no | ||
sourceMap: yes | ||
sourceRoot: process.cwd() | ||
fs.writeFileSync out, "#{js}\n//# sourceMappingURL=#{mapOut}\n" | ||
fs.writeFileSync mapOut, "#{v3SourceMap}\n" | ||
{ | ||
original: f | ||
opath: path.resolve f | ||
code: code | ||
codeLines: code.split '\n' | ||
js: out | ||
mapFile: mapOut | ||
map: sourceMap | ||
} | ||
|
||
compiled = coffeeCompileWithSourceMap process.argv[2..] | ||
byJsPath = {} | ||
for {js, original, opath, code, codeLines, map} in compiled | ||
byJsPath[path.resolve js] = {original, opath, code, codeLines, map} | ||
|
||
generateMappedCoffeeSource = (name, filePath, content) -> | ||
{ | ||
...(ts.createSourceFile name, content, ts.ScriptTarget.Latest, no, ts.ScriptKind.Unknown), | ||
statements: [], | ||
parseDiagnostics: [], | ||
identifiers: new Map, | ||
path: filePath, | ||
resolvedPath: filePath, | ||
originalFileName: name | ||
} | ||
|
||
locationToOffset = (contentLines, line, column) -> | ||
offset = 0 | ||
for i in [0...line] | ||
cur = contentLines[i] | ||
# NB: add 1 for newline | ||
offset += cur.length + 1 | ||
offset += column | ||
offset | ||
|
||
mapSpan = (diag, map, contentLines, content) -> | ||
{line: startLine, character: startCol} = ts.getLineAndCharacterOfPosition diag.file, diag.start | ||
{line: endLine, character: endCol} = ts.getLineAndCharacterOfPosition diag.file, (diag.start + diag.length) | ||
|
||
[origStartLine, origStartCol] = map.sourceLocation [startLine, startCol] | ||
[origEndLine, origEndCol] = map.sourceLocation [endLine, endCol] | ||
|
||
lengthInferred = origEndLine == origStartLine and origEndCol == 0 | ||
|
||
origStart = locationToOffset contentLines, origStartLine, origStartCol | ||
|
||
origEnd = if lengthInferred then origStart + diag.length | ||
else locationToOffset contentLines, origEndLine, origEndCol | ||
|
||
{start: origStart, length: origEnd - origStart} | ||
|
||
checkMappedJsDoc = (mappedCompiled, options) -> | ||
fileNames = (k for own k of mappedCompiled) | ||
for f in fileNames | ||
assert f.endsWith '.js' | ||
program = ts.createProgram fileNames, options | ||
emitResult = program.emit() | ||
|
||
rawDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics) | ||
|
||
mappedDiagnostics = for diag in rawDiagnostics | ||
if not diag.file? then diag else | ||
{original, opath, code, codeLines, map} = mappedCompiled[diag.file.resolvedPath] | ||
realSource = generateMappedCoffeeSource original, opath, code | ||
{start: realStart, length: realLength} = mapSpan diag, map, codeLines, code | ||
{ | ||
...diag, | ||
file: realSource, | ||
start: realStart, | ||
length: realLength, | ||
} | ||
|
||
console.log ts.formatDiagnosticsWithColorAndContext mappedDiagnostics, | ||
getCurrentDirectory: -> process.cwd() | ||
getNewLine: -> '\n' | ||
getCanonicalFileName: (fileName) -> fileName | ||
|
||
exitCode = if emitResult.emitSkipped then 1 else 0 | ||
process.exit exitCode | ||
|
||
checkMappedJsDoc byJsPath, | ||
allowJs: yes | ||
checkJs: yes | ||
noEmit: yes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
###* | ||
* @type {(x: number) => number} | ||
### | ||
f = (x) -> x | ||
|
||
f("asdf") | ||
|
||
x = 3 |