Skip to content

Commit 9ebe8da

Browse files
committed
feat(prelude): add tokens support
1 parent 68a4570 commit 9ebe8da

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"homepage": "https://github.com/esnext-coverage/babel-plugin-transform-esnext-coverage",
2828
"dependencies": {
2929
"babel-template": "^6.16.0",
30-
"esnext-coverage-analytics": "^0.0.5"
30+
"esnext-coverage-analytics": "^0.0.6"
3131
},
3232
"devDependencies": {
3333
"babel-cli": "^6.18.0",
@@ -37,8 +37,7 @@
3737
"babel-preset-es2015": "^6.18.0",
3838
"eslint": "^3.12.2",
3939
"eslint-config-meetic": "^4.0.0",
40-
"esnext-coverage": "^0.0.5",
41-
"esnext-coverage-format-html": "^0.0.6",
40+
"esnext-coverage": "^0.0.7",
4241
"tape": "^4.6.3"
4342
},
4443
"esnextcoverage": {

src/marker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ export function createMarker(state, {loc, tags}, node) {
3737
args.push(node);
3838
}
3939
const marker = types.callExpression(variable, args);
40-
locations.push(codec.encode({loc, tags, count: 0}));
40+
locations.push(codec.encodeLocation({loc, tags, count: 0}));
4141
return markAsInstrumented(marker);
4242
}

src/prelude.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,35 @@ import {readFileSync} from 'fs';
22
import path from 'path';
33
import {types} from 'babel-core';
44
import template from 'babel-template';
5+
import {codec} from 'esnext-coverage-analytics';
56
import {getCoverageMeta} from './meta';
67

78
const preludeFilePath = path.resolve(__dirname, 'templates/prelude-template.js');
89
const render = template(readFileSync(preludeFilePath, 'utf8'));
910

1011
export const defaultNamespace = '__coverage__';
1112

13+
function normalizeTokenType(tokenType) {
14+
if (tokenType === 'CommentBlock' || tokenType === 'CommentLine') { return 'comment'; }
15+
if (tokenType.keyword) { return 'keyword'; }
16+
if (tokenType.label) { return tokenType.label; }
17+
return 'other';
18+
}
19+
20+
function simplifyToken({loc, type}) {
21+
return {loc, type: normalizeTokenType(type)};
22+
}
23+
1224
export default function prelude(state) {
1325
const {variable, locations} = getCoverageMeta(state);
1426
const name = state.file.opts.filenameRelative.replace(`${process.cwd()}${path.sep}`, '');
1527
const namespace = state.opts && state.opts.global || defaultNamespace;
28+
const tokens = state.file.ast.tokens.map(simplifyToken).map(codec.encodeToken);
1629
return render({
1730
VARIABLE: variable,
1831
NAMESPACE: types.stringLiteral(namespace),
1932
FILEPATH: types.stringLiteral(name),
33+
TOKENS: types.stringLiteral(JSON.stringify(tokens)),
2034
LOCATIONS: types.stringLiteral(JSON.stringify(locations))
2135
});
2236
}

src/templates/.eslintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"globals": {
33
"NAMESPACE": true,
44
"FILEPATH": true,
5+
"TOKENS": true,
56
"LOCATIONS": true,
67
"window": true,
78
"global": true

src/templates/prelude-template.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
var VARIABLE = (function (context) {
2+
var tokens = JSON.parse(TOKENS);
23
var locations = JSON.parse(LOCATIONS);
4+
var stack = [];
35
context[NAMESPACE] = context[NAMESPACE] || {};
4-
context[NAMESPACE][FILEPATH] = locations;
6+
context[NAMESPACE][FILEPATH] = [locations, tokens, stack];
57
return function (id, value) {
68
locations[id][0]++;
9+
stack.push(id);
710
return value;
811
};
912
})(

test/spec/helpers/run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default function runFixture(fixtureName) {
1616
};
1717
runInNewContext(code, sandbox);
1818
const fileCoverage = sandbox.global[namespace][relativeFixturePath];
19-
return codec.decodeAll(fileCoverage);
19+
return codec.decode(fileCoverage).locations;
2020
})
2121
.catch(error => console.error(error)); // eslint-disable-line no-console
2222
}

0 commit comments

Comments
 (0)