Update to latest typescript
This commit is contained in:
parent
9384e93e94
commit
9f0dd17419
4 changed files with 1393 additions and 1682 deletions
|
@ -12,7 +12,13 @@ export default class Example0 {
|
||||||
* Example0.get() + 1 // => 43
|
* Example0.get() + 1 // => 43
|
||||||
*/
|
*/
|
||||||
private static get(){
|
private static get(){
|
||||||
|
// a comment
|
||||||
|
// @ts-ignore
|
||||||
return 42
|
return 42
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public testMore(){
|
||||||
|
return ({} as any) ?.xyz?.abc ?? 0
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
import * as ts from 'typescript'
|
import * as ts from 'typescript'
|
||||||
import {SyntaxKind} from 'typescript'
|
import {JSDocComment} from 'typescript'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
|
|
||||||
|
@ -31,7 +31,20 @@ export interface Comment {
|
||||||
|
|
||||||
export function Comments(s: string): Comment[] {
|
export function Comments(s: string): Comment[] {
|
||||||
const out: Comment[] = []
|
const out: Comment[] = []
|
||||||
|
function registerComment(context: string | null,comment: string | ts.NodeArray<JSDocComment> | undefined){
|
||||||
|
if(comment === undefined){
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if(typeof comment === "string"){
|
||||||
|
out.push({comment: comment || '', context});
|
||||||
|
}else{
|
||||||
|
comment.forEach(jsDocComment => {
|
||||||
|
out.push({comment: jsDocComment.text || '', context});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function traverse(node: ts.Node) {
|
function traverse(node: ts.Node) {
|
||||||
const jsdocs = (node as any).jsDoc || []
|
const jsdocs = (node as any).jsDoc || []
|
||||||
if (jsdocs.length > 0) {
|
if (jsdocs.length > 0) {
|
||||||
|
@ -45,18 +58,17 @@ export function Comments(s: string): Comment[] {
|
||||||
context = decls[0].name.escapedText || null
|
context = decls[0].name.escapedText || null
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// console.dir(node)
|
|
||||||
context = ts.isConstructorDeclaration(node) ? 'constructor' : null
|
context = ts.isConstructorDeclaration(node) ? 'constructor' : null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
jsdocs.forEach((doc: ts.JSDoc) => {
|
jsdocs.forEach((doc: ts.JSDoc) => {
|
||||||
out.push({comment: doc.comment || '', context});
|
registerComment(context, doc.comment)
|
||||||
|
|
||||||
// A part of the comment might be in the tags; we simply add those too and figure out later if they contain doctests
|
// A part of the comment might be in the tags; we simply add those too and figure out later if they contain doctests
|
||||||
const tags = doc.tags;
|
const tags = doc.tags;
|
||||||
if(tags !== undefined){
|
if(tags !== undefined){
|
||||||
tags.forEach(tag => {
|
tags.forEach(tag => {
|
||||||
out.push({comment: tag.comment || '', context});
|
registerComment(context, tag.comment)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -64,7 +76,7 @@ export function Comments(s: string): Comment[] {
|
||||||
}
|
}
|
||||||
ts.forEachChild(node, traverse)
|
ts.forEachChild(node, traverse)
|
||||||
}
|
}
|
||||||
|
|
||||||
const ast = ts.createSourceFile('_.ts', s, ts.ScriptTarget.Latest)
|
const ast = ts.createSourceFile('_.ts', s, ts.ScriptTarget.Latest)
|
||||||
traverse(ast)
|
traverse(ast)
|
||||||
|
|
||||||
|
@ -228,7 +240,7 @@ function exposePrivates(s: string): string {
|
||||||
|
|
||||||
const transformed = ts.transform(ast, [transformer]).transformed[0]
|
const transformed = ts.transform(ast, [transformer]).transformed[0]
|
||||||
|
|
||||||
const pwoc = ts.createPrinter({removeComments: true})
|
const pwoc = ts.createPrinter({ removeComments: false})
|
||||||
return pwoc.printNode(ts.EmitHint.Unspecified, transformed, ast)
|
return pwoc.printNode(ts.EmitHint.Unspecified, transformed, ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
18
test/test.ts
18
test/test.ts
|
@ -46,7 +46,7 @@ test('modules and namespace', t => {
|
||||||
/** ns */
|
/** ns */
|
||||||
namespace ns {}
|
namespace ns {}
|
||||||
`)
|
`)
|
||||||
t.deepEqual(cs, [c('m ', 'm'), c('ns ', 'ns')])
|
t.deepEqual(cs, [c('m', 'm'), c('ns', 'ns')])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('const', t => {
|
test('const', t => {
|
||||||
|
@ -55,7 +55,7 @@ test('const', t => {
|
||||||
/** u */
|
/** u */
|
||||||
const u = 1
|
const u = 1
|
||||||
`)
|
`)
|
||||||
t.deepEqual(cs, [c('u ', 'u')])
|
t.deepEqual(cs, [c('u', 'u')])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('const object', t => {
|
test('const object', t => {
|
||||||
|
@ -69,7 +69,7 @@ test('const object', t => {
|
||||||
b(x: string) { return x+x }
|
b(x: string) { return x+x }
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
t.deepEqual(cs, [c('k ', 'k'), c('a ', 'a'), c('b ', 'b')])
|
t.deepEqual(cs, [c('k', 'k'), c('a', 'a'), c('b', 'b')])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('object deconstruction', t => {
|
test('object deconstruction', t => {
|
||||||
|
@ -78,7 +78,7 @@ test('object deconstruction', t => {
|
||||||
/** hello */
|
/** hello */
|
||||||
const {u, v} = {u: 1, v: 2}
|
const {u, v} = {u: 1, v: 2}
|
||||||
`)
|
`)
|
||||||
t.deepEqual(cs, [c('hello ', null)])
|
t.deepEqual(cs, [c('hello', null)])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('function', t => {
|
test('function', t => {
|
||||||
|
@ -89,7 +89,7 @@ test('function', t => {
|
||||||
return s.length + 1
|
return s.length + 1
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
t.deepEqual(cs, [c('v ', 'v')])
|
t.deepEqual(cs, [c('v', 'v')])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('class', t => {
|
test('class', t => {
|
||||||
|
@ -106,7 +106,7 @@ test('class', t => {
|
||||||
p: Array<number>
|
p: Array<number>
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
t.deepEqual(cs, [c('C ', 'C'), c('constructor ', 'constructor'), c('m ', 'm'), c('p ', 'p')])
|
t.deepEqual(cs, [c('C', 'C'), c('constructor', 'constructor'), c('m', 'm'), c('p', 'p')])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('interface', t => {
|
test('interface', t => {
|
||||||
|
@ -120,7 +120,7 @@ test('interface', t => {
|
||||||
j(a: A): string
|
j(a: A): string
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
t.deepEqual(cs, [c('I ', 'I'), c('i ', 'i'), c('j ', 'j')])
|
t.deepEqual(cs, [c('I', 'I'), c('i', 'i'), c('j', 'j')])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('type', t => {
|
test('type', t => {
|
||||||
|
@ -129,7 +129,7 @@ test('type', t => {
|
||||||
/** T */
|
/** T */
|
||||||
type T = number
|
type T = number
|
||||||
`)
|
`)
|
||||||
t.deepEqual(cs, [c('T ', 'T')])
|
t.deepEqual(cs, [c('T', 'T')])
|
||||||
})
|
})
|
||||||
|
|
||||||
test('anywhere', t => {
|
test('anywhere', t => {
|
||||||
|
@ -148,5 +148,5 @@ test('anywhere', t => {
|
||||||
return f(f(w))
|
return f(f(w))
|
||||||
}
|
}
|
||||||
`)
|
`)
|
||||||
t.deepEqual(cs, [c('test1 ', 'w'), c('test2 ', 'f')])
|
t.deepEqual(cs, [c('test1', 'w'), c('test2', 'f'), c('test3', null)])
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue