Skip to content

Commit fee6521

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

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Below is a list of rules that valid `npm` package name should conform to.
2929
- package name should not start with `.` or `_`
3030
- package name should *not* contain any spaces
3131
- package name should *not* contain any of the following characters: `~)('!*`
32-
- package name *cannot* be the same as a node.js/io.js core module nor a reserved/blacklisted name. For example, the following names are invalid:
32+
- package name *cannot* be the same as a node.js/io.js core module nor a reserved/excluded name. For example, the following names are invalid:
3333
+ http
3434
+ stream
3535
+ node_modules

lib/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { builtinModules: builtins } = require('module')
33

44
var scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$')
5-
var blacklist = [
5+
var exclusionList = [
66
'node_modules',
77
'favicon.ico',
88
]
@@ -43,9 +43,9 @@ function validate (name) {
4343
}
4444

4545
// No funny business
46-
blacklist.forEach(function (blacklistedName) {
47-
if (name.toLowerCase() === blacklistedName) {
48-
errors.push(blacklistedName + ' is a blacklisted name')
46+
exclusionList.forEach(function (excludedName) {
47+
if (name.toLowerCase() === excludedName) {
48+
errors.push(excludedName + ' is not a valid package name')
4949
}
5050
})
5151

@@ -80,6 +80,19 @@ 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+
exclusionList.forEach(function (excludedName) {
88+
if (pkg.toLowerCase() === excludedName) {
89+
errors.push(excludedName + ' is not a valid package name')
90+
}
91+
})
92+
93+
// Note: Core module names are allowed in scoped packages
94+
// This is intentional - @user/http is a valid package name
95+
8396
if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
8497
return done(warnings, errors)
8598
}

test/index.js

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

29+
// Scoped package validation for excluded 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 not a valid package 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: true,
45+
validForOldPackages: true,
46+
})
47+
2948
// Invalid
3049

3150
t.same(validate(null), {
@@ -98,12 +117,12 @@ test('validate-npm-package-name', function (t) {
98117
t.same(validate('node_modules'), {
99118
validForNewPackages: false,
100119
validForOldPackages: false,
101-
errors: ['node_modules is a blacklisted name'] })
120+
errors: ['node_modules is not a valid package name'] })
102121

103122
t.same(validate('favicon.ico'), {
104123
validForNewPackages: false,
105124
validForOldPackages: false,
106-
errors: ['favicon.ico is a blacklisted name'] })
125+
errors: ['favicon.ico is not a valid package name'] })
107126

108127
// Node/IO Core
109128

0 commit comments

Comments
 (0)