feat(typescript): add delcare modifier support for vars, classes and functions (#1436)

master
Lucas Azzola 2017-04-28 02:37:42 +10:00 committed by Christopher Chedeau
parent 4b3835925e
commit 7a02e9ad27
8 changed files with 145 additions and 14 deletions

View File

@ -332,7 +332,11 @@ function genericPrintNoParens(path, options, print, args) {
]);
case "FunctionDeclaration":
case "FunctionExpression":
return printFunctionDeclaration(path, print, options);
if (isNodeStartingWithDeclare(n, options)) {
parts.push("declare ");
}
parts.push(printFunctionDeclaration(path, print, options));
return concat(parts);
case "ArrowFunctionExpression": {
if (n.async) parts.push("async ");
@ -996,6 +1000,7 @@ function genericPrintNoParens(path, options, print, args) {
}, "declarations");
parts = [
isNodeStartingWithDeclare(n, options) ? "declare " : "",
n.kind,
" ",
printed[0],
@ -1476,7 +1481,11 @@ function genericPrintNoParens(path, options, print, args) {
case "ClassDeclaration":
case "ClassExpression":
case "TSAbstractClassDeclaration":
return concat(printClass(path, options, print));
if (isNodeStartingWithDeclare(n, options)) {
parts.push("declare ");
}
parts.push(concat(printClass(path, options, print)));
return concat(parts);
case "TemplateElement":
return join(literalline, n.value.raw.split("\n"));
case "TemplateLiteral":
@ -1684,7 +1693,7 @@ function genericPrintNoParens(path, options, print, args) {
case "InterfaceDeclaration": {
if (
n.type === "DeclareInterface" ||
isFlowNodeStartingWithDeclare(n, options)
isNodeStartingWithDeclare(n, options)
) {
parts.push("declare ");
}
@ -1823,7 +1832,7 @@ function genericPrintNoParens(path, options, print, args) {
case "TypeAlias": {
if (
n.type === "DeclareTypeAlias" ||
isFlowNodeStartingWithDeclare(n, options)
isNodeStartingWithDeclare(n, options)
) {
parts.push("declare ");
}
@ -1933,7 +1942,7 @@ function genericPrintNoParens(path, options, print, args) {
return concat([path.call(print, "elementType"), "[]"]);
case "TSPropertySignature":
parts.push(path.call(print, "name"));
parts.push(": ")
parts.push(": ");
parts.push(path.call(print, "typeAnnotation"));
return concat(parts);
@ -2133,7 +2142,6 @@ function printStatementSequence(path, options, print) {
parts.push(stmtPrinted);
}
if (!options.semi && isClass) {
if (classPropMayCauseASIProblems(stmtPath)) {
parts.push(";");
@ -2562,7 +2570,7 @@ function printReturnType(path, print) {
// prepend colon to TypeScript type annotation
if (n.returnType && n.returnType.typeAnnotation) {
parts.unshift(": ")
parts.unshift(": ");
}
if (n.predicate) {
@ -3658,14 +3666,16 @@ function isTypeAnnotationAFunction(node) {
);
}
function isFlowNodeStartingWithDeclare(node, options) {
if (options.parser !== "flow") {
function isNodeStartingWithDeclare(node, options) {
if (!(options.parser === "flow" || options.parser === "typescript")) {
return false;
}
return options.originalText
.slice(0, util.locStart(node))
.match(/declare\s*$/);
return (
options.originalText.slice(0, util.locStart(node)).match(/declare\s*$/) ||
options.originalText
.slice(node.range[0], node.range[1])
.startsWith("declare ")
);
}
function printArrayItems(path, options, printPath, print) {

View File

@ -0,0 +1,58 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ambientDeclarations.ts 1`] = `
declare var n;
declare var m: string;
declare function fn1();
declare function fn2(n: string): number;
declare function fn3(n: string): number;
declare function fn4(n: number, y: number): string;
declare function fn5(x, y?);
declare function fn6(e?);
declare function fn7(x, y?, ...z);
declare function fn8(y?, ...z: number[]);
declare function fn9(...q: {}[]);
declare function fn10<T>(...q: T[]);
declare class cls {
constructor();
method(): cls;
static static(p): number;
static q;
private fn();
private static fns();
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare var n;
declare var m: string;
declare function fn1()
declare function fn2(n: string): number
declare function fn3(n: string): number
declare function fn4(n: number, y: number): string
declare function fn5(x, y?)
declare function fn6(e?)
declare function fn7(x, y?, ...z)
declare function fn8(y?, ...z: number[])
declare function fn9(...q: {}[])
declare function fn10<T>(...q: T[])
declare class cls {
constructor();
method(): cls;
static static(p): number;
static q;
private fn();
static private fns();
}
`;

View File

@ -0,0 +1,26 @@
declare var n;
declare var m: string;
declare function fn1();
declare function fn2(n: string): number;
declare function fn3(n: string): number;
declare function fn4(n: number, y: number): string;
declare function fn5(x, y?);
declare function fn6(e?);
declare function fn7(x, y?, ...z);
declare function fn8(y?, ...z: number[]);
declare function fn9(...q: {}[]);
declare function fn10<T>(...q: T[]);
declare class cls {
constructor();
method(): cls;
static static(p): number;
static q;
private fn();
private static fns();
}

View File

@ -0,0 +1 @@
run_spec(__dirname, { parser: "typescript" });

View File

@ -5,7 +5,7 @@ declare class MyArray<T> extends Array<T> {
sort(compareFn?: (a: T, b: T) => number): this;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class MyArray<T> extends Array {
declare class MyArray<T> extends Array {
sort(compareFn?: (a: T, b: T) => number): this;
}

View File

@ -0,0 +1,25 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`declareModifier.d.ts 1`] = `
declare function a();
declare const b: string;
declare var c: number;
declare let d: any;
declare class e {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
declare function a()
declare const b: string;
declare var c: number;
declare let d: any;
declare class e {}
`;

View File

@ -0,0 +1,10 @@
declare function a();
declare const b: string;
declare var c: number;
declare let d: any;
declare class e {}

View File

@ -0,0 +1 @@
run_spec(__dirname, { parser: "typescript" });