Fix DependencyResolver to support ES6 multiline imports

Summary: *This is a PR to fix #1939 (DependencyResolver fails to handle ES6 modules import statements with new lines)*

**This PR includes:**
- A fix to the problematic regular expression
- Updated tests that support the new line style.

**Summary:**
We found out that while the packager does its module wrapping thing for lines like this:
```js
import theDefault, { named1, named2 } from 'src/mylib';
```
It fails to do the same for multi line imports:
```js
import theDefault, {
  named1,
  named2
} from 'src/mylib';
```
We've tracked done the issue to a [faulty regular expression in replacePatterns.js](https://github.com/facebook/react-native/blob/master/packager/react-packager/src/DependencyResolver/replacePatterns.js#L12)

You can see various import statements with the problematic regular expression [here](http://regexr.com/3bc8m)

We've figure out a better regular expression (you can play around with it [here](http://regexr.com/3bd3s))Closes https://github.com/facebook/react-native/pull/1940

Reviewed By: @​svcscm

Differential Revision: D2498519

Pulled By: @vjeux
This commit is contained in:
shlomiatar 2015-10-01 12:15:24 -07:00 committed by facebook-github-bot-3
parent f45eaaaa1e
commit e5f2e03af2
2 changed files with 117 additions and 1 deletions

View File

@ -218,6 +218,7 @@ describe('HasteDependencyResolver', function() {
/*eslint-disable */
var code = [
// single line import
"import'x';",
"import 'x';",
"import 'x' ;",
@ -350,6 +351,63 @@ describe('HasteDependencyResolver', function() {
'import Default, { Foo as Bar, Baz as Qux, Norf as Enuf, } from "x";',
'import Default from "y";',
'import * as All from \'z\';',
// import with support for new lines
"import { Foo,\n Bar }\n from 'x';",
"import { \nFoo,\nBar,\n }\n from 'x';",
"import { Foo as Bar,\n Baz\n }\n from 'x';",
"import { \nFoo as Bar,\n Baz\n, }\n from 'x';",
"import { Foo,\n Bar as Baz\n }\n from 'x';",
"import { Foo,\n Bar as Baz,\n }\n from 'x';",
"import { Foo as Bar,\n Baz as Qux\n }\n from 'x';",
"import { Foo as Bar,\n Baz as Qux,\n }\n from 'x';",
"import { Foo,\n Bar,\n Baz }\n from 'x';",
"import { Foo,\n Bar,\n Baz,\n }\n from 'x';",
"import { Foo as Bar,\n Baz,\n Qux\n }\n from 'x';",
"import { Foo as Bar,\n Baz,\n Qux,\n }\n from 'x';",
"import { Foo,\n Bar as Baz,\n Qux\n }\n from 'x';",
"import { Foo,\n Bar as Baz,\n Qux,\n }\n from 'x';",
"import { Foo,\n Bar,\n Baz as Qux\n }\n from 'x';",
"import { Foo,\n Bar,\n Baz as Qux,\n }\n from 'x';",
"import { Foo as Bar,\n Baz as Qux,\n Norf\n }\n from 'x';",
"import { Foo as Bar,\n Baz as Qux,\n Norf,\n }\n from 'x';",
"import { Foo as Bar,\n Baz,\n Qux as Norf\n }\n from 'x';",
"import { Foo as Bar,\n Baz,\n Qux as Norf,\n }\n from 'x';",
"import { Foo,\n Bar as Baz,\n Qux as Norf\n }\n from 'x';",
"import { Foo,\n Bar as Baz,\n Qux as Norf,\n }\n from 'x';",
"import { Foo as Bar,\n Baz as Qux,\n Norf as Enuf\n }\n from 'x';",
"import { Foo as Bar,\n Baz as Qux,\n Norf as Enuf,\n }\n from 'x';",
"import Default,\n * as All from 'x';",
"import Default,\n { } from 'x';",
"import Default,\n { Foo\n }\n from 'x';",
"import Default,\n { Foo,\n }\n from 'x';",
"import Default,\n { Foo as Bar\n }\n from 'x';",
"import Default,\n { Foo as Bar,\n }\n from 'x';",
"import Default,\n { Foo,\n Bar\n } from\n 'x';",
"import Default,\n { Foo,\n Bar,\n } from\n 'x';",
"import Default,\n { Foo as Bar,\n Baz\n }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz,\n }\n from 'x';",
"import Default,\n { Foo,\n Bar as Baz\n }\n from 'x';",
"import Default,\n { Foo,\n Bar as Baz,\n }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz as Qux\n }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz as Qux,\n }\n from 'x';",
"import Default,\n { Foo,\n Bar,\n Baz\n }\n from 'x';",
"import Default,\n { Foo,\n Bar,\n Baz,\n }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz,\n Qux\n }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz,\n Qux,\n }\n from 'x';",
"import Default,\n { Foo,\n Bar as Baz,\n Qux\n }\n from 'x';",
"import Default,\n { Foo,\n Bar as Baz,\n Qux,\n }\n from 'x';",
"import Default,\n { Foo,\n Bar,\n Baz as Qux\n }\n from 'x';",
"import Default,\n { Foo,\n Bar,\n Baz as Qux,\n }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz as Qux,\n Norf\n }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz as Qux,\n Norf,\n }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz,\n Qux as Norf }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz,\n Qux as Norf, }\n from 'x';",
"import Default,\n { Foo, Bar as Baz,\n Qux as Norf }\n from 'x';",
"import Default,\n { Foo, Bar as Baz,\n Qux as Norf, }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz as Qux,\n Norf as NoMore\n }\n from 'x';",
"import Default,\n { Foo as Bar,\n Baz as Qux,\n Norf as NoMore,\n }\n from 'x';",
"import Default\n , { } from 'x';",
// require
'require("x")',
'require("y")',
'require( \'z\' )',
@ -381,6 +439,7 @@ describe('HasteDependencyResolver', function() {
expect(processedCode).toEqual([
'__d(\'test module\',["changed","Y"],function(global, require,' +
' module, exports) { ' +
// single line import
"import'x';",
"import 'changed';",
"import 'changed' ;",
@ -513,6 +572,63 @@ describe('HasteDependencyResolver', function() {
'import Default, { Foo as Bar, Baz as Qux, Norf as Enuf, } from "changed";',
'import Default from "Y";',
'import * as All from \'z\';',
// import with support for new lines
"import { Foo,\n Bar }\n from 'changed';",
"import { \nFoo,\nBar,\n }\n from 'changed';",
"import { Foo as Bar,\n Baz\n }\n from 'changed';",
"import { \nFoo as Bar,\n Baz\n, }\n from 'changed';",
"import { Foo,\n Bar as Baz\n }\n from 'changed';",
"import { Foo,\n Bar as Baz,\n }\n from 'changed';",
"import { Foo as Bar,\n Baz as Qux\n }\n from 'changed';",
"import { Foo as Bar,\n Baz as Qux,\n }\n from 'changed';",
"import { Foo,\n Bar,\n Baz }\n from 'changed';",
"import { Foo,\n Bar,\n Baz,\n }\n from 'changed';",
"import { Foo as Bar,\n Baz,\n Qux\n }\n from 'changed';",
"import { Foo as Bar,\n Baz,\n Qux,\n }\n from 'changed';",
"import { Foo,\n Bar as Baz,\n Qux\n }\n from 'changed';",
"import { Foo,\n Bar as Baz,\n Qux,\n }\n from 'changed';",
"import { Foo,\n Bar,\n Baz as Qux\n }\n from 'changed';",
"import { Foo,\n Bar,\n Baz as Qux,\n }\n from 'changed';",
"import { Foo as Bar,\n Baz as Qux,\n Norf\n }\n from 'changed';",
"import { Foo as Bar,\n Baz as Qux,\n Norf,\n }\n from 'changed';",
"import { Foo as Bar,\n Baz,\n Qux as Norf\n }\n from 'changed';",
"import { Foo as Bar,\n Baz,\n Qux as Norf,\n }\n from 'changed';",
"import { Foo,\n Bar as Baz,\n Qux as Norf\n }\n from 'changed';",
"import { Foo,\n Bar as Baz,\n Qux as Norf,\n }\n from 'changed';",
"import { Foo as Bar,\n Baz as Qux,\n Norf as Enuf\n }\n from 'changed';",
"import { Foo as Bar,\n Baz as Qux,\n Norf as Enuf,\n }\n from 'changed';",
"import Default,\n * as All from 'changed';",
"import Default,\n { } from 'changed';",
"import Default,\n { Foo\n }\n from 'changed';",
"import Default,\n { Foo,\n }\n from 'changed';",
"import Default,\n { Foo as Bar\n }\n from 'changed';",
"import Default,\n { Foo as Bar,\n }\n from 'changed';",
"import Default,\n { Foo,\n Bar\n } from\n 'changed';",
"import Default,\n { Foo,\n Bar,\n } from\n 'changed';",
"import Default,\n { Foo as Bar,\n Baz\n }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz,\n }\n from 'changed';",
"import Default,\n { Foo,\n Bar as Baz\n }\n from 'changed';",
"import Default,\n { Foo,\n Bar as Baz,\n }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz as Qux\n }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz as Qux,\n }\n from 'changed';",
"import Default,\n { Foo,\n Bar,\n Baz\n }\n from 'changed';",
"import Default,\n { Foo,\n Bar,\n Baz,\n }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz,\n Qux\n }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz,\n Qux,\n }\n from 'changed';",
"import Default,\n { Foo,\n Bar as Baz,\n Qux\n }\n from 'changed';",
"import Default,\n { Foo,\n Bar as Baz,\n Qux,\n }\n from 'changed';",
"import Default,\n { Foo,\n Bar,\n Baz as Qux\n }\n from 'changed';",
"import Default,\n { Foo,\n Bar,\n Baz as Qux,\n }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz as Qux,\n Norf\n }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz as Qux,\n Norf,\n }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz,\n Qux as Norf }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz,\n Qux as Norf, }\n from 'changed';",
"import Default,\n { Foo, Bar as Baz,\n Qux as Norf }\n from 'changed';",
"import Default,\n { Foo, Bar as Baz,\n Qux as Norf, }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz as Qux,\n Norf as NoMore\n }\n from 'changed';",
"import Default,\n { Foo as Bar,\n Baz as Qux,\n Norf as NoMore,\n }\n from 'changed';",
"import Default\n , { } from 'changed';",
// require
'require("changed")',
'require("Y")',
'require( \'z\' )',

View File

@ -9,7 +9,7 @@
'use strict';
exports.IMPORT_RE = /(\bimport\s+?(?:.+\s+?from\s+?)?)(['"])([^'"]+)(\2)/g;
exports.IMPORT_RE = /(\bimport\s+(?:[^'"]+\s+from\s+)??)(['"])([^'"]+)(\2)/g;
exports.REQUIRE_RE = /(\brequire\s*?\(\s*?)(['"])([^'"]+)(\2\s*?\))/g;
exports.SYSTEM_IMPORT_RE = /(\bSystem\.import\s*?\(\s*?)(['"])([^'"]+)(\2\s*?\))/g;