From e5f2e03af2ae302dbb60fd544c4c726616170a9f Mon Sep 17 00:00:00 2001 From: shlomiatar Date: Thu, 1 Oct 2015 12:15:24 -0700 Subject: [PATCH] Fix DependencyResolver to support ES6 multiline imports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../__tests__/HasteDependencyResolver-test.js | 116 ++++++++++++++++++ .../src/DependencyResolver/replacePatterns.js | 2 +- 2 files changed, 117 insertions(+), 1 deletion(-) diff --git a/react-packager/src/DependencyResolver/__tests__/HasteDependencyResolver-test.js b/react-packager/src/DependencyResolver/__tests__/HasteDependencyResolver-test.js index 5231dc4a..bb04dd43 100644 --- a/react-packager/src/DependencyResolver/__tests__/HasteDependencyResolver-test.js +++ b/react-packager/src/DependencyResolver/__tests__/HasteDependencyResolver-test.js @@ -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\' )', diff --git a/react-packager/src/DependencyResolver/replacePatterns.js b/react-packager/src/DependencyResolver/replacePatterns.js index c27d7c77..ef5c42f2 100644 --- a/react-packager/src/DependencyResolver/replacePatterns.js +++ b/react-packager/src/DependencyResolver/replacePatterns.js @@ -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;