From ab88423a4a4e48884ac4c8ddbf4f63c0ff5dfb0b Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Fri, 15 Nov 2019 00:06:45 +0800 Subject: [PATCH] Disable trailingComma for Angular internal parser (#6912) --- src/language-js/printer-estree.js | 10 + .../__snapshots__/jsfmt.spec.js.snap | 176 ++++++++++++++++++ tests/angular_interpolation/jsfmt.spec.js | 2 + 3 files changed, 188 insertions(+) diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 9c588dfb6..c9f73d0ca 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -120,6 +120,16 @@ const { let uid = 0; function shouldPrintComma(options, level) { + // angular does not allow trailing comma + if ( + options.parser === "__ng_interpolation" || + options.parser === "__ng_action" || + options.parser === "__ng_binding" || + options.parser === "__ng_directive" + ) { + return false; + } + level = level || "es5"; switch (options.trailingComma) { diff --git a/tests/angular_interpolation/__snapshots__/jsfmt.spec.js.snap b/tests/angular_interpolation/__snapshots__/jsfmt.spec.js.snap index 6a5d630a4..41c46208c 100644 --- a/tests/angular_interpolation/__snapshots__/jsfmt.spec.js.snap +++ b/tests/angular_interpolation/__snapshots__/jsfmt.spec.js.snap @@ -20,6 +20,48 @@ printWidth: 80 ================================================================================ `; +exports[`logical-expression.ng 2`] = ` +====================================options===================================== +parsers: ["__ng_interpolation"] +printWidth: 80 +trailingComma: "es5" + | printWidth +=====================================input====================================== +[ + advancedSearchService.patientInformationFieldsRow2 && advancedSearchService.patientInformationFieldsRow2.indexOf(advancedSearchService.formElementData.customFieldList[i].customFieldType) !== -1 +] + +=====================================output===================================== +[ + advancedSearchService.patientInformationFieldsRow2 && + advancedSearchService.patientInformationFieldsRow2.indexOf( + advancedSearchService.formElementData.customFieldList[i].customFieldType + ) !== -1 +] +================================================================================ +`; + +exports[`logical-expression.ng 3`] = ` +====================================options===================================== +parsers: ["__ng_interpolation"] +printWidth: 80 +trailingComma: "all" + | printWidth +=====================================input====================================== +[ + advancedSearchService.patientInformationFieldsRow2 && advancedSearchService.patientInformationFieldsRow2.indexOf(advancedSearchService.formElementData.customFieldList[i].customFieldType) !== -1 +] + +=====================================output===================================== +[ + advancedSearchService.patientInformationFieldsRow2 && + advancedSearchService.patientInformationFieldsRow2.indexOf( + advancedSearchService.formElementData.customFieldList[i].customFieldType + ) !== -1 +] +================================================================================ +`; + exports[`pipe-expression.ng 1`] = ` ====================================options===================================== parsers: ["__ng_interpolation"] @@ -85,3 +127,137 @@ printWidth: 80 ] ================================================================================ `; + +exports[`pipe-expression.ng 2`] = ` +====================================options===================================== +parsers: ["__ng_interpolation"] +printWidth: 80 +trailingComma: "es5" + | printWidth +=====================================input====================================== +[ + a ? (b | c : d) : (e | f : g), + a | b | c | d, + ((a | b) | c) | d, + a | b:(c | d), + { a: b | c }, + (a + b) | c, + (a | b) + c, + fn(a | b), + a?.b(c | d), + a[b | c], + ($students | async).items, + ($students | async)(), + myData | myPipe:'arg1':'arg2':'arg3', + value + | pipeA: { + keyA: reallySuperLongValue, + keyB: shortValue | pipeB | pipeC: valueToPipeC + } : { + keyA: reallySuperLongValue, + keyB: shortValue | pipeB | pipeC: valueToPipeC + } + | aaa, + (hideLinqPanel ? "ReportSelection.HideShowLabel_Show.String" : "ReportSelection.HideShowLabel_Hide.String") | localize:(localizationSection) +] + +=====================================output===================================== +[ + a ? (b | c: d) : (e | f: g), + a | b | c | d, + a | b | c | d, + a | b: (c | d), + { a: b | c }, + a + b | c, + (a | b) + c, + fn(a | b), + a?.b(c | d), + a[b | c], + ($students | async).items, + ($students | async)(), + myData | myPipe: "arg1":"arg2":"arg3", + value + | pipeA + : { + keyA: reallySuperLongValue, + keyB: shortValue | pipeB | pipeC: valueToPipeC + } + : { + keyA: reallySuperLongValue, + keyB: shortValue | pipeB | pipeC: valueToPipeC + } + | aaa, + (hideLinqPanel + ? "ReportSelection.HideShowLabel_Show.String" + : "ReportSelection.HideShowLabel_Hide.String" + ) | localize: localizationSection +] +================================================================================ +`; + +exports[`pipe-expression.ng 3`] = ` +====================================options===================================== +parsers: ["__ng_interpolation"] +printWidth: 80 +trailingComma: "all" + | printWidth +=====================================input====================================== +[ + a ? (b | c : d) : (e | f : g), + a | b | c | d, + ((a | b) | c) | d, + a | b:(c | d), + { a: b | c }, + (a + b) | c, + (a | b) + c, + fn(a | b), + a?.b(c | d), + a[b | c], + ($students | async).items, + ($students | async)(), + myData | myPipe:'arg1':'arg2':'arg3', + value + | pipeA: { + keyA: reallySuperLongValue, + keyB: shortValue | pipeB | pipeC: valueToPipeC + } : { + keyA: reallySuperLongValue, + keyB: shortValue | pipeB | pipeC: valueToPipeC + } + | aaa, + (hideLinqPanel ? "ReportSelection.HideShowLabel_Show.String" : "ReportSelection.HideShowLabel_Hide.String") | localize:(localizationSection) +] + +=====================================output===================================== +[ + a ? (b | c: d) : (e | f: g), + a | b | c | d, + a | b | c | d, + a | b: (c | d), + { a: b | c }, + a + b | c, + (a | b) + c, + fn(a | b), + a?.b(c | d), + a[b | c], + ($students | async).items, + ($students | async)(), + myData | myPipe: "arg1":"arg2":"arg3", + value + | pipeA + : { + keyA: reallySuperLongValue, + keyB: shortValue | pipeB | pipeC: valueToPipeC + } + : { + keyA: reallySuperLongValue, + keyB: shortValue | pipeB | pipeC: valueToPipeC + } + | aaa, + (hideLinqPanel + ? "ReportSelection.HideShowLabel_Show.String" + : "ReportSelection.HideShowLabel_Hide.String" + ) | localize: localizationSection +] +================================================================================ +`; diff --git a/tests/angular_interpolation/jsfmt.spec.js b/tests/angular_interpolation/jsfmt.spec.js index 09a639b7b..509a66203 100644 --- a/tests/angular_interpolation/jsfmt.spec.js +++ b/tests/angular_interpolation/jsfmt.spec.js @@ -1 +1,3 @@ run_spec(__dirname, ["__ng_interpolation"]); +run_spec(__dirname, ["__ng_interpolation"], { trailingComma: "es5" }); +run_spec(__dirname, ["__ng_interpolation"], { trailingComma: "all" });