# validate-npm-package-name

检测 npm 包名是否符合标准变量命名

# 环境准备

拉取代码

git clone https://github.com/npm/validate-npm-package-name.git
cd validate-npm-package-name
npm i

# 源码分析

这一次的项目很简单,validate-npm-package-name

test
  -- index.js 测试文件
index.js   项目文件
package.json npm配置文件

# package.json

"main": "index.js",
"scripts": {
    "cov:test": "TAP_FLAGS='--cov' npm run test:code",
    "test:code": "tap ${TAP_FLAGS:-'--'} test/*.js",
    "test:style": "standard",
    "test": "npm run test:code && npm run test:style"
},

在这里能够获得的信息是:

  1. 整个项目文件的入口: index.js
  2. 提供了四个npm脚本命令

# 关于tap和standard

  • tap:Node.js 的 TAP 测试框架。
  • standard: 用于JavaScript语法样式和格式化程序

# index.js

这个文件的代码量在100多行,就不进行折叠了。

'use strict'

// 正则表达式 --作用域的包名
var scopedPackagePattern = new RegExp('^(?:@([^/]+?)[/])?([^/]+?)$')
// 内部关键字
var builtins = require('builtins')
// 黑名单
var blacklist = [
  'node_modules',
  'favicon.ico'
]

// 模块导出
var validate = module.exports = function (name) {
  var warnings = []
  var errors = []

  if (name === null) {
    errors.push('name cannot be null')
    return done(warnings, errors)
  }

  if (name === undefined) {
    errors.push('name cannot be undefined')
    return done(warnings, errors)
  }

  if (typeof name !== 'string') {
    errors.push('name must be a string')
    return done(warnings, errors)
  }

  if (!name.length) {
    errors.push('name length must be greater than zero')
  }

  if (name.match(/^\./)) {
    errors.push('name cannot start with a period')
  }

  if (name.match(/^_/)) {
    errors.push('name cannot start with an underscore')
  }
  // 首尾空格
  if (name.trim() !== name) {
    errors.push('name cannot contain leading or trailing spaces')
  }

  // No funny business
  blacklist.forEach(function (blacklistedName) {
    if (name.toLowerCase() === blacklistedName) {
      errors.push(blacklistedName + ' is a blacklisted name')
    }
  })

  // Generate warnings for stuff that used to be allowed

  // core module names like http, events, util, etc
  builtins.forEach(function (builtin) {
    if (name.toLowerCase() === builtin) {
      warnings.push(builtin + ' is a core module name')
    }
  })

  // really-long-package-names-------------------------------such--length-----many---wow
  // the thisisareallyreallylongpackagenameitshouldpublishdowenowhavealimittothelengthofpackagenames-poch.
  if (name.length > 214) {
    warnings.push('name can no longer contain more than 214 characters')
  }

  // mIxeD CaSe nAMEs
  if (name.toLowerCase() !== name) {
    warnings.push('name can no longer contain capital letters')
  }

  if (/[~'!()*]/.test(name.split('/').slice(-1)[0])) {
    warnings.push('name can no longer contain special characters ("~\'!()*")')
  }

  if (encodeURIComponent(name) !== name) {
    // Maybe it's a scoped package name, like @user/package
    var nameMatch = name.match(scopedPackagePattern)
    if (nameMatch) {
      var user = nameMatch[1]
      var pkg = nameMatch[2]
      if (encodeURIComponent(user) === user && encodeURIComponent(pkg) === pkg) {
        return done(warnings, errors)
      }
    }

    errors.push('name can only contain URL-friendly characters')
  }

  return done(warnings, errors)
}

validate.scopedPackagePattern = scopedPackagePattern

// 根据产生的wawring errors返回结果
var done = function (warnings, errors) {
  var result = {
    validForNewPackages: errors.length === 0 && warnings.length === 0,
    validForOldPackages: errors.length === 0,
    warnings: warnings,
    errors: errors
  }
  if (!result.warnings.length) delete result.warnings
  if (!result.errors.length) delete result.errors
  return result
}

执行之后,会出现错误的是:

  1. 名字为null,undefined
  2. 名字类型不为字符串
  3. 名字长度为0,名字以.,_开头, 或者首尾存在空格
  4. 名字存在黑名单中,(不在乎大小写)
  5. 名字经过 encodeURIComponent 编码不能改变

会出现warngin:

  1. 已存在的关键字
  2. 名字长度超过214
  3. 名字包含~ ! () * '特殊字符
  4. 名字包含大写字符

# 参考资料

  1. validate-npm-package-name github (opens new window)
  2. 每天一个npm包:validate-npm-package-name
  3. https://www.yuque.com/ruochuan12/group11/dvf80r
  4. npm tap (opens new window)