Skip to content

Commit 9006ddf

Browse files
committed
fix: scoped names validate blacklist/underscore/core module
1 parent 8123f73 commit 9006ddf

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ function validate (name) {
8080
errors.push('name cannot start with a period')
8181
}
8282

83+
if (pkg.match(/^_/)) {
84+
errors.push('name cannot start with an underscore')
85+
}
86+
87+
blacklist.forEach(function (blacklistedName) {
88+
if (pkg.toLowerCase() === blacklistedName) {
89+
errors.push(blacklistedName + ' is a blacklisted name')
90+
}
91+
})
92+
93+
if (builtins.includes(pkg.toLowerCase())) {
94+
warnings.push(pkg + ' is a core module name')
95+
}
96+
8397
if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
8498
return done(warnings, errors)
8599
}

test/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ test('validate-npm-package-name', function (t) {
2626
warnings: ['name can no longer contain special characters ("~\'!()*")'],
2727
})
2828

29+
// Scoped package validation for blacklisted names, underscore starts, and core modules
30+
31+
t.same(validate('@user/node_modules'), {
32+
validForNewPackages: false,
33+
validForOldPackages: false,
34+
errors: ['node_modules is a blacklisted name'],
35+
})
36+
37+
t.same(validate('@user/_package'), {
38+
validForNewPackages: false,
39+
validForOldPackages: false,
40+
errors: ['name cannot start with an underscore'],
41+
})
42+
43+
t.same(validate('@user/http'), {
44+
validForNewPackages: false,
45+
validForOldPackages: true,
46+
warnings: ['http is a core module name'],
47+
})
48+
2949
// Invalid
3050

3151
t.same(validate(null), {

0 commit comments

Comments
 (0)