Skip to content

Commit

Permalink
standard
Browse files Browse the repository at this point in the history
  • Loading branch information
feross committed Oct 29, 2020
1 parent 93906eb commit c37df35
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 56 deletions.
16 changes: 8 additions & 8 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

module.exports = Cli

var minimist = require('minimist')
var getStdin = require('get-stdin')
const minimist = require('minimist')
const getStdin = require('get-stdin')

function Cli (opts) {
var Linter = require('../').linter
var standard = opts.linter || new Linter(opts)
const Linter = require('../').linter
const standard = opts.linter || new Linter(opts)

opts = Object.assign({
cmd: 'standard-engine',
tagline: 'JavaScript Custom Style',
version: '0.0.0'
}, opts)

var argv = minimist(process.argv.slice(2), {
const argv = minimist(process.argv.slice(2), {
alias: {
global: 'globals',
plugin: 'plugins',
Expand Down Expand Up @@ -83,7 +83,7 @@ Flags (advanced):
return
}

var lintOpts = {
const lintOpts = {
fix: argv.fix,
extensions: argv.ext,
globals: argv.global,
Expand All @@ -92,7 +92,7 @@ Flags (advanced):
parser: argv.parser
}

var stdinText
let stdinText

if (argv.stdin) {
getStdin().then(function (text) {
Expand Down Expand Up @@ -124,7 +124,7 @@ Flags (advanced):
console.error('%s: %s (%s)', opts.cmd, opts.tagline, opts.homepage)

// Are any fixable rules present?
var isFixable = result.results.some(function (result) {
const isFixable = result.results.some(function (result) {
return result.messages.some(function (message) {
return !!message.fix
})
Expand Down
36 changes: 18 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ module.exports.cli = require('./bin/cmd')

module.exports.linter = Linter

var os = require('os')
var path = require('path')
var pkgConf = require('pkg-conf')
var fs = require('fs')
const os = require('os')
const path = require('path')
const pkgConf = require('pkg-conf')
const fs = require('fs')

var CACHE_HOME = require('xdg-basedir').cache || os.tmpdir()
const CACHE_HOME = require('xdg-basedir').cache || os.tmpdir()

var DEFAULT_EXTENSIONS = [
const DEFAULT_EXTENSIONS = [
'.js',
'.jsx',
'.mjs',
'.cjs'
]

var DEFAULT_IGNORE = [
const DEFAULT_IGNORE = [
'**/*.min.js',
'coverage/**',
'node_modules/**',
Expand All @@ -36,11 +36,11 @@ function Linter (opts) {
this.cwd = opts.cwd || process.cwd()
this.customParseOpts = opts.parseOpts

var m = opts.version && opts.version.match(/^(\d+)\./)
var majorVersion = (m && m[1]) || '0'
const m = opts.version && opts.version.match(/^(\d+)\./)
const majorVersion = (m && m[1]) || '0'

// Example cache location: ~/.cache/standard/v12/
var cacheLocation = path.join(CACHE_HOME, this.cmd, `v${majorVersion}/`)
const cacheLocation = path.join(CACHE_HOME, this.cmd, `v${majorVersion}/`)

this.eslintConfig = Object.assign({
cache: true,
Expand Down Expand Up @@ -80,7 +80,7 @@ Linter.prototype.lintTextSync = function (text, opts) {

Linter.prototype.lintText = function (text, opts, cb) {
if (typeof opts === 'function') return this.lintText(text, null, opts)
var result
let result
try {
result = this.lintTextSync(text, opts)
} catch (err) {
Expand All @@ -105,14 +105,14 @@ Linter.prototype.lintText = function (text, opts, cb) {
* @param {function(Error, Object)} cb callback
*/
Linter.prototype.lintFiles = function (files, opts, cb) {
var self = this
const self = this
if (typeof opts === 'function') return self.lintFiles(files, null, opts)
opts = self.parseOpts(opts)

if (typeof files === 'string') files = [files]
if (files.length === 0) files = ['.']

var result
let result
try {
result = new self.eslint.CLIEngine(opts.eslintConfig).executeOnFiles(files)
} catch (err) {
Expand All @@ -127,7 +127,7 @@ Linter.prototype.lintFiles = function (files, opts, cb) {
}

Linter.prototype.parseOpts = function (opts) {
var self = this
const self = this

opts = {
eslintConfig: { ...self.eslintConfig },
Expand All @@ -148,8 +148,8 @@ Linter.prototype.parseOpts = function (opts) {
opts.eslintConfig.cwd = opts.cwd
opts.eslintConfig.fix = opts.fix

var packageOpts = {}
var rootPath = null
let packageOpts = {}
let rootPath = null

if (opts.usePackageJson || opts.useGitIgnore) {
packageOpts = pkgConf.sync(self.cmd, { cwd: opts.cwd })
Expand Down Expand Up @@ -199,9 +199,9 @@ Linter.prototype.parseOpts = function (opts) {
setParser(packageOpts.parser || opts.parser)

if (self.customParseOpts) {
var rootDir
let rootDir
if (opts.usePackageJson) {
var filePath = pkgConf.filepath(packageOpts)
const filePath = pkgConf.filepath(packageOpts)
rootDir = filePath ? path.dirname(filePath) : opts.cwd
} else {
rootDir = opts.cwd
Expand Down
24 changes: 12 additions & 12 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var eslint = require('eslint')
var Linter = require('../').linter
var path = require('path')
var test = require('tape')
const eslint = require('eslint')
const Linter = require('../').linter
const path = require('path')
const test = require('tape')

function getStandard () {
return new Linter({
Expand All @@ -14,7 +14,7 @@ function getStandard () {

test('api: lintFiles', function (t) {
t.plan(3)
var standard = getStandard()
const standard = getStandard()
standard.lintFiles([], { cwd: path.join(__dirname, '../bin') }, function (err, result) {
t.error(err, 'no error while linting')
t.equal(typeof result, 'object', 'result is an object')
Expand All @@ -24,7 +24,7 @@ test('api: lintFiles', function (t) {

test('api: lintText', function (t) {
t.plan(3)
var standard = getStandard()
const standard = getStandard()
standard.lintText('console.log("hi there")\n', function (err, result) {
t.error(err, 'no error while linting')
t.equal(typeof result, 'object', 'result is an object')
Expand All @@ -34,24 +34,24 @@ test('api: lintText', function (t) {

test('api: lintTextSync', function (t) {
t.plan(2)
var standard = getStandard()
var result = standard.lintTextSync('console.log("hi there")\n')
const standard = getStandard()
const result = standard.lintTextSync('console.log("hi there")\n')
t.equal(typeof result, 'object', 'result is an object')
t.equal(result.errorCount, 1, 'should have used single quotes')
})

test('api: parseOpts -- avoid self.eslintConfig parser mutation', function (t) {
t.plan(2)
var standard = getStandard()
var opts = standard.parseOpts({ parser: 'blah' })
const standard = getStandard()
const opts = standard.parseOpts({ parser: 'blah' })
t.equal(opts.parser, 'blah')
t.equal(standard.eslintConfig.parser, undefined)
})

test('api: parseOpts -- avoid self.eslintConfig global mutation', function (t) {
t.plan(2)
var standard = getStandard()
var opts = standard.parseOpts({ globals: ['what'] })
const standard = getStandard()
const opts = standard.parseOpts({ globals: ['what'] })
t.deepEqual(opts.globals, ['what'])
t.deepEqual(standard.eslintConfig.globals, [])
})
30 changes: 15 additions & 15 deletions test/clone.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env node

var crossSpawn = require('cross-spawn')
var fs = require('fs')
var path = require('path')
var test = require('tape')
const crossSpawn = require('cross-spawn')
const fs = require('fs')
const path = require('path')
const test = require('tape')

var GIT = 'git'
var STANDARD = path.join(__dirname, 'lib', 'standard-cmd.js')
var TMP = path.join(__dirname, '..', 'tmp')
const GIT = 'git'
const STANDARD = path.join(__dirname, 'lib', 'standard-cmd.js')
const TMP = path.join(__dirname, '..', 'tmp')

const pkg = {
name: 'standard',
Expand All @@ -19,9 +19,9 @@ test('test `standard` repo', function (t) {

fs.mkdirSync(TMP, { recursive: true })

var name = pkg.name
var url = pkg.repo + '.git'
var folder = path.join(TMP, name)
const name = pkg.name
const url = pkg.repo + '.git'
const folder = path.join(TMP, name)
fs.access(path.join(TMP, name), fs.R_OK | fs.W_OK, function (err) {
downloadPackage(function (err) {
if (err) throw err
Expand All @@ -34,26 +34,26 @@ test('test `standard` repo', function (t) {
}

function gitClone (cb) {
var args = ['clone', '--depth', 1, url, path.join(TMP, name)]
const args = ['clone', '--depth', 1, url, path.join(TMP, name)]
spawn(GIT, args, { stdio: 'ignore' }, function (err) {
if (err) err.message += ' (git clone) (' + name + ')'
cb(err)
})
}

function gitPull (cb) {
var args = ['pull']
const args = ['pull']
spawn(GIT, args, { cwd: folder, stdio: 'ignore' }, function (err) {
if (err) err.message += ' (git pull) (' + name + ')'
cb(err)
})
}

function runStandard () {
var args = ['--verbose']
const args = ['--verbose']
if (pkg.args) args.push.apply(args, pkg.args)
spawn(STANDARD, args, { cwd: folder }, function (err) {
var str = name + ' (' + pkg.repo + ')'
const str = name + ' (' + pkg.repo + ')'
if (err) { t.fail(str) } else { t.pass(str) }
})
}
Expand All @@ -63,7 +63,7 @@ test('test `standard` repo', function (t) {
function spawn (command, args, opts, cb) {
if (!opts.stdio) opts.stdio = 'inherit'

var child = crossSpawn(command, args, opts)
const child = crossSpawn(command, args, opts)
child.on('error', cb)
child.on('close', function (code) {
if (code !== 0) return cb(new Error('non-zero exit code: ' + code))
Expand Down
6 changes: 3 additions & 3 deletions test/lib/standard-cmd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
var path = require('path')
var eslint = require('eslint')
var opts = {
const path = require('path')
const eslint = require('eslint')
const opts = {
cmd: 'pocketlint',
version: '0.0.0',
eslint,
Expand Down

0 comments on commit c37df35

Please sign in to comment.