Skip to content

Commit

Permalink
update lint rules to match @npmcli/arborist
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Oct 23, 2020
1 parent b737ee9 commit 47640eb
Show file tree
Hide file tree
Showing 98 changed files with 1,118 additions and 1,195 deletions.
48 changes: 20 additions & 28 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"parserOptions": {
"ecmaVersion": 2020,
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module"
"ecmaVersion": 2018,
"ecmaFeatures": {},
"sourceType": "script"
},

"env": {
Expand All @@ -30,20 +28,20 @@
"array-bracket-spacing": ["error", "never"],
"arrow-spacing": ["error", { "before": true, "after": true }],
"block-spacing": ["error", "always"],
"brace-style": ["error", "1tbs", { "allowSingleLine": true }],
"brace-style": ["error", "1tbs", { "allowSingleLine": false }],
"camelcase": ["error", { "properties": "never" }],
"comma-dangle": ["error", {
"arrays": "never",
"objects": "never",
"imports": "never",
"exports": "never",
"arrays": "always-multiline",
"objects": "always-multiline",
"imports": "always-multiline",
"exports": "always-multiline",
"functions": "never"
}],
"comma-spacing": ["error", { "before": false, "after": true }],
"comma-style": ["error", "last"],
"computed-property-spacing": ["error", "never"],
"constructor-super": "error",
"curly": ["error", "multi-line"],
"curly": ["error", "multi-or-nest"],
"dot-location": ["error", "property"],
"dot-notation": ["error", { "allowKeywords": true }],
"eol-last": "error",
Expand Down Expand Up @@ -77,7 +75,7 @@
"no-case-declarations": "error",
"no-class-assign": "error",
"no-compare-neg-zero": "error",
"no-cond-assign": "error",
"no-cond-assign": "off",
"no-const-assign": "error",
"no-constant-condition": ["error", { "checkLoops": false }],
"no-control-regex": "error",
Expand All @@ -104,25 +102,18 @@
"no-invalid-regexp": "error",
"no-irregular-whitespace": "error",
"no-iterator": "error",
"no-labels": ["error", { "allowLoop": false, "allowSwitch": false }],
"no-labels": ["error", { "allowLoop": true, "allowSwitch": false }],
"no-lone-blocks": "error",
"no-misleading-character-class": "error",
"no-prototype-builtins": "error",
"no-useless-catch": "error",
"no-mixed-operators": ["error", {
"groups": [
["==", "!=", "===", "!==", ">", ">=", "<", "<="],
["&&", "||"],
["in", "instanceof"]
],
"allowSamePrecedence": true
}],
"no-mixed-operators": "off",
"no-mixed-spaces-and-tabs": "error",
"no-multi-spaces": "error",
"no-multi-str": "error",
"no-multiple-empty-lines": ["error", { "max": 1, "maxEOF": 0 }],
"no-negated-in-lhs": "error",
"no-new": "error",
"no-new": "off",
"no-new-func": "error",
"no-new-object": "error",
"no-new-require": "error",
Expand All @@ -135,8 +126,8 @@
"no-proto": "error",
"no-redeclare": ["error", { "builtinGlobals": false }],
"no-regex-spaces": "error",
"no-return-assign": ["error", "except-parens"],
"no-self-assign": ["error", { "props": true }],
"no-return-assign": "off",
"no-self-assign": "off",
"no-self-compare": "error",
"no-sequences": "error",
"no-shadow-restricted-names": "error",
Expand Down Expand Up @@ -166,16 +157,17 @@
"no-void": "error",
"no-whitespace-before-property": "error",
"no-with": "error",
"object-curly-newline": ["error", { "multiline": true, "consistent": true }],
"object-curly-spacing": ["error", "always"],
"nonblock-statement-body-position": [2, "below"],
"object-curly-newline": "off",
"object-curly-spacing": "off",
"object-property-newline": ["error", { "allowMultiplePropertiesPerLine": true }],
"one-var": ["error", { "initialized": "never" }],
"operator-linebreak": ["error", "after", { "overrides": { "?": "before", ":": "before", "|>": "before" } }],
"operator-linebreak": "off",
"padded-blocks": ["error", { "blocks": "never", "switches": "never", "classes": "never" }],
"prefer-const": ["error", {"destructuring": "all"}],
"prefer-promise-reject-errors": "error",
"quote-props": ["error", "as-needed"],
"quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": false }],
"quotes": ["error", "single", { "avoidEscape": true, "allowTemplateLiterals": true }],
"rest-spread-spacing": ["error", "never"],
"semi": ["error", "never"],
"semi-spacing": ["error", { "before": false, "after": true }],
Expand Down
43 changes: 17 additions & 26 deletions lib/access.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ const subcommands = [
'ls-collaborators',
'edit',
'2fa-required',
'2fa-not-required'
'2fa-not-required',
]

const UsageError = (msg) =>
Object.assign(new Error(`\nUsage: ${msg}\n\n` + usage), {
code: 'EUSAGE'
code: 'EUSAGE',
})

const cmd = (args, cb) =>
Expand All @@ -53,30 +53,27 @@ const cmd = (args, cb) =>
const access = async ([cmd, ...args], cb) => {
const fn = subcommands.includes(cmd) && access[cmd]

if (!cmd) {
if (!cmd)
throw UsageError('Subcommand is required.')
}

if (!fn) {
if (!fn)
throw UsageError(`${cmd} is not a recognized subcommand.`)
}

return fn(args, { ...npm.flatOptions })
}

const completion = function (opts, cb) {
var argv = opts.conf.argv.remain
if (argv.length === 2) {
if (argv.length === 2)
return cb(null, subcommands)
}

switch (argv[2]) {
case 'grant':
if (argv.length === 3) {
if (argv.length === 3)
return cb(null, ['read-only', 'read-write'])
} else {
else
return cb(null, [])
}

case 'public':
case 'restricted':
case 'ls-packages':
Expand All @@ -98,13 +95,11 @@ access.restricted = ([pkg], opts) =>
modifyPackage(pkg, opts, libaccess.restricted)

access.grant = async ([perms, scopeteam, pkg], opts) => {
if (!perms || (perms !== 'read-only' && perms !== 'read-write')) {
if (!perms || (perms !== 'read-only' && perms !== 'read-write'))
throw UsageError('First argument must be either `read-only` or `read-write`.')
}

if (!scopeteam) {
if (!scopeteam)
throw UsageError('`<scope:team>` argument is required.')
}

const [, scope, team] = scopeteam.match(/^@?([^:]+):(.*)$/) || []

Expand All @@ -120,9 +115,8 @@ access.grant = async ([perms, scopeteam, pkg], opts) => {
}

access.revoke = async ([scopeteam, pkg], opts) => {
if (!scopeteam) {
if (!scopeteam)
throw UsageError('`<scope:team>` argument is required.')
}

const [, scope, team] = scopeteam.match(/^@?([^:]+):(.*)$/) || []

Expand All @@ -144,9 +138,8 @@ access['2fa-not-required'] = access.tfaNotRequired = ([pkg], opts) =>
modifyPackage(pkg, opts, libaccess.tfaNotRequired, false)

access['ls-packages'] = access.lsPackages = async ([owner], opts) => {
if (!owner) {
if (!owner)
owner = await getIdentity(opts)
}

const pkgs = await libaccess.lsPackages(owner, opts)

Expand All @@ -170,9 +163,9 @@ const modifyPackage = (pkg, opts, fn, requireScope = true) =>
.then(pkgName => otplease(opts, opts => fn(pkgName, opts)))

const getPackage = async (name, requireScope) => {
if (name && name.trim()) {
if (name && name.trim())
return name.trim()
} else {
else {
try {
const pkg = await readPackageJson(path.resolve(prefix, 'package.json'))
name = pkg.name
Expand All @@ -181,16 +174,14 @@ const getPackage = async (name, requireScope) => {
throw new Error(
'no package name passed to command and no package.json found'
)
} else {
} else
throw err
}
}

if (requireScope && !name.match(/^@[^/]+\/.*$/)) {
if (requireScope && !name.match(/^@[^/]+\/.*$/))
throw UsageError('This command is only available for scoped packages.')
} else {
else
return name
}
}
}

Expand Down
15 changes: 6 additions & 9 deletions lib/adduser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const authTypes = {
legacy: require('./auth/legacy.js'),
oauth: require('./auth/oauth.js'),
saml: require('./auth/saml.js'),
sso: require('./auth/sso.js')
sso: require('./auth/sso.js'),
}

const usage = usageUtil(
Expand All @@ -24,29 +24,26 @@ const getRegistry = ({ scope, registry }) => {
if (scope) {
const scopedRegistry = npm.config.get(`${scope}:registry`)
const cliRegistry = npm.config.get('registry', 'cli')
if (scopedRegistry && !cliRegistry) {
if (scopedRegistry && !cliRegistry)
return scopedRegistry
}
}
return registry
}

const getAuthType = ({ authType }) => {
const type = authTypes[authType]

if (!type) {
if (!type)
throw new Error('no such auth module')
}

return type
}

const updateConfig = async ({ newCreds, registry, scope }) => {
npm.config.delete('_token', 'user') // prevent legacy pollution

if (scope) {
if (scope)
npm.config.set(scope + ':registry', registry, 'user')
}

npm.config.setCredentialsByURI(registry, newCreds)
await npm.config.save('user')
Expand All @@ -63,13 +60,13 @@ const adduser = async (args) => {
const { message, newCreds } = await auth({
creds,
registry,
scope
scope,
})

await updateConfig({
newCreds,
registry,
scope
scope,
})

output(message)
Expand Down
11 changes: 5 additions & 6 deletions lib/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ const audit = async args => {
const arb = new Arborist({
...npm.flatOptions,
audit: true,
path: npm.prefix
path: npm.prefix,
})
const fix = args[0] === 'fix'
await arb.audit({ fix })
if (fix) {
if (fix)
reifyOutput(arb)
} else {
else {
// will throw if there's an error, because this is an audit command
auditError(arb.auditReport)
const reporter = npm.flatOptions.json ? 'json' : 'detail'
const result = auditReport(arb.auditReport, {
...npm.flatOptions,
reporter
reporter,
})
process.exitCode = process.exitCode || result.exitCode
output(result.report)
Expand All @@ -41,9 +41,8 @@ const usage = usageUtil(
const completion = (opts, cb) => {
const argv = opts.conf.argv.remain

if (argv.length === 2) {
if (argv.length === 2)
return cb(null, ['fix'])
}

switch (argv[2]) {
case 'fix':
Expand Down
17 changes: 8 additions & 9 deletions lib/auth/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,10 @@ const login = async (opts) => {
opts
)
} catch (err) {
if (err.code === 'EOTP') {
if (err.code === 'EOTP')
newUser = await requestOTP()
} else {
else
throw err
}
}

return newUser
Expand All @@ -65,20 +64,20 @@ const login = async (opts) => {
opts.creds.username &&
opts.creds.password &&
opts.creds.email)
if (err.code === 'EOTP') {
if (err.code === 'EOTP')
res = await requestOTP()
} else if (needsMoreInfo) {
else if (needsMoreInfo)
throw err
} else {
else {
// TODO: maybe this needs to check for err.code === 'E400' instead?
res = await addNewUser()
}
}

const newCreds = {}
if (res && res.token) {
if (res && res.token)
newCreds.token = res.token
} else {
else {
newCreds.username = opts.creds.username
newCreds.password = opts.creds.password
newCreds.email = opts.creds.email
Expand All @@ -94,7 +93,7 @@ const login = async (opts) => {

return {
message,
newCreds
newCreds,
}
}

Expand Down
Loading

0 comments on commit 47640eb

Please sign in to comment.