From 088dd9fc61af33d86fcb2f2d6a6a013050960368 Mon Sep 17 00:00:00 2001 From: "Benjamin E. Coe" Date: Thu, 31 May 2018 16:18:21 -0700 Subject: [PATCH] feat: add support for optional catch binding (#175) --- .../src/instrumenter.js | 1 + .../src/read-coverage.js | 1 + .../test/specs/try.yaml | 26 +++++++++++++++++++ .../test/util/guards.js | 4 +++ 4 files changed, 32 insertions(+) diff --git a/packages/istanbul-lib-instrument/src/instrumenter.js b/packages/istanbul-lib-instrument/src/instrumenter.js index d6e7655b..d563799c 100644 --- a/packages/istanbul-lib-instrument/src/instrumenter.js +++ b/packages/istanbul-lib-instrument/src/instrumenter.js @@ -87,6 +87,7 @@ class Instrumenter { 'asyncGenerators', 'dynamicImport', 'objectRestSpread', + 'optionalCatchBinding', 'flow', 'jsx' ] diff --git a/packages/istanbul-lib-instrument/src/read-coverage.js b/packages/istanbul-lib-instrument/src/read-coverage.js index a4a3fb6f..5e95f7c3 100644 --- a/packages/istanbul-lib-instrument/src/read-coverage.js +++ b/packages/istanbul-lib-instrument/src/read-coverage.js @@ -18,6 +18,7 @@ export default function readInitialCoverage (code) { 'asyncGenerators', 'dynamicImport', 'objectRestSpread', + 'optionalCatchBinding', 'flow', 'jsx' ] diff --git a/packages/istanbul-lib-instrument/test/specs/try.yaml b/packages/istanbul-lib-instrument/test/specs/try.yaml index 0acdf30a..101b9839 100644 --- a/packages/istanbul-lib-instrument/test/specs/try.yaml +++ b/packages/istanbul-lib-instrument/test/specs/try.yaml @@ -23,3 +23,29 @@ tests: lines: {'1': 1, '2': 1, '3': 0, '5': 1, '7': 1} branches: {'0': [1, 0]} statements: {'0': 1, '1': 1, '2': 1, '3': 0, '4': 1, '5': 1} +--- +name: optional catch binding +guard: isOptionalCatchBindingAvailable +code: | + try { + if (args[0] === "X") { throw "foo"; } + output = args[0]; + } catch { + output="Y"; + } finally { + output += 1; + } +tests: + - name: happy path + args: [1] + out: 2 + lines: {'1': 1, '2': 1, '3': 1, '5': 0, '7': 1} + branches: {'0': [0, 1]} + statements: {'0': 1, '1': 1, '2': 0, '3': 1, '4': 0, '5': 1} + + - name: sad path + args: [X] + out: Y1 + lines: {'1': 1, '2': 1, '3': 0, '5': 1, '7': 1} + branches: {'0': [1, 0]} + statements: {'0': 1, '1': 1, '2': 1, '3': 0, '4': 1, '5': 1} diff --git a/packages/istanbul-lib-instrument/test/util/guards.js b/packages/istanbul-lib-instrument/test/util/guards.js index 927fa167..38c67b2a 100644 --- a/packages/istanbul-lib-instrument/test/util/guards.js +++ b/packages/istanbul-lib-instrument/test/util/guards.js @@ -42,6 +42,10 @@ export function isObjectFreezeAvailable() { } } +export function isOptionalCatchBindingAvailable () { + return tryThis('try {} catch {}'); +} + export function isImportAvailable() { return tryThis('import fs from "fs"', 'import', true); }