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

feat(jest-haste-map): support dependencyExtractor written in ESM #12008

Merged
merged 9 commits into from
Feb 24, 2022
Prev Previous commit
Next Next commit
types
  • Loading branch information
SimenB committed Feb 24, 2022

Verified

This commit was signed with the committer’s verified signature.
gsmet Guillaume Smet
commit e22038f032e7bf3aecbbf04125514ccdb7087b9f
31 changes: 17 additions & 14 deletions packages/jest-haste-map/src/lib/dependencyExtractor.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import type {DependencyExtractor} from '../types';
import isRegExpSupported from './isRegExpSupported';

// Negative look behind is only supported in Node 9+
@@ -73,20 +74,22 @@ const JEST_EXTENSIONS_RE = createRegExp(
'g',
);

export function extract(code: string): Set<string> {
const dependencies = new Set<string>();
export const extractor: DependencyExtractor = {
extract(code) {
const dependencies = new Set<string>();

const addDependency = (match: string, _: string, dep: string) => {
dependencies.add(dep);
return match;
};
const addDependency = (match: string, _: string, dep: string) => {
dependencies.add(dep);
return match;
};

code
.replace(BLOCK_COMMENT_RE, '')
.replace(LINE_COMMENT_RE, '')
.replace(IMPORT_OR_EXPORT_RE, addDependency)
.replace(REQUIRE_OR_DYNAMIC_IMPORT_RE, addDependency)
.replace(JEST_EXTENSIONS_RE, addDependency);
code
.replace(BLOCK_COMMENT_RE, '')
.replace(LINE_COMMENT_RE, '')
.replace(IMPORT_OR_EXPORT_RE, addDependency)
.replace(REQUIRE_OR_DYNAMIC_IMPORT_RE, addDependency)
.replace(JEST_EXTENSIONS_RE, addDependency);

return dependencies;
}
return dependencies;
},
};
10 changes: 7 additions & 3 deletions packages/jest-haste-map/src/worker.ts
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@ import * as fs from 'graceful-fs';
import {requireOrImportModule} from 'jest-util';
import blacklist from './blacklist';
import H from './constants';
import * as dependencyExtractor from './lib/dependencyExtractor';
import {extractor as defaultDependencyExtractor} from './lib/dependencyExtractor';
import type {
DependencyExtractor,
HasteImpl,
@@ -82,9 +82,13 @@ export async function worker(data: WorkerMessage): Promise<WorkerMetadata> {
data.dependencyExtractor,
false,
)
: dependencyExtractor;
: defaultDependencyExtractor;
dependencies = Array.from(
extractor.extract(content, filePath, dependencyExtractor.extract),
extractor.extract(
content,
filePath,
defaultDependencyExtractor.extract,
),
);
}