Skip to content

Commit

Permalink
address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Jul 17, 2021
1 parent 73d8edb commit f48ceb5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 12 additions & 3 deletions src/actions/autoinstall/get-requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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`)
}
})

Expand Down
2 changes: 1 addition & 1 deletion test/mocks/deps/one.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
5 changes: 4 additions & 1 deletion test/unit/src/actions/autoinstall/get-lambda-deps-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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')
})

0 comments on commit f48ceb5

Please sign in to comment.