Skip to content

Commit

Permalink
Use namespace for dependency extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
rubennorte committed Nov 9, 2018
1 parent 2c18a53 commit 0da47a8
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- `[babel-preset-jest]` [**BREAKING**] Export a function instead of an object for Babel 7 compatibility ([#7203](https://github.com/facebook/jest/pull/7203))
- `[expect]` Check constructor equality in .toStrictEqual() ([#7005](https://github.com/facebook/jest/pull/7005))
- `[jest-util]` Add `jest.getTimerCount()` to get the count of scheduled fake timers ([#7285](https://github.com/facebook/jest/pull/7285))
- `[jest-config]` Add `dependencyExtractor` option to use a custom module to extract dependencies from files ([#7313](https://github.com/facebook/jest/pull/7313))
- `[jest-config]` Add `dependencyExtractor` option to use a custom module to extract dependencies from files ([#7313](https://github.com/facebook/jest/pull/7313), [#7349](https://github.com/facebook/jest/pull/7349)).
- `[jest-haste-map]` [**BREAKING**] Expose relative paths when getting the file iterator ([#7321](https://github.com/facebook/jest/pull/7321))
- `[jest-config]` Add `haste.computeSha1` option to compute the sha-1 of the files in the haste map ([#7345](https://github.com/facebook/jest/pull/7345))

Expand Down
2 changes: 1 addition & 1 deletion docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ Jest will fail if:

Default: `undefined`

This option allows the use of a custom dependency extractor. It must be a node module that exports a function expecting a string as the first argument for the code to analyze and Jest's dependency extractor as the second argument (in case you only want to extend it).
This option allows the use of a custom dependency extractor. It must be a node module that exports an object with an `extract` function expecting a string as the first argument for the code to analyze and Jest's dependency extractor as the second argument (in case you only want to extend it).

The function should return an iterable (`Array`, `Set`, etc.) with the dependencies found in the code.

Expand Down
18 changes: 10 additions & 8 deletions e2e/__tests__/find_related_files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,16 @@ describe('--findRelatedTests flag', () => {
'a.js': 'module.exports = {foo: 5};',
'dependencyExtractor.js': `
const DYNAMIC_IMPORT_RE = /(?:^|[^.]\\s*)(\\bdynamicImport\\s*?\\(\\s*?)([\`'"])([^\`'"]+)(\\2\\s*?\\))/g;
module.exports = function dependencyExtractor(code) {
const dependencies = new Set();
const addDependency = (match, pre, quot, dep, post) => {
dependencies.add(dep);
return match;
};
code.replace(DYNAMIC_IMPORT_RE, addDependency);
return dependencies;
module.exports = {
extract(code) {
const dependencies = new Set();
const addDependency = (match, pre, quot, dep, post) => {
dependencies.add(dep);
return match;
};
code.replace(DYNAMIC_IMPORT_RE, addDependency);
return dependencies;
},
};
`,
'package.json': JSON.stringify({
Expand Down
7 changes: 2 additions & 5 deletions packages/jest-haste-map/src/__tests__/dependencyExtractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ const blockCommentRe = /\/\*[^]*?\*\//g;
const lineCommentRe = /\/\/.*/g;
const LOAD_MODULE_RE = /(?:^|[^.]\s*)(\bloadModule\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g;

module.exports = function dependencyExtractor(
code,
defaultDependencyExtractor,
) {
export function extract(code, defaultDependencyExtractor) {
const dependencies = defaultDependencyExtractor(code);

const addDependency = (match, pre, quot, dep, post) => {
Expand All @@ -28,4 +25,4 @@ module.exports = function dependencyExtractor(
.replace(LOAD_MODULE_RE, addDependency);

return dependencies;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
'use strict';

import extractRequires from '../extractRequires';
import {extract as extractRequires} from '../dependencyExtractor';

it('extracts both requires and imports from code', () => {
const code = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const replacePatterns = {
REQUIRE_RE: /(?:^|[^.]\s*)(\brequire\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g,
};

export default function extractRequires(code: string): Set<string> {
export function extract(code: string): Set<string> {
const dependencies = new Set();
const addDependency = (match, pre, quot, dep, post) => {
dependencies.add(dep);
Expand Down
9 changes: 6 additions & 3 deletions packages/jest-haste-map/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import path from 'path';
import fs from 'graceful-fs';
import blacklist from './blacklist';
import H from './constants';
import extractRequires from './lib/extractRequires';
import * as dependencyExtractor from './lib/dependencyExtractor';

const PACKAGE_JSON = path.sep + 'package.json';

Expand Down Expand Up @@ -81,8 +81,11 @@ export async function worker(data: WorkerMessage): Promise<WorkerMetadata> {
dependencies = Array.from(
data.dependencyExtractor
? // $FlowFixMe
require(data.dependencyExtractor)(content, extractRequires)
: extractRequires(content),
require(data.dependencyExtractor).extract(
content,
dependencyExtractor.extract,
)
: dependencyExtractor.extract(content),
);
}

Expand Down

0 comments on commit 0da47a8

Please sign in to comment.