diff --git a/package.json b/package.json index 836109c..4451c74 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "cpr": "~3.0.1", "esquery": "~1.3.1", "glob": "~7.1.6", - "package-name-regex": "~2.0.1", "rimraf": "~3.0.2", "run-series": "~1.1.9", "strip-ansi": "~6.0.0", diff --git a/src/actions/autoinstall/get-requires.js b/src/actions/autoinstall/get-requires.js index 3280b5f..0dd02df 100644 --- a/src/actions/autoinstall/get-requires.js +++ b/src/actions/autoinstall/get-requires.js @@ -2,7 +2,6 @@ let { readFileSync } = require('fs') let { builtinModules: builtins } = require('module') let loose = require('acorn-loose') let esquery = require('esquery') -let isPkg = require('package-name-regex') module.exports = function getRequires ({ dir, file, update }) { let contents = readFileSync(file).toString() @@ -51,13 +50,23 @@ module.exports = function getRequires ({ dir, file, update }) { // Filter invalid package calls, Architect shared + views, and Node.js builtins let isArcShared = /^@architect(\/|\\)(shared|views)/ + // https://www.npmjs.com/package/package-name-regex + let isPkg = /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/ called.forEach(r => { if (typeof r === 'string') { let pkg = getPackageName(r) - if (isPkg.test(pkg) && !isArcShared.test(pkg) && !builtins.includes(pkg)) { - deps.push(pkg) + if (pkg && isPkg.test(pkg)) { + if (!isArcShared.test(pkg) && !builtins.includes(pkg)) { + deps.push(pkg) + } } + else { + update.warn(`Invalid module string '${pkg}' in require call found in ${dir}`) + } + } + else { + update.warn(`Non-string argument '${r}' passed to require() call`) } }) diff --git a/test/mocks/deps/one.js b/test/mocks/deps/one.js index cb9db13..a4612d3 100644 --- a/test/mocks/deps/one.js +++ b/test/mocks/deps/one.js @@ -21,7 +21,7 @@ missingInit = 'ignoredmodule' require(missingInit) // When minifiers redefine variables, the original might be an invalid module string -let requiredThing = 'some thing ' +let requiredThing = 'invalid module string' requiredThing = 'validmodule' require(requiredThing) diff --git a/test/unit/src/actions/autoinstall/get-lambda-deps-tests.js b/test/unit/src/actions/autoinstall/get-lambda-deps-tests.js index 63935d6..4860f2a 100644 --- a/test/unit/src/actions/autoinstall/get-lambda-deps-tests.js +++ b/test/unit/src/actions/autoinstall/get-lambda-deps-tests.js @@ -12,7 +12,7 @@ test('Set up env', t => { }) test(`Walk a folder's deps`, t => { - t.plan(4) + t.plan(7) let stdout = process.stdout.write let data = '' process.stdout.write = write => { @@ -30,4 +30,7 @@ test(`Walk a folder's deps`, t => { t.equal(files.length, 6, 'Walked 6 js files') t.match(data, /'something'/, 'Warned about dynamic require') + t.match(data, /'missingInit'/, 'Warned about reassigned require argument string') + t.match(data, /'invalid module string'/, 'Warned about invalid module string') + t.match(data, /'1234'/, 'Warned about non-string require argument') })