From 02639da9a9a31133ae85b047d565c00fe16fc052 Mon Sep 17 00:00:00 2001 From: Lucas Azzola Date: Mon, 17 Jul 2017 23:31:42 +1000 Subject: [PATCH] Improve coverage and remove some dead code (#2504) * Improve test coverage * Clean up some un-used code * Lower patch requirements on codecov --- .codecov.yml | 9 + src/printer.js | 166 ++++-------------- .../__snapshots__/jsfmt.spec.js.snap | 12 +- tests/typescript_keywords/keywords.js | 6 +- .../__snapshots__/jsfmt.spec.js.snap | 11 ++ tests/typescript_module/namespace_function.ts | 3 + 6 files changed, 74 insertions(+), 133 deletions(-) create mode 100644 tests/typescript_module/namespace_function.ts diff --git a/.codecov.yml b/.codecov.yml index db247200..e22022cc 100644 --- a/.codecov.yml +++ b/.codecov.yml @@ -1 +1,10 @@ comment: off + +coverage: + status: + patch: + default: + target: 80% + project: + default: + target: auto diff --git a/src/printer.js b/src/printer.js index 3db182b2..4aba5504 100644 --- a/src/printer.js +++ b/src/printer.js @@ -575,49 +575,24 @@ function genericPrintNoParens(path, options, print, args) { return concat(parts); case "AwaitExpression": - parts.push("await"); - - if (n.all) { - parts.push("*"); - } - if (n.argument) { - parts.push(" ", path.call(print, "argument")); - } - - return concat(parts); + return concat(["await ", path.call(print, "argument")]); case "ImportSpecifier": - if (n.imported) { - if (n.importKind) { - parts.push(path.call(print, "importKind"), " "); - } + if (n.importKind) { + parts.push(path.call(print, "importKind"), " "); + } - parts.push(path.call(print, "imported")); + parts.push(path.call(print, "imported")); - if (n.local && n.local.name !== n.imported.name) { - parts.push(" as ", path.call(print, "local")); - } - } else if (n.id) { - parts.push(path.call(print, "id")); - - if (n.name) { - parts.push(" as ", path.call(print, "name")); - } + if (n.local && n.local.name !== n.imported.name) { + parts.push(" as ", path.call(print, "local")); } return concat(parts); case "ExportSpecifier": - if (n.local) { - parts.push(path.call(print, "local")); + parts.push(path.call(print, "local")); - if (n.exported && n.exported.name !== n.local.name) { - parts.push(" as ", path.call(print, "exported")); - } - } else if (n.id) { - parts.push(path.call(print, "id")); - - if (n.name) { - parts.push(" as ", path.call(print, "name")); - } + if (n.exported && n.exported.name !== n.local.name) { + parts.push(" as ", path.call(print, "exported")); } return concat(parts); @@ -637,22 +612,13 @@ function genericPrintNoParens(path, options, print, args) { } return path.call(print, "id"); - case "TSExportAssigment": { + case "TSExportAssigment": return concat(["export = ", path.call(print, "expression"), semi]); - } case "ExportDefaultDeclaration": case "ExportNamedDeclaration": return printExportDeclaration(path, options, print); case "ExportAllDeclaration": - parts.push("export *"); - - if (n.exported) { - parts.push(" as ", path.call(print, "exported")); - } - - parts.push(" from ", path.call(print, "source"), semi); - - return concat(parts); + return concat(["export * from ", path.call(print, "source"), semi]); case "ExportNamespaceSpecifier": case "ExportDefaultSpecifier": return path.call(print, "exported"); @@ -720,7 +686,7 @@ function genericPrintNoParens(path, options, print, args) { ); } - parts.push(" ", "from "); + parts.push(" from "); } else if ( (n.importKind && n.importKind === "type") || // import {} from 'x' @@ -1569,32 +1535,19 @@ function genericPrintNoParens(path, options, print, args) { path.call(print, "body") ]); case "TryStatement": - parts.push("try ", path.call(print, "block")); - - if (n.handler) { - parts.push(" ", path.call(print, "handler")); - } else if (n.handlers) { - path.each(handlerPath => { - parts.push(" ", print(handlerPath)); - }, "handlers"); - } - - if (n.finalizer) { - parts.push(" finally ", path.call(print, "finalizer")); - } - - return concat(parts); + return concat([ + "try ", + path.call(print, "block"), + n.handler ? concat([" ", path.call(print, "handler")]) : "", + n.finalizer ? concat([" finally ", path.call(print, "finalizer")]) : "" + ]); case "CatchClause": - parts.push("catch (", path.call(print, "param")); - - if (n.guard) { - // Note: esprima does not recognize conditional catch clauses. - parts.push(" if ", path.call(print, "guard")); - } - - parts.push(") ", path.call(print, "body")); - - return concat(parts); + return concat([ + "catch (", + path.call(print, "param"), + ") ", + path.call(print, "body") + ]); case "ThrowStatement": return concat(["throw ", path.call(print, "argument"), semi]); // Note: ignoring n.lexical because it has no printing consequences. @@ -1935,6 +1888,7 @@ function genericPrintNoParens(path, options, print, args) { return path.call(print, "typeAnnotation"); } + /* istanbul ignore next */ return ""; case "TSTupleType": case "TupleTypeAnnotation": { @@ -2374,20 +2328,18 @@ function genericPrintNoParens(path, options, print, args) { case "TSArrayType": return concat([path.call(print, "elementType"), "[]"]); case "TSPropertySignature": { - if (n.accessibility) { - parts.push(n.accessibility + " "); - } if (n.export) { parts.push("export "); } + if (n.accessibility) { + parts.push(n.accessibility + " "); + } if (n.static) { parts.push("static "); } - if (n.readonly) { parts.push("readonly "); } - if (n.computed) { parts.push("["); } @@ -2397,11 +2349,9 @@ function genericPrintNoParens(path, options, print, args) { if (n.computed) { parts.push("]"); } - if (n.optional) { parts.push("?"); } - if (n.typeAnnotation) { parts.push(": "); parts.push(path.call(print, "typeAnnotation")); @@ -2443,24 +2393,14 @@ function genericPrintNoParens(path, options, print, args) { } case "TSIndexSignature": { const parent = path.getParentNode(); - let printedParams = []; - if (n.params) { - printedParams = path.map(print, "params"); - } - if (n.parameters) { - printedParams = path.map(print, "parameters"); - } return concat([ - n.accessibility ? concat([n.accessibility, " "]) : "", n.export ? "export " : "", + n.accessibility ? concat([n.accessibility, " "]) : "", n.static ? "static " : "", n.readonly ? "readonly " : "", "[", path.call(print, "index"), - // This should only contain a single element, however TypeScript parses - // it using parseDelimitedList that uses commas as delimiter. - join(", ", printedParams), "]: ", path.call(print, "typeAnnotation"), parent.type === "ClassBody" ? semi : "" @@ -2568,18 +2508,10 @@ function genericPrintNoParens(path, options, print, args) { } return group(concat(parts)); case "TSNamespaceExportDeclaration": - if (n.declaration) { - parts.push( - "export ", - n.default ? "default " : "", - path.call(print, "declaration") - ); - } else { - parts.push("export as namespace ", path.call(print, "name")); + parts.push("export as namespace ", path.call(print, "name")); - if (options.semi) { - parts.push(";"); - } + if (options.semi) { + parts.push(";"); } return group(concat(parts)); @@ -2724,6 +2656,7 @@ function printStatementSequence(path, options, print) { // Just in case the AST has been modified to contain falsy // "statements," it's safer simply to skip them. + /* istanbul ignore if */ if (!stmt) { return; } @@ -2979,14 +2912,7 @@ function printArgumentsList(path, options, print) { function printFunctionTypeParameters(path, options, print) { const fun = path.getValue(); - const paramsFieldIsArray = Array.isArray(fun["typeParameters"]); - if (fun.typeParameters) { - // for TSFunctionType typeParameters is an array - // for FunctionTypeAnnotation it's a single node - if (paramsFieldIsArray) { - return concat("<", join(", ", path.map(print, "typeParameters")), ">"); - } return path.call(print, "typeParameters"); } return ""; @@ -3005,17 +2931,6 @@ function printFunctionParams(path, print, options, expandArg, printTypeParams) { printed = path.map(print, paramsField); } - if (fun.defaults) { - path.each(defExprPath => { - const i = defExprPath.getName(); - const p = printed[i]; - - if (p && defExprPath.getValue()) { - printed[i] = concat([p, " = ", print(defExprPath)]); - } - }, "defaults"); - } - if (fun.rest) { printed.push(concat(["...", path.call(print, "rest")])); } @@ -3339,11 +3254,10 @@ function getFlowVariance(path) { switch (variance) { case "plus": return "+"; - case "minus": return "-"; - default: + /* istanbul ignore next */ return variance; } } @@ -3403,9 +3317,6 @@ function printClass(path, options, print) { const n = path.getValue(); const parts = []; - if (n.accessibility) { - parts.push(n.accessibility + " "); - } if (n.type === "TSAbstractClassDeclaration") { parts.push("abstract "); } @@ -4558,9 +4469,6 @@ function exprNeedsASIProtection(node) { } function stmtNeedsASIProtection(path) { - if (!path) { - return false; - } const node = path.getNode(); if (node.type !== "ExpressionStatement") { @@ -4578,9 +4486,6 @@ function classPropMayCauseASIProblems(path) { } const name = node.key && node.key.name; - if (!name) { - return false; - } // this isn't actually possible yet with most parsers available today // so isn't properly tested yet. @@ -4628,6 +4533,7 @@ function classChildNeedsASIProtection(node) { } default: + /* istanbul ignore next */ return false; } } diff --git a/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap index ef7fe27d..88a810b5 100644 --- a/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_keywords/__snapshots__/jsfmt.spec.js.snap @@ -9,7 +9,11 @@ module Y3 { } // Apparently this parses :P - export private public protected static readonly async enum X { } + export private public protected static readonly abstract async enum X { } + + interface x { + export private static readonly [x: any]: any; + } } module Y4 { @@ -46,7 +50,11 @@ namespace Y3 { } // Apparently this parses :P - export private public protected static readonly async enum X {} + export private public protected static readonly abstract async enum X {} + + interface x { + export private static readonly [x: any]: any; + } } namespace Y4 { diff --git a/tests/typescript_keywords/keywords.js b/tests/typescript_keywords/keywords.js index 6880cf1d..9dd21028 100644 --- a/tests/typescript_keywords/keywords.js +++ b/tests/typescript_keywords/keywords.js @@ -6,7 +6,11 @@ module Y3 { } // Apparently this parses :P - export private public protected static readonly async enum X { } + export private public protected static readonly abstract async enum X { } + + interface x { + export private static readonly [x: any]: any; + } } module Y4 { diff --git a/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap index 6ec7da40..4f943172 100644 --- a/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_module/__snapshots__/jsfmt.spec.js.snap @@ -27,3 +27,14 @@ declare global { } `; + +exports[`namespace_function.ts 1`] = ` +namespace X { + declare function f(); +} +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +namespace X { + declare function f(); +} + +`; diff --git a/tests/typescript_module/namespace_function.ts b/tests/typescript_module/namespace_function.ts new file mode 100644 index 00000000..d40ba042 --- /dev/null +++ b/tests/typescript_module/namespace_function.ts @@ -0,0 +1,3 @@ +namespace X { + declare function f(); +}