diff --git a/src/printer.js b/src/printer.js index bf8234f1..52f493c8 100644 --- a/src/printer.js +++ b/src/printer.js @@ -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) { diff --git a/tests/typescript/conformance/ambient/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/ambient/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..ad119aa1 --- /dev/null +++ b/tests/typescript/conformance/ambient/__snapshots__/jsfmt.spec.js.snap @@ -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(...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(...q: T[]) + +declare class cls { + constructor(); + method(): cls; + static static(p): number; + static q; + private fn(); + static private fns(); +} + +`; diff --git a/tests/typescript/conformance/ambient/ambientDeclarations.ts b/tests/typescript/conformance/ambient/ambientDeclarations.ts new file mode 100644 index 00000000..9cc2cf20 --- /dev/null +++ b/tests/typescript/conformance/ambient/ambientDeclarations.ts @@ -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(...q: T[]); + +declare class cls { + constructor(); + method(): cls; + static static(p): number; + static q; + private fn(); + private static fns(); +} diff --git a/tests/typescript/conformance/ambient/jsfmt.spec.js b/tests/typescript/conformance/ambient/jsfmt.spec.js new file mode 100644 index 00000000..bc085c48 --- /dev/null +++ b/tests/typescript/conformance/ambient/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" }); diff --git a/tests/typescript/conformance/types/thisType/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/conformance/types/thisType/__snapshots__/jsfmt.spec.js.snap index 9d1319ff..4a037b5d 100644 --- a/tests/typescript/conformance/types/thisType/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript/conformance/types/thisType/__snapshots__/jsfmt.spec.js.snap @@ -5,7 +5,7 @@ declare class MyArray extends Array { sort(compareFn?: (a: T, b: T) => number): this; } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -class MyArray extends Array { +declare class MyArray extends Array { sort(compareFn?: (a: T, b: T) => number): this; } diff --git a/tests/typescript/custom/declare/__snapshots__/jsfmt.spec.js.snap b/tests/typescript/custom/declare/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 00000000..5a14f2b5 --- /dev/null +++ b/tests/typescript/custom/declare/__snapshots__/jsfmt.spec.js.snap @@ -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 {} + +`; diff --git a/tests/typescript/custom/declare/declareModifier.d.ts b/tests/typescript/custom/declare/declareModifier.d.ts new file mode 100644 index 00000000..6c2d3628 --- /dev/null +++ b/tests/typescript/custom/declare/declareModifier.d.ts @@ -0,0 +1,10 @@ + +declare function a(); + +declare const b: string; + +declare var c: number; + +declare let d: any; + +declare class e {} diff --git a/tests/typescript/custom/declare/jsfmt.spec.js b/tests/typescript/custom/declare/jsfmt.spec.js new file mode 100644 index 00000000..bc085c48 --- /dev/null +++ b/tests/typescript/custom/declare/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname, { parser: "typescript" });