Skip to content

Commit

Permalink
feat: Enable classProperties and classPrivateProperties parsers and c…
Browse files Browse the repository at this point in the history
…overage. (#379)
  • Loading branch information
coreyfarrell committed Apr 24, 2019
1 parent 72b0f05 commit c09dc38
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
7 changes: 2 additions & 5 deletions packages/istanbul-lib-instrument/src/instrumenter.js
Expand Up @@ -26,11 +26,8 @@ export function defaultOpts() {
plugins: [
'asyncGenerators',
'bigInt',
/* Verify and add upon release of node.js 12:
* classProperties
* classPrivateProperties
* classPrivateMethods
*/
'classProperties',
'classPrivateProperties',
'dynamicImport',
'importMeta',
'objectRestSpread',
Expand Down
10 changes: 2 additions & 8 deletions packages/istanbul-lib-instrument/src/read-coverage.js
Expand Up @@ -2,6 +2,7 @@ import { parse } from '@babel/parser';
import traverse from '@babel/traverse';
import * as t from '@babel/types';
import { MAGIC_KEY, MAGIC_VALUE } from './constants';
import { defaultOpts } from './instrumenter';

export default function readInitialCoverage(code) {
if (typeof code !== 'string') {
Expand All @@ -14,14 +15,7 @@ export default function readInitialCoverage(code) {
allowReturnOutsideFunction: true,
allowSuperOutsideMethod: true,
sourceType: 'script',
plugins: [
'asyncGenerators',
'dynamicImport',
'objectRestSpread',
'optionalCatchBinding',
'flow',
'jsx'
]
plugins: defaultOpts().plugins
});

let covScope;
Expand Down
6 changes: 6 additions & 0 deletions packages/istanbul-lib-instrument/src/visitor.js
Expand Up @@ -356,6 +356,10 @@ function coverVariableDeclarator(path) {
this.insertStatementCounter(path.get('init'));
}

function coverClassPropDeclarator(path) {
this.insertStatementCounter(path.get('value'));
}

function makeBlock(path) {
const T = this.types;
if (!path.node) {
Expand Down Expand Up @@ -485,6 +489,8 @@ const codeVisitor = {
ExportNamedDeclaration: entries(), // ignore processing only
ClassMethod: entries(coverFunction),
ClassDeclaration: entries(parenthesizedExpressionProp('superClass')),
ClassProperty: entries(coverClassPropDeclarator),
ClassPrivateProperty: entries(coverClassPropDeclarator),
ObjectMethod: entries(coverFunction),
ExpressionStatement: entries(coverStatement),
BreakStatement: entries(coverStatement),
Expand Down
49 changes: 49 additions & 0 deletions packages/istanbul-lib-instrument/test/specs/class-properties.yaml
@@ -0,0 +1,49 @@
---
name: class property declaration
guard: isClassPropAvailable
code: |
class Foo {
bar = 1;
uninitialized;
}
output = args === 1 ? new Foo().bar : args
tests:
- name: covered
args: 1
out: 1
lines: {'2': 1, '5': 1}
statements: {'0': 1, '1': 1}
branches: {'0': [1, 0]}
functions: {}
- name: not covered
args: 2
out: 2
lines: {'2': 0, '5': 1}
statements: {'0': 0, '1': 1}
branches: {'0': [0, 1]}
functions: {}
---
name: class private property declaration
guard: isClassPrivatePropAvailable
code: |
class Foo {
#bar = 1;
get bar() { return this.#bar; }
#uninitialized;
}
output = args === 1 ? new Foo().bar : args
tests:
- name: covered
args: 1
out: 1
lines: {'2': 1, '3': 1, '6': 1}
statements: {'0': 1, '1': 1, '2': 1}
branches: {'0': [1, 0]}
functions: {'0': 1}
- name: not covered
args: 2
out: 2
lines: {'2': 0, '3': 0, '6': 1}
statements: {'0': 0, '1': 0, '2': 1}
branches: {'0': [0, 1]}
functions: {'0': 0}
8 changes: 8 additions & 0 deletions packages/istanbul-lib-instrument/test/util/guards.js
Expand Up @@ -18,6 +18,14 @@ export function isYieldAvailable() {
return tryThis('function *foo() { yield 1; }', 'yield');
}

export function isClassPropAvailable() {
return tryThis('class Foo { a = 1; }', 'class property');
}

export function isClassPrivatePropAvailable() {
return tryThis('class Foo { #a = 1; }', 'class private property');
}

export function isForOfAvailable() {
return tryThis(
'function *foo() { yield 1; }\n' + 'for (var k of foo()) {}',
Expand Down

0 comments on commit c09dc38

Please sign in to comment.