Skip to content

Commit

Permalink
add transformer for untested file "all" options
Browse files Browse the repository at this point in the history
  • Loading branch information
cenfun committed Sep 15, 2024
1 parent 3cc04f3 commit 5f60e4e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
24 changes: 13 additions & 11 deletions lib/converter/untested.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ const resolveAllOptions = (input) => {

let dir;
let filter;
let transformer;

if (typeof input === 'string') {
const obj = Util.strToObj(input);
if (obj) {
dir = obj.dir;
filter = obj.filter;
transformer = obj.transformer;
} else {
dir = input;
}
Expand All @@ -78,6 +80,7 @@ const resolveAllOptions = (input) => {
} else {
dir = input.dir;
filter = input.filter;
transformer = input.transformer;
}

const dirList = resolveAllDirList(dir);
Expand All @@ -86,10 +89,12 @@ const resolveAllOptions = (input) => {
}

const fileFilter = resolveAllFilter(filter);
const fileTransformer = typeof transformer === 'function' ? transformer : () => {};

return {
dirList,
fileFilter
fileFilter,
fileTransformer
};
};

Expand Down Expand Up @@ -216,12 +221,7 @@ const getUntestedCoverageData = async (emptyList, options, coverageType) => {
return emptyCoverageList;
};

const getEmptyCoverages = async (fileList, options, coverageType) => {

let onEmpty = options.onEmpty;
if (typeof onEmpty !== 'function') {
onEmpty = () => {};
}
const getEmptyCoverages = async (fileList, options, coverageType, fileTransformer) => {

const emptyList = [];

Expand All @@ -241,9 +241,9 @@ const getEmptyCoverages = async (fileList, options, coverageType) => {
source
};

await onEmpty(entryFile, coverageType);
await fileTransformer(entryFile, coverageType);

// after onEmpty
// after transformer
entryFile.id = Util.calculateSha1(entryFile.sourcePath + entryFile.source);

emptyList.push(entryFile);
Expand All @@ -259,7 +259,9 @@ const getUntestedList = (testedMap, options, coverageType = 'v8') => {
if (!allOptions) {
return;
}
const { dirList, fileFilter } = allOptions;
const {
dirList, fileFilter, fileTransformer
} = allOptions;
const fileList = [];
dirList.forEach((dir) => {
Util.forEachFile(dir, [], (fileName, fileDir) => {
Expand All @@ -283,7 +285,7 @@ const getUntestedList = (testedMap, options, coverageType = 'v8') => {
return;
}

return getEmptyCoverages(fileList, options, coverageType);
return getEmptyCoverages(fileList, options, coverageType, fileTransformer);

};

Expand Down
3 changes: 2 additions & 1 deletion lib/default/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ module.exports = {
// options for adding empty coverage for all files
// all: {
// dir: ['src'],
// filter: (sourcePath) => true
// filter: (sourcePath) => true,
// transformer: (entry) => {}
// },
all: null,

Expand Down
8 changes: 8 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,14 @@ declare namespace MCR {
filter?: string | {
[pattern: string]: "js" | "css" | boolean;
} | ((filePath: string) => "js" | "css" | boolean);

/**
* the file transformer for source and sourceMap
* some of untested files like .ts/.jsx/.vue can not be parsed to AST directly by acorn
* so this is the function which can transform the original source to generated source and sourceMap
*/
transformer?: (entry: any) => Promise<void>;

};

/** (V8 only) {boolean} Enable/Disable ignoring uncovered codes with the special comments: v8 ignore next/next N/start/stop */
Expand Down

0 comments on commit 5f60e4e

Please sign in to comment.