From 121fcd5ce2c0d7ce740f75ebb57e27faadad0574 Mon Sep 17 00:00:00 2001 From: david Date: Mon, 10 Jun 2019 23:24:21 +0800 Subject: [PATCH 1/6] fix(jest-haste-map): fix missing import statement --- CHANGELOG.md | 1 + .../src/lib/__tests__/dependencyExtractor.test.js | 3 ++- .../jest-haste-map/src/lib/dependencyExtractor.ts | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3e3025a0511..d2a31211f041 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixes +- `[jest-haste-map]` Fix the dependencyExtractor ([8547](https://github.com/facebook/jest/issues/8547)) - `[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)) diff --git a/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js b/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js index e13ed50c735f..c941e0d36bd4 100644 --- a/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js +++ b/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js @@ -59,12 +59,13 @@ 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', () => { diff --git a/packages/jest-haste-map/src/lib/dependencyExtractor.ts b/packages/jest-haste-map/src/lib/dependencyExtractor.ts index 1e10b59db7c2..8ab96a3313f3 100644 --- a/packages/jest-haste-map/src/lib/dependencyExtractor.ts +++ b/packages/jest-haste-map/src/lib/dependencyExtractor.ts @@ -52,7 +52,7 @@ 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), @@ -60,6 +60,14 @@ const IMPORT_OR_EXPORT_RE = createRegExp( 'g', ); +const IMPORT_RE = createRegExp( + [ + '\\b(?:import)\\s+', + CAPTURE_STRING_LITERAL(1), + ], + 'g', +); + const JEST_EXTENSIONS_RE = createRegExp( [ ...functionCallStart( @@ -75,7 +83,7 @@ const JEST_EXTENSIONS_RE = createRegExp( ); export function extract(code: string): Set { - const dependencies = new Set(); + const dependencies = new Set(); const addDependency = (match: string, _: string, dep: string) => { dependencies.add(dep); @@ -85,7 +93,8 @@ export function extract(code: string): Set { 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) .replace(REQUIRE_OR_DYNAMIC_IMPORT_RE, addDependency) .replace(JEST_EXTENSIONS_RE, addDependency); From 729885d67a2dd6e7c5f148dac17a6c7dccd32875 Mon Sep 17 00:00:00 2001 From: dy93 Date: Mon, 10 Jun 2019 23:49:56 +0800 Subject: [PATCH 2/6] Modify CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2a31211f041..02a7dca6852a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ### Fixes -- `[jest-haste-map]` Fix the dependencyExtractor ([8547](https://github.com/facebook/jest/issues/8547)) +- `[jest-haste-map]` Fix the dependencyExtractor ([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)) From 36cd5937c97556911a0eae8715dd146d633e3b7a Mon Sep 17 00:00:00 2001 From: dy93 Date: Tue, 11 Jun 2019 00:23:21 +0800 Subject: [PATCH 3/6] Update CHANGELOG.md Co-Authored-By: Tim Seckinger --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02a7dca6852a..23196b0dba34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ ### Fixes -- `[jest-haste-map]` Fix the dependencyExtractor ([8548](https://github.com/facebook/jest/pull/8548)) +- `[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)) From dbae641115b64eeec07265286138697607bddc6f Mon Sep 17 00:00:00 2001 From: david Date: Tue, 11 Jun 2019 00:31:45 +0800 Subject: [PATCH 4/6] Add negative test cases --- .../src/lib/__tests__/dependencyExtractor.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js b/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js index c941e0d36bd4..6339e0a4e8b0 100644 --- a/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js +++ b/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js @@ -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'); */ `; From dbe715f9d57422454546605ea9f1871143750350 Mon Sep 17 00:00:00 2001 From: david Date: Tue, 11 Jun 2019 00:31:53 +0800 Subject: [PATCH 5/6] Fix linter errors --- packages/jest-haste-map/src/lib/FSEventsWatcher.ts | 1 - .../src/lib/__tests__/dependencyExtractor.test.js | 4 +++- packages/jest-haste-map/src/lib/dependencyExtractor.ts | 5 +---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/packages/jest-haste-map/src/lib/FSEventsWatcher.ts b/packages/jest-haste-map/src/lib/FSEventsWatcher.ts index b0ebad2867dd..95b515b083dc 100644 --- a/packages/jest-haste-map/src/lib/FSEventsWatcher.ts +++ b/packages/jest-haste-map/src/lib/FSEventsWatcher.ts @@ -11,7 +11,6 @@ import path from 'path'; import {EventEmitter} from 'events'; import anymatch from 'anymatch'; import micromatch from 'micromatch'; -// eslint-disable-next-line import {Watcher} from 'fsevents'; // @ts-ignore no types import walker from 'walker'; diff --git a/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js b/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js index 6339e0a4e8b0..0bc0b0f424a9 100644 --- a/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js +++ b/packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js @@ -67,7 +67,9 @@ describe('dependencyExtractor', () => { ${COMMENT_NO_NEG_LB} foo . import ('inv1'); ${COMMENT_NO_NEG_LB} foo . export ('inv2'); `; - expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4', 'dep5'])); + expect(extract(code)).toEqual( + new Set(['dep1', 'dep2', 'dep3', 'dep4', 'dep5']), + ); }); it('should not extract dependencies from `import type/typeof` statements', () => { diff --git a/packages/jest-haste-map/src/lib/dependencyExtractor.ts b/packages/jest-haste-map/src/lib/dependencyExtractor.ts index 8ab96a3313f3..13b2c6cd60bb 100644 --- a/packages/jest-haste-map/src/lib/dependencyExtractor.ts +++ b/packages/jest-haste-map/src/lib/dependencyExtractor.ts @@ -61,10 +61,7 @@ const IMPORT_OR_EXPORT_FROM_RE = createRegExp( ); const IMPORT_RE = createRegExp( - [ - '\\b(?:import)\\s+', - CAPTURE_STRING_LITERAL(1), - ], + ['\\b(?:import)\\s+', CAPTURE_STRING_LITERAL(1)], 'g', ); From d46edca48e1a5232ed6ca71551050c01479b5c13 Mon Sep 17 00:00:00 2001 From: dy93 Date: Tue, 11 Jun 2019 00:42:07 +0800 Subject: [PATCH 6/6] Fix linter error --- packages/jest-haste-map/src/lib/FSEventsWatcher.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/jest-haste-map/src/lib/FSEventsWatcher.ts b/packages/jest-haste-map/src/lib/FSEventsWatcher.ts index 95b515b083dc..b0ebad2867dd 100644 --- a/packages/jest-haste-map/src/lib/FSEventsWatcher.ts +++ b/packages/jest-haste-map/src/lib/FSEventsWatcher.ts @@ -11,6 +11,7 @@ import path from 'path'; import {EventEmitter} from 'events'; import anymatch from 'anymatch'; import micromatch from 'micromatch'; +// eslint-disable-next-line import {Watcher} from 'fsevents'; // @ts-ignore no types import walker from 'walker';