Skip to content

Commit

Permalink
Proposed fix for systemjs#919
Browse files Browse the repository at this point in the history
- Fix code in cjs.js to correctly match last index of comments.
- Add test module that includes the require('') at end of comment string
- Add unit test to prove the module can be loaded OK with the comment present
  • Loading branch information
colmbrady committed Nov 18, 2015
1 parent 78ec7f2 commit dbf53dc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
function inLocation(locations, match, starts) {
var inLocation = false;
for (var i = 0; i < locations.length; i++)
if (locations[i][0] < match.index && locations[i][1] > match.index + (!starts ? match[0].length : 0))
if (locations[i][0] < match.index && locations[i][1] > match.index + (!starts ? (match[0].length - 1) : 0))
return true;
return false;
}
Expand Down
7 changes: 7 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,13 @@ asyncTest('CommonJS detection variation 1', function() {
}, err);
});

asyncTest('CommonJS module with require at end of comment loads OK', function() {
System['import']('tests/commonjs-requires-in-comment.js').then(function(m) {
ok(m);
start();
}, err);
});

if (!ie8)
asyncTest('CommonJS detection variation 2', function() {
System['import']('tests/commonjs-variation2.js').then(function(m) {
Expand Down
15 changes: 15 additions & 0 deletions test/tests/commonjs-requires-in-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"format cjs";

System.register("lib/module-with-comment", [], function($__export) {
"use strict";
var __moduleName = "lib/module-with-comment";
var shared;
return {
// This comment triggered SystemJS to do a require because of this -> require('')
setters: [function(m) {
shared = m.default;
}],
execute: function() {
}
};
});

0 comments on commit dbf53dc

Please sign in to comment.