Skip to content

Commit cd74707

Browse files
author
Mikolaj Dadela
committed
A bugfix and one more test.
1 parent 828436c commit cd74707

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

loaders/examples.loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var marked = require('marked');
44
var evalPlaceholder = '<%{#eval#}%>';
55
var codePlaceholder = '<%{#code#}%>';
66

7-
var requireAnythingRegex = /require\s*\((.+)\)/g;
7+
var requireAnythingRegex = /require\s*\(([^)]+)\)/g;
88
var simpleStringRegex = /^"([^"]+)"$|^'([^']+)'$/;
99

1010
function readExamples(markdown) {

test/examples.loader.spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ describe('examples loader', () => {
3030
expect(`var f = require;`).not.to.match(regex);
3131
expect(`require.call(module, "foo")`).not.to.match(regex);
3232
});
33+
34+
it('should match many requires in the same line correctly', () => {
35+
var replaced = `require('foo');require('bar')`.replace(examplesLoader.requireAnythingRegex, 'x');
36+
expect(replaced).to.equal('x;x');
37+
});
3338
});
3439

3540
describe('simpleStringRegex', () => {
@@ -59,6 +64,9 @@ describe('examples loader', () => {
5964
it('should find calls to require in code', () => {
6065
let findRequires = examplesLoader.findRequires;
6166
expect(findRequires(`require('foo')`)).to.deep.equal(['foo']);
67+
expect(findRequires(`require('./foo')`)).to.deep.equal(['./foo']);
68+
expect(findRequires(`require('foo');require('bar')`)).to.deep.equal(['foo', 'bar']);
69+
expect(() => findRequires(`require('foo' + 'bar')`)).to.throw(Error);
6270
});
6371
});
6472

0 commit comments

Comments
 (0)