Skip to content

Commit

Permalink
Bugfix/require module regexp
Browse files Browse the repository at this point in the history
Summary:
Resolves facebook/react-native#316. Also updated the spec for the Haste Dependency Resolver. Not sure if these changes are the ones desired so feedback would be welcome!
Closes facebook/react-native#368
Github Author: daviskoh <koh.davis.0@gmail.com>

Test Plan: ./runJestTests
  • Loading branch information
daviskoh committed Apr 1, 2015
1 parent 2c4e9ab commit 28a0a25
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jest
.dontMock('path')
.dontMock('absolute-path')
.dontMock('../docblock')
.dontMock('../../requirePattern')
.setMock('../../../ModuleDescriptor', function(data) {return data;});

describe('DependencyGraph', function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var ModuleDescriptor = require('../../ModuleDescriptor');
var q = require('q');
var fs = require('fs');
var docblock = require('./docblock');
var requirePattern = require('../requirePattern');
var path = require('path');
var isAbsolutePath = require('absolute-path');
var debug = require('debug')('DependecyGraph');
Expand Down Expand Up @@ -600,7 +601,6 @@ DependecyGraph.prototype._processAssetChange = function(eventType, file) {
/**
* Extract all required modules from a `code` string.
*/
var requireRe = /\brequire\s*\(\s*[\'"]([^"\']+)["\']\s*\)/g;
var blockCommentRe = /\/\*(.|\n)*?\*\//g;
var lineCommentRe = /\/\/.+(\n|$)/g;
function extractRequires(code) {
Expand All @@ -609,7 +609,7 @@ function extractRequires(code) {
code
.replace(blockCommentRe, '')
.replace(lineCommentRe, '')
.replace(requireRe, function(match, dep) {
.replace(requirePattern, function(match, _, dep) {
deps.push(dep);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

jest.dontMock('../')
.dontMock('q')
.dontMock('../requirePattern')
.setMock('../../ModuleDescriptor', function(data) {return data;});

var q = require('q');
Expand Down Expand Up @@ -226,11 +227,13 @@ describe('HasteDependencyResolver', function() {
});

var depGraph = depResolver._depGraph;
var dependencies = ['x', 'y', 'z'];
var dependencies = ['x', 'y', 'z', 'a', 'b'];
var code = [
'require("x")',
'require("y")',
'require("z")',
'require( "z" )',
'require( "a")',
'require("b" )',
].join('\n');

depGraph.resolveDependency.mockImpl(function(fromModule, toModuleName) {
Expand All @@ -255,7 +258,9 @@ describe('HasteDependencyResolver', function() {
' require, requireDynamic, requireLazy, module, exports) {' +
' require(\'changed\')',
'require(\'y\')',
'require("z")});',
'require("z")',
'require("a")',
'require("b")});',
].join('\n'));
});
});
Expand Down
7 changes: 3 additions & 4 deletions react-packager/src/DependencyResolver/haste/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
'use strict';

var path = require('path');
var FileWatcher = require('../../FileWatcher');
var DependencyGraph = require('./DependencyGraph');
var requirePattern = require('./requirePattern');
var ModuleDescriptor = require('../ModuleDescriptor');
var declareOpts = require('../../lib/declareOpts');

Expand All @@ -25,7 +25,6 @@ var DEFINE_MODULE_CODE = [
].join('');

var DEFINE_MODULE_REPLACE_RE = /_moduleName_|_code_|_deps_/g;
var REL_REQUIRE_STMT = /require\(['"]([\.\/0-9A-Z_$\-]*)['"]\)/gi;

var validateOpts = declareOpts({
projectRoots: {
Expand Down Expand Up @@ -146,12 +145,12 @@ HasteDependencyResolver.prototype.wrapModule = function(module, code) {
}

var relativizedCode =
code.replace(REL_REQUIRE_STMT, function(codeMatch, depName) {
code.replace(requirePattern, function(codeMatch, _, depName) {
var depId = resolvedDeps[depName];
if (depId != null) {
return 'require(\'' + depId + '\')';
} else {
return codeMatch;
return codeMatch.replace(/\s+/g, '');
}
});

Expand Down
14 changes: 14 additions & 0 deletions react-packager/src/DependencyResolver/haste/requirePattern.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

'use strict';

var REQUIRE_RE = /\brequire\s*?\(\s*?([\'"])([^"\']+)\1\s*?\)/g;

module.exports = REQUIRE_RE;

0 comments on commit 28a0a25

Please sign in to comment.