Skip to content

Commit

Permalink
fix(utils): [findRequires] array item extraction
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Feb 22, 2023
1 parent e12e977 commit e719d34
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/utils/__snapshots__/find-requires.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,17 @@ exports[`unit:utils/findRequires > should return RequireStatement object array 1
"start": 147,
"syntax": "require",
},
{
"code": "require(\\"./arr\\")",
"end": 214,
"imports": [],
"kind": "require",
"specifier": "./arr",
"specifier_kind": "relative",
"specifier_syntax": "static",
"start": 198,
"syntax": "require",
},
{
"code": "const {
default: myDefault,
Expand All @@ -86,7 +97,7 @@ exports[`unit:utils/findRequires > should return RequireStatement object array 1
squareThree,
squareTwo
} = require('./lib')",
"end": 327,
"end": 358,
"imports": [
"default: myDefault",
"addFive",
Expand All @@ -102,7 +113,7 @@ exports[`unit:utils/findRequires > should return RequireStatement object array 1
"specifier": "./lib",
"specifier_kind": "relative",
"specifier_syntax": "static",
"start": 185,
"start": 216,
"syntax": "require",
},
]
Expand Down
3 changes: 2 additions & 1 deletion src/utils/__tests__/find-requires.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('unit:utils/findRequires', () => {
const mod = require('lib');
require(id)
require('#src/utils/resolve-aliases')
const arr = [require("./arr")]
const {
default: myDefault,
addFive,
Expand All @@ -32,7 +33,7 @@ describe('unit:utils/findRequires', () => {
const results = testSubject(code)

// Expect
expect(results).to.be.an('array').of.length(7)
expect(results).to.be.an('array').of.length(8)
expect(results).toMatchSnapshot()
})

Expand Down
2 changes: 1 addition & 1 deletion src/utils/find-requires.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const findRequires = (code: string = ''): RequireStatement[] => {
* @const {RegExp} REQUIRE_REGEX
*/
const REQUIRE_REGEX: RegExp =
/(?<=^|[\s;])\b(?:(?:const\s*|let\s*|var\s*)?(?<imports>(?:[$_\p{ID_Start}][$\u200C\u200D\p{ID_Continue}]*)|(?:[\w\t\n\r "$'*,./:{}-]+?))?\s*=?\s*(?<kind>require)\((?<specifier>["']?[\S\t\n\r]+?["']?)\)(?=;?\n?))(?<!(?:\/\/|\*).*)/gu
/(?<=^|[\s;[])\b(?:(?:const\s*|let\s*|var\s*)?(?<imports>(?:[$_\p{ID_Start}][$\u200C\u200D\p{ID_Continue}]*)|(?:[\w\t\n\r "$'*,./:{}-]+?))?\s*=?\s*(?<kind>require)\((?<specifier>["']?[\S\t\n\r]+?["']?)\)(?=;?\n?))(?<!(?:\/\/|\*).*)/gu

return [...code.matchAll(REQUIRE_REGEX)].map(match => {
const { 0: code = '', index: start = 0, groups = {} } = match
Expand Down

0 comments on commit e719d34

Please sign in to comment.