From f343f81bff1649a2e71c21a7d2db4c6c7ca72e77 Mon Sep 17 00:00:00 2001 From: Anthony Astige Date: Wed, 10 Jul 2019 09:26:40 -0700 Subject: [PATCH] Issue 8547: side effect import deps extracted ed4edd890 -> red 965a24c04 -> green c4bbbba26 -> Refinement --- CHANGELOG.md | 1 + .../lib/__tests__/dependencyExtractor.test.js | 21 +++++++++++++++++++ .../src/lib/dependencyExtractor.ts | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c57e8fc17c7..bc746212bc31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ ### Fixes +- `[jest-cli]` Detect side-effect only imports when running `--onlyChanged` or `--changedSince` ([#8670](https://github.com/facebook/jest/pull/8670)) - `[jest-cli]` Allow `--maxWorkers` to work with % input again ([#8565](https://github.com/facebook/jest/pull/8565)) - `[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)) 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..516b5741b598 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,13 @@ describe('dependencyExtractor', () => { it('should not extract dependencies inside comments', () => { const code = ` // import a from 'ignore-line-comment'; + // import 'ignore-line-comment'; + // import './ignore-line-comment'; // require('ignore-line-comment'); /* * import a from 'ignore-block-comment'; + * import './ignore-block-comment'; + * import 'ignore-block-comment'; * require('ignore-block-comment'); */ `; @@ -67,6 +71,23 @@ describe('dependencyExtractor', () => { expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4'])); }); + // https://github.com/facebook/jest/issues/8547 + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_a_module_for_its_side_effects_only + it('should extract dependencies from side-effect only `import` statements', () => { + const code = ` + // Good + import './side-effect-dep1'; + import 'side-effect-dep2'; + + // Bad + import ./inv1; + import inv2 + `; + expect(extract(code)).toEqual( + new Set(['./side-effect-dep1', 'side-effect-dep2']), + ); + }); + it('should not extract dependencies from `import type/typeof` statements', () => { const code = ` // Bad diff --git a/packages/jest-haste-map/src/lib/dependencyExtractor.ts b/packages/jest-haste-map/src/lib/dependencyExtractor.ts index 2335e30d6b69..d1aa4445ff3c 100644 --- a/packages/jest-haste-map/src/lib/dependencyExtractor.ts +++ b/packages/jest-haste-map/src/lib/dependencyExtractor.ts @@ -54,7 +54,7 @@ const REQUIRE_OR_DYNAMIC_IMPORT_RE = createRegExp( const IMPORT_OR_EXPORT_RE = createRegExp( [ - '\\b(?:import|export)\\s+(?!type(?:of)?\\s+)[^\'"]+\\s+from\\s+', + '\\b(?:import|export)\\s+(?!type(?:of)?\\s+)(?:[^\'"]+\\s+from\\s+)?', CAPTURE_STRING_LITERAL(1), ], 'g',