-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
119 lines (103 loc) · 5.39 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*!
* requires-regex <https://github.com/jonschlinkert/requires-regex>
*
* Copyright (c) 2014-2018 Jon Schlinkert, contributors.
* Licensed under the MIT License
*/
'use strict';
require('mocha');
var assert = require('assert');
var re = require('./');
function match(str) {
var matches = [];
var regex = re();
var m;
while ((m = regex.exec(str))) {
if (!m[2] && !m[4]) continue;
var tok = {};
tok[m[2] || m[4]] = m[4];
matches.push(tok);
}
return matches;
}
describe('requires regex', function() {
it('should not match require statements in quotes', function() {
assert.deepEqual(match('"require(\'foo\');"'), []);
assert.deepEqual(match('"require(\'foo\')require(\'bar\');"'), []);
assert.deepEqual(match('"require("foo")const foo = require(\'bar\');"'), []);
assert.deepEqual(match('"require(\'foo\')const foo = require(\'bar\');"require("bar")'), [{bar: 'bar'}]);
});
it('should correctly detect the word "require" in statements in quoted strings', function() {
assert.deepEqual(match('"this is not a require statement"'), []);
assert.deepEqual(match('"this is not a require("") statement"'), []);
assert.deepEqual(match('"this is not a require() statement"'), []);
});
it('should detect require statements before and after quoted strings with "require"', function() {
assert.deepEqual(match('require("bar")"require(\'foo\');"require("baz")'), [{bar: 'bar'}, {baz: 'baz'}]);
assert.deepEqual(match('\nrequire("bar")"\nrequire(\'foo\');"\n\nrequire("baz")'), [{bar: 'bar'}, {baz: 'baz'}]);
assert.deepEqual(match('require("bar")"require(\'foo\');require(\'zzz\');"'), [{bar: 'bar'}]);
assert.deepEqual(match('require("bar")"require(\'foo\');"'), [{bar: 'bar'}]);
});
it('should match require statements with no leading characters', function() {
assert.deepEqual(match("const one = require('two');"), [{ one: 'two' }]);
assert.deepEqual(match("require('foo')"), [{ foo: 'foo' }]);
assert.deepEqual(match('require("foo")'), [{ foo: 'foo' }]);
assert.deepEqual(match('require("./")'), [{ './': './' }]);
});
it('should work with backticks', function() {
assert.deepEqual(match("const one = require(`two`);"), [{ one: 'two' }]);
assert.deepEqual(match("\n\nrequire(`foo`);"), [{ foo: 'foo' }]);
assert.deepEqual(match("\nrequire(`foo`);\n\n"), [{ foo: 'foo' }]);
assert.deepEqual(match("require(`foo`);"), [{ foo: 'foo' }]);
assert.deepEqual(match('require(`foo`)'), [{ foo: 'foo' }]);
});
it('should match requires with leading whitespace and parens', function() {
assert.deepEqual(match(' app.use(require("foo"));'), [{ foo: 'foo' }]);
assert.deepEqual(match('app.use(require("foo"));'), [{ foo: 'foo' }]);
assert.deepEqual(match(' .use(require("foo"));'), [{ foo: 'foo' }]);
});
it('should match variable name and module name', function() {
var a = re().exec("var isDir = require('is-directory');");
assert.equal(a[0], "var isDir = require('is-directory');");
assert.equal(a[2], 'isDir');
assert.equal(a[4], 'is-directory');
var b = re().exec("const isDir = require('is-directory')");
assert.equal(b[0], "const isDir = require('is-directory')");
assert.equal(b[2], 'isDir');
assert.equal(b[4], 'is-directory');
assert.deepEqual(match("const path = require('path')let list = require('dirs');"), [
{ path: 'path' },
{ list: 'dirs' }
]);
assert.deepEqual(match("require('path')\nrequire('dirs');"), [{ path: 'path' }, { dirs: 'dirs' }]);
assert.deepEqual(match('require("path")\nrequire("dirs");'), [{ path: 'path' }, { dirs: 'dirs' }]);
assert.deepEqual(match('require("path")require("dirs");'), [{ path: 'path' }, { dirs: 'dirs' }]);
assert.deepEqual(match('var foo = require("path")require("dirs");'), [{ foo: 'path' }, { dirs: 'dirs' }]);
assert.deepEqual(match('var\nfoo\n=\nrequire("path")require("dirs");'), [{ foo: 'path' }, { dirs: 'dirs' }]);
assert.deepEqual(match('var foo = require("path")require("dirs")'), [{ foo: 'path' }, { dirs: 'dirs' }]);
assert.deepEqual(match('foo = require("path")require("dirs")'), [{ foo: 'path' }, { dirs: 'dirs' }]);
assert.deepEqual(match('foo = require("path")bar = require("dirs")'), [{ foo: 'path' }, { bar: 'dirs' }]);
assert.deepEqual(match('foo = require("a-b")bar = require("c-d-e")'), [{ foo: 'a-b' }, { bar: 'c-d-e' }]);
assert.deepEqual(match('foo = require("./path")bar = require("./dirs")'), [{ foo: './path' }, { bar: './dirs' }]);
assert.deepEqual(match('const foo = require("./path")bar = require("./dirs")'), [
{ foo: './path' },
{ bar: './dirs' }
]);
});
it('should match indented variables', function() {
var str = " var path = require('path');\n\nvar list = require('dirs');";
assert.deepEqual(match(str), [{ path: 'path' }, { list: 'dirs' }]);
});
it('should match scoped names', function() {
var str = "const bar = require('@foo/bar')";
assert.deepEqual(match(str), [{ bar: '@foo/bar' }]);
});
it('should match requires in parens', function() {
var str = 'const bar = require(\'@foo/bar\')(require("@one/two"))';
assert.deepEqual(match(str), [{ bar: '@foo/bar' }, { '@one/two': '@one/two' }]);
});
it('should match scoped package names', function() {
var str = " var bar = require('@foo/bar');\n\nvar qux = require('@baz/qux');";
assert.deepEqual(match(str), [{ bar: '@foo/bar' }, { qux: '@baz/qux' }]);
});
});