Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

babel-plugin-istanbul 7.x [DO NOT MERGE] #259

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
@@ -0,0 +1,22 @@
name: Tests
on: [push, pull_request]

env:
CI: true

jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [10, 12, 14, 15]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2
- name: Node.js ${{matrix.node-version}} on ${{matrix.os}}
uses: actions/setup-node@v1
with:
node-version: ${{matrix.node-version}}
- run: npm install
- name: Test
run: npm run test
8 changes: 7 additions & 1 deletion .npmignore
@@ -1,2 +1,8 @@
.nycrc
.github
*.tgz
coverage/
src/

fixtures/
babel.config.js
**/*test*
20 changes: 20 additions & 0 deletions .nycrc
@@ -0,0 +1,20 @@
{
"temp-dir": "coverage/tmp",
"include": [
"src/*.js",
"fixtures/should-cover.js"
],
"require": [
"@babel/register"
],
"reporter": [
"lcov",
"text"
],
"lines": 100,
"functions": 100,
"statements": 100,
"branches": 100,
"sourceMap": false,
"instrument": false
}
21 changes: 0 additions & 21 deletions .travis.yml

This file was deleted.

36 changes: 27 additions & 9 deletions README.md
@@ -1,10 +1,4 @@
# babel-plugin-istanbul

[![Greenkeeper badge](https://badges.greenkeeper.io/istanbuljs/babel-plugin-istanbul.svg)](https://greenkeeper.io/)
[![Build Status](https://travis-ci.org/istanbuljs/babel-plugin-istanbul.svg?branch=master)](https://travis-ci.org/istanbuljs/babel-plugin-istanbul)
[![Coverage Status](https://coveralls.io/repos/github/istanbuljs/babel-plugin-istanbul/badge.svg?branch=master)](https://coveralls.io/github/istanbuljs/babel-plugin-istanbul?branch=master)
[![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
[![community slack](http://devtoolscommunity.herokuapp.com/badge.svg)](http://devtoolscommunity.herokuapp.com)
# babel-plugin-istanbul [![NPM Version][npm-image]][npm-url]

_Having problems? want to contribute? join our [community slack](http://devtoolscommunity.herokuapp.com)_.

Expand All @@ -20,7 +14,7 @@ To integrate with testing tools, please see the [Integrations](#integrations) se
Install it:

```
npm install --save-dev babel-plugin-istanbul
npm install --save-dev babel-plugin-istanbul @babel/core
```

Add it to `.babelrc` in test mode:
Expand Down Expand Up @@ -115,7 +109,7 @@ If you're instrumenting code programatically, you can pass a source map explicit
import babelPluginIstanbul from 'babel-plugin-istanbul';

function instrument(sourceCode, sourceMap, fileName) {
return babel.transform(sourceCode, {
return babel.transformSync(sourceCode, {
filename,
plugins: [
[babelPluginIstanbul, {
Expand All @@ -126,6 +120,27 @@ function instrument(sourceCode, sourceMap, fileName) {
}
```

You can also retrieve existing coverage data programatically. This does
not perform any modifications of the code.
```js
import {readCoverage} from 'babel-plugin-istanbul';
async function readInitialCoverage(sourceCode, fileName) {
let coverageData;
await babel.transformAsync(sourceCode, {
filename,
plugins: [
[readCoverage, {
onComplete(initialCoverage) {
coverageData = initialCoverage;
}
}]
]
});

return coverageData;
}
```

## Credit where credit is due

The approach used in `babel-plugin-istanbul` was inspired by [Thai Pangsakulyanont](https://github.com/dtinth)'s original library [`babel-plugin-__coverage__`](https://github.com/dtinth/babel-plugin-__coverage__).
Expand All @@ -135,3 +150,6 @@ The approach used in `babel-plugin-istanbul` was inspired by [Thai Pangsakulyano
Available as part of the Tidelift Subscription.

The maintainers of `babel-plugin-istanbul` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-babel-plugin-istanbul?utm_source=npm-babel-plugin-istanbul&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)

[npm-image]: https://img.shields.io/npm/v/babel-plugin-istanbul.svg
[npm-url]: https://npmjs.org/package/babel-plugin-istanbul
11 changes: 0 additions & 11 deletions fixtures/class-functions.js

This file was deleted.

1 change: 0 additions & 1 deletion fixtures/config/file1.js

This file was deleted.

1 change: 0 additions & 1 deletion fixtures/config/file2.js

This file was deleted.

5 changes: 0 additions & 5 deletions fixtures/config/nyc-alt.config.js

This file was deleted.

5 changes: 0 additions & 5 deletions fixtures/config/nyc.config.js

This file was deleted.

1 change: 0 additions & 1 deletion fixtures/config/package.json

This file was deleted.

4 changes: 0 additions & 4 deletions fixtures/should-respect-cwd.js

This file was deleted.

55 changes: 21 additions & 34 deletions package.json
Expand Up @@ -5,36 +5,34 @@
"license": "BSD-3-Clause",
"description": "A babel plugin that adds istanbul instrumentation to ES6 code",
"main": "lib/index.js",
"files": [
"lib"
],
"dependencies": {
"@babel/helper-plugin-utils": "^7.0.0",
"@istanbuljs/load-nyc-config": "^1.0.0",
"@istanbuljs/schema": "^0.1.2",
"istanbul-lib-instrument": "^4.0.0",
"@babel/helper-plugin-utils": "^7.12.13",
"@istanbuljs/schema": "^0.1.3",
"istanbul-lib-coverage": "^3.0.0",
"test-exclude": "^6.0.0"
},
"peerDependencies": {
"@babel/core": "^7"
},
"devDependencies": {
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@babel/plugin-transform-modules-commonjs": "^7.7.5",
"@babel/register": "^7.7.4",
"chai": "^4.2.0",
"coveralls": "^3.0.9",
"cross-env": "^6.0.3",
"mocha": "^6.2.2",
"nyc": "^15.0.0",
"pmock": "^0.2.3",
"standard": "^14.3.1",
"standard-version": "^7.1.0"
"@babel/cli": "^7.12.16",
"@babel/core": "^7.12.9",
"@babel/plugin-transform-modules-commonjs": "^7.12.13",
"@babel/register": "^7.12.13",
"cross-env": "^7.0.3",
"libtap": "^0.3.0",
"nyc": "^15.1.0",
"standard": "^16.0.3",
"standard-version": "^9.1.0",
"tap-yaml-summary": "^0.1.0"
},
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"release": "babel src --out-dir lib",
"pretest": "standard && npm run release",
"test": "cross-env NODE_ENV=test nyc --reporter=lcov --reporter=text mocha --timeout 5000 test/*.js",
"prepublish": "npm test && npm run release",
"tests-only": "cross-env NODE_ENV=test nyc --silent node test/babel-plugin-istanbul.js|tap-yaml-summary",
"test": "npm run -s tests-only",
"posttest": "nyc report --check-coverage",
"prepublish": "npm run release",
"version": "standard-version"
},
"standard": {
Expand All @@ -52,22 +50,11 @@
"plugin",
"instrumentation"
],
"nyc": {
"include": [
"src/*.js",
"fixtures/should-cover.js"
],
"require": [
"@babel/register"
],
"sourceMap": false,
"instrument": false
},
"bugs": {
"url": "https://github.com/istanbuljs/babel-plugin-istanbul/issues"
},
"homepage": "https://github.com/istanbuljs/babel-plugin-istanbul#readme",
"engines": {
"node": ">=8"
"node": ">=10.13.0"
}
}
13 changes: 13 additions & 0 deletions src/constants.js
@@ -0,0 +1,13 @@
import { createHash } from 'crypto'

// We should avoid changing this at all costs.
const versionString = 'istanbul-lib-instrument@4'

// function to use for creating hashes
export const SHA = 'sha1'
// name of coverage data magic key
export const MAGIC_KEY = '_coverageSchema'
// name of coverage data magic value
export const MAGIC_VALUE = createHash(SHA)
.update(versionString)
.digest('hex')
66 changes: 15 additions & 51 deletions src/index.js
@@ -1,62 +1,24 @@
import path from 'path'
import { realpathSync } from 'fs'
import { execFileSync } from 'child_process'
import { declare } from '@babel/helper-plugin-utils'
import { programVisitor } from 'istanbul-lib-instrument'
import programVisitor from './visitor.js'
import TestExclude from 'test-exclude'
import schema from '@istanbuljs/schema'

export { default as readCoverage } from './read-coverage.js'

function getRealpath (n) {
try {
return realpathSync(n) || /* istanbul ignore next */ n
} catch (e) {
} catch {
/* istanbul ignore next */
return n
}
}

const memoize = new Map()
/* istanbul ignore next */
const memosep = path.sep === '/' ? ':' : ';'

function loadNycConfig (cwd, opts) {
let memokey = cwd
const args = [
path.resolve(__dirname, 'load-nyc-config-sync.js'),
cwd
]

if ('nycrcPath' in opts) {
args.push(opts.nycrcPath)

memokey += memosep + opts.nycrcPath
}

/* execFileSync is expensive, avoid it if possible! */
if (memoize.has(memokey)) {
return memoize.get(memokey)
}

const result = JSON.parse(execFileSync(process.execPath, args))
const error = result['load-nyc-config-sync-error']
if (error) {
throw new Error(error)
}

const config = {
...schema.defaults.babelPluginIstanbul,
cwd,
...result
}
memoize.set(memokey, config)
return config
}

function findConfig (opts) {
const cwd = getRealpath(opts.cwd || process.env.NYC_CWD || /* istanbul ignore next */ process.cwd())
const keys = Object.keys(opts)
const ignored = Object.keys(opts).filter(s => s === 'nycrcPath' || s === 'cwd')
if (keys.length > ignored.length) {
if (keys.length > 0) {
// explicitly configuring options in babel
// takes precedence.
return {
Expand All @@ -66,12 +28,15 @@ function findConfig (opts) {
}
}

if (ignored.length === 0 && process.env.NYC_CONFIG) {
if (process.env.NYC_CONFIG) {
// defaults were already applied by nyc
return JSON.parse(process.env.NYC_CONFIG)
}

return loadNycConfig(cwd, opts)
return {
...schema.defaults.babelPluginIstanbul,
cwd
}
}

function makeShouldSkip () {
Expand All @@ -94,19 +59,18 @@ function makeShouldSkip () {
}

export default declare(api => {
api.assertVersion(7)
api.assertVersion('^7')

const shouldSkip = makeShouldSkip()

const t = api.types
return {
visitor: {
Program: {
enter (path) {
this.__dv__ = null
this.nycConfig = findConfig(this.opts)
const realPath = getRealpath(this.file.opts.filename)
if (shouldSkip(realPath, this.nycConfig)) {
const realPath = this.file.opts.filename
if (!this.opts.disableTestExclude && shouldSkip(realPath, this.nycConfig)) {
return
}
let { inputSourceMap } = this.opts
Expand All @@ -123,7 +87,7 @@ export default declare(api => {
visitorOptions[name] = schema.defaults.instrumentVisitor[name]
}
})
this.__dv__ = programVisitor(t, realPath, {
this.__dv__ = programVisitor(api, realPath, {
...visitorOptions,
inputSourceMap
})
Expand All @@ -135,7 +99,7 @@ export default declare(api => {
}
const result = this.__dv__.exit(path)
if (this.opts.onCover) {
this.opts.onCover(getRealpath(this.file.opts.filename), result.fileCoverage)
this.opts.onCover(getRealpath(this.file.opts.filename), result.fileCoverage, result.sourceMappingURL)
}
}
}
Expand Down