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

fix(jest-haste-map): fix missing import statement #8548

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@

### Fixes

- `[jest-haste-map]` Detect imports without `from` ([8548](https://github.com/facebook/jest/pull/8548))
- `[babel-plugin-jest-hoist]` Expand list of whitelisted globals in global mocks ([#8429](https://github.com/facebook/jest/pull/8429)
- `[jest-core]` Make watch plugin initialization errors look nice ([#8422](https://github.com/facebook/jest/pull/8422))
- `[jest-snapshot]` Prevent inline snapshots from drifting when inline snapshots are updated ([#8492](https://github.com/facebook/jest/pull/8492))
Expand Down
1 change: 0 additions & 1 deletion packages/jest-haste-map/src/lib/FSEventsWatcher.ts
Expand Up @@ -11,7 +11,6 @@ import path from 'path';
import {EventEmitter} from 'events';
import anymatch from 'anymatch';
import micromatch from 'micromatch';
// eslint-disable-next-line
dy93 marked this conversation as resolved.
Show resolved Hide resolved
import {Watcher} from 'fsevents';
// @ts-ignore no types
import walker from 'walker';
Expand Down
Expand Up @@ -14,9 +14,11 @@ describe('dependencyExtractor', () => {
it('should not extract dependencies inside comments', () => {
const code = `
// import a from 'ignore-line-comment';
// import 'ignore-line-comment';
// require('ignore-line-comment');
/*
* import a from 'ignore-block-comment';
* import 'ignore-block-comment';
* require('ignore-block-comment');
*/
`;
Expand Down Expand Up @@ -59,12 +61,15 @@ describe('dependencyExtractor', () => {
a as aliased_a,
b,
}, depDefault from 'dep4';
import 'dep5';

// Bad
${COMMENT_NO_NEG_LB} foo . import ('inv1');
${COMMENT_NO_NEG_LB} foo . export ('inv2');
`;
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
expect(extract(code)).toEqual(
new Set(['dep1', 'dep2', 'dep3', 'dep4', 'dep5']),
);
});

it('should not extract dependencies from `import type/typeof` statements', () => {
Expand Down
12 changes: 9 additions & 3 deletions packages/jest-haste-map/src/lib/dependencyExtractor.ts
Expand Up @@ -52,14 +52,19 @@ const REQUIRE_OR_DYNAMIC_IMPORT_RE = createRegExp(
'g',
);

const IMPORT_OR_EXPORT_RE = createRegExp(
const IMPORT_OR_EXPORT_FROM_RE = createRegExp(
[
'\\b(?:import|export)\\s+(?!type(?:of)?\\s+)[^\'"]+\\s+from\\s+',
CAPTURE_STRING_LITERAL(1),
],
'g',
);

const IMPORT_RE = createRegExp(
['\\b(?:import)\\s+', CAPTURE_STRING_LITERAL(1)],
'g',
);

const JEST_EXTENSIONS_RE = createRegExp(
[
...functionCallStart(
Expand All @@ -75,7 +80,7 @@ const JEST_EXTENSIONS_RE = createRegExp(
);

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

const addDependency = (match: string, _: string, dep: string) => {
dependencies.add(dep);
Expand All @@ -85,7 +90,8 @@ export function extract(code: string): Set<string> {
code
.replace(BLOCK_COMMENT_RE, '')
.replace(LINE_COMMENT_RE, '')
.replace(IMPORT_OR_EXPORT_RE, addDependency)
.replace(IMPORT_OR_EXPORT_FROM_RE, addDependency)
.replace(IMPORT_RE, addDependency)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does that impact performance? could we squeeze this check into a single regex pass so we don't have to parse the text once more?

.replace(REQUIRE_OR_DYNAMIC_IMPORT_RE, addDependency)
.replace(JEST_EXTENSIONS_RE, addDependency);

Expand Down