Skip to content

Commit

Permalink
feat: usage of ES6 class syntax (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
theoludwig committed Sep 14, 2021
1 parent 581a9d7 commit b92f2da
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 138 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ Create the files below and fill in your own values for `options.js`.

```js
// programmatic usage
var Linter = require('standard-engine').linter
var opts = require('./options.js')
const { Linter } = require('standard-engine')
const opts = require('./options.js')
module.exports = new Linter(opts)
```

Expand All @@ -52,16 +52,17 @@ module.exports = new Linter(opts)
```js
#!/usr/bin/env node

var opts = require('../options.js')
const opts = require('../options.js')

require('standard-engine').cli(opts)
```

### `options.js`

```js
var eslint = require('eslint')
var path = require('path')
var pkg = require('./package.json')
const eslint = require('eslint')
const path = require('path')
const pkg = require('./package.json')

module.exports = {
// homepage, version and bugs pulled from package.json
Expand All @@ -78,7 +79,7 @@ module.exports = {
}
```

Additionally an optional `parseOpts()` function can be provided. See below for details.
Additionally an optional `resolveEslintConfig()` function can be provided. See below for details.

### `eslintrc.json`

Expand Down Expand Up @@ -164,7 +165,7 @@ a `ignore` property to `package.json`:
Some files are ignored by default:

```js
var DEFAULT_IGNORE = [
const DEFAULT_IGNORE = [
'*.min.js',
'coverage/',
'node_modules/',
Expand Down Expand Up @@ -270,9 +271,9 @@ install `babel-eslint` globally with `npm install babel-eslint -g`.
You can provide a `resolveEslintConfig()` function in the `options.js` exports:

```js
var eslint = require('eslint')
var path = require('path')
var pkg = require('./package.json')
const eslint = require('eslint')
const path = require('path')
const pkg = require('./package.json')

module.exports = {
// homepage, version and bugs pulled from package.json
Expand Down Expand Up @@ -330,7 +331,7 @@ The `callback` will be called with an `Error` and `results` object.
The `results` object will contain the following properties:

```js
var results = {
const results = {
results: [
{
filePath: '',
Expand Down
11 changes: 5 additions & 6 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#!/usr/bin/env node

module.exports = Cli

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

/**
* @typedef StandardCliOptions
* @property {import('../').linter} [linter]
* @property {import('../').Linter} [linter]
* @property {string} [cmd]
* @property {string} [tagline]
* @property {string} [homepage]
Expand All @@ -17,16 +15,15 @@ const getStdin = require('get-stdin')
/**
* @param {Omit<import('../').LinterOptions, 'cmd'> & StandardCliOptions} rawOpts
*/
function Cli (rawOpts) {
function cli (rawOpts) {
const opts = {
cmd: 'standard-engine',
tagline: 'JavaScript Custom Style',
version: '0.0.0',
...rawOpts
}

const Linter = require('../').linter
const standard = rawOpts.linter || new Linter(opts)
const standard = rawOpts.linter || new (require('../').Linter)(opts)

const argv = minimist(process.argv.slice(2), {
alias: {
Expand Down Expand Up @@ -214,3 +211,5 @@ Flags (advanced):
}
}
}

module.exports = cli
235 changes: 118 additions & 117 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/*! standard-engine. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
module.exports.cli = require('./bin/cmd')

module.exports.linter = Linter

const os = require('os')
const path = require('path')
Expand All @@ -24,128 +21,132 @@ const { resolveEslintConfig } = require('./lib/resolve-eslint-config')
* @property {string} [version]
*/

/**
* @param {LinterOptions} opts
*/
function Linter (opts) {
if (!(this instanceof Linter)) return new Linter(opts)

if (!opts || !opts.cmd) throw new Error('opts.cmd option is required')
if (!opts.eslint) throw new Error('opts.eslint option is required')

/** @type {string} */
this.cmd = opts.cmd
/** @type {import('eslint')} */
this.eslint = opts.eslint
/** @type {string} */
this.cwd = opts.cwd || process.cwd()
this.customEslintConfigResolver = opts.resolveEslintConfig

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

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

/** @type {CLIEngineOptions} */
this.eslintConfig = {
cache: true,
cacheLocation,
envs: [],
fix: false,
globals: [],
plugins: [],
ignorePattern: [],
extensions: [],
useEslintrc: false,
...(opts.eslintConfig || {})
class Linter {
/**
* @param {LinterOptions} opts
*/
constructor (opts) {
if (!opts || !opts.cmd) throw new Error('opts.cmd option is required')
if (!opts.eslint) throw new Error('opts.eslint option is required')

/** @type {string} */
this.cmd = opts.cmd
/** @type {import('eslint')} */
this.eslint = opts.eslint
/** @type {string} */
this.cwd = opts.cwd || process.cwd()
this.customEslintConfigResolver = opts.resolveEslintConfig

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

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

/** @type {CLIEngineOptions} */
this.eslintConfig = {
cache: true,
cacheLocation,
envs: [],
fix: false,
globals: [],
plugins: [],
ignorePattern: [],
extensions: [],
useEslintrc: false,
...(opts.eslintConfig || {})
}

if (this.eslintConfig.configFile != null) {
this.eslintConfig.resolvePluginsRelativeTo = path.dirname(
this.eslintConfig.configFile
)
}
}

if (this.eslintConfig.configFile != null) {
this.eslintConfig.resolvePluginsRelativeTo =
path.dirname(this.eslintConfig.configFile)
/**
* Lint text to enforce JavaScript Style.
*
* @param {string} text file text to lint
* @param {Omit<BaseLintOptions, 'ignore'|'noDefaultIgnore'> & { filename?: string }} [opts] base options + path of file containing the text being linted
* @returns {import('eslint').CLIEngine.LintReport}
*/
lintTextSync (text, opts) {
const eslintConfig = this.resolveEslintConfig(opts)
const engine = new this.eslint.CLIEngine(eslintConfig)
return engine.executeOnText(text, (opts || {}).filename)
}
}

/**
* Lint text to enforce JavaScript Style.
*
* @param {string} text file text to lint
* @param {Omit<BaseLintOptions, 'ignore'|'noDefaultIgnore'> & { filename?: string }} [opts] base options + path of file containing the text being linted
* @returns {import('eslint').CLIEngine.LintReport}
*/
Linter.prototype.lintTextSync = function (text, opts) {
const eslintConfig = this.resolveEslintConfig(opts)
const engine = new this.eslint.CLIEngine(eslintConfig)
return engine.executeOnText(text, (opts || {}).filename)
}

/**
* Lint text to enforce JavaScript Style.
*
* @param {string} text file text to lint
* @param {Omit<BaseLintOptions, 'ignore'|'noDefaultIgnore'> & { filename?: string }} [opts] base options + path of file containing the text being linted
* @param {LinterCallback} [cb]
* @returns {void}
*/
Linter.prototype.lintText = function (text, opts, cb) {
if (typeof opts === 'function') return this.lintText(text, undefined, opts)
if (!cb) throw new Error('callback is required')

let result
try {
result = this.lintTextSync(text, opts)
} catch (err) {
return process.nextTick(cb, err)
/**
* Lint text to enforce JavaScript Style.
*
* @param {string} text file text to lint
* @param {Omit<BaseLintOptions, 'ignore'|'noDefaultIgnore'> & { filename?: string }} [opts] base options + path of file containing the text being linted
* @param {LinterCallback} [cb]
* @returns {void}
*/
lintText (text, opts, cb) {
if (typeof opts === 'function') return this.lintText(text, undefined, opts)
if (!cb) throw new Error('callback is required')

let result
try {
result = this.lintTextSync(text, opts)
} catch (err) {
return process.nextTick(cb, err)
}
process.nextTick(cb, null, result)
}
process.nextTick(cb, null, result)
}

/**
* Lint files to enforce JavaScript Style.
*
* @param {Array.<string>} files file globs to lint
* @param {BaseLintOptions & { cwd?: string }} [opts] base options + file globs to ignore (has sane defaults) + current working directory (default: process.cwd())
* @param {LinterCallback} [cb]
* @returns {void}
*/
Linter.prototype.lintFiles = function (files, opts, cb) {
if (typeof opts === 'function') return this.lintFiles(files, undefined, opts)
if (!cb) throw new Error('callback is required')

const eslintConfig = this.resolveEslintConfig(opts)

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

let result
try {
result = new this.eslint.CLIEngine(eslintConfig).executeOnFiles(files)
} catch (err) {
return cb(err)
/**
* Lint files to enforce JavaScript Style.
*
* @param {Array.<string>} files file globs to lint
* @param {BaseLintOptions & { cwd?: string }} [opts] base options + file globs to ignore (has sane defaults) + current working directory (default: process.cwd())
* @param {LinterCallback} [cb]
* @returns {void}
*/
lintFiles (files, opts, cb) {
if (typeof opts === 'function') { return this.lintFiles(files, undefined, opts) }
if (!cb) throw new Error('callback is required')

const eslintConfig = this.resolveEslintConfig(opts)

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

let result
try {
result = new this.eslint.CLIEngine(eslintConfig).executeOnFiles(files)
} catch (err) {
return cb(err)
}

if (eslintConfig.fix) {
this.eslint.CLIEngine.outputFixes(result)
}

return cb(null, result)
}

if (eslintConfig.fix) {
this.eslint.CLIEngine.outputFixes(result)
/**
* @param {BaseLintOptions & { cwd?: string }} [opts]
* @returns {CLIEngineOptions}
*/
resolveEslintConfig (opts) {
const eslintConfig = resolveEslintConfig(
{
cwd: this.cwd,
...opts,
cmd: this.cmd
},
this.eslintConfig,
this.customEslintConfigResolver
)

return eslintConfig
}

return cb(null, result)
}

/**
* @param {BaseLintOptions & { cwd?: string }} [opts]
* @returns {CLIEngineOptions}
*/
Linter.prototype.resolveEslintConfig = function (opts) {
const eslintConfig = resolveEslintConfig(
{
cwd: this.cwd,
...opts,
cmd: this.cmd
},
this.eslintConfig,
this.customEslintConfigResolver
)

return eslintConfig
}
module.exports.cli = require('./bin/cmd')
module.exports.Linter = Linter
7 changes: 4 additions & 3 deletions test/api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const eslint = require('eslint')
const Linter = require('../').linter
const path = require('path')
const test = require('tape')

const { Linter } = require('../')

function getStandard () {
return new Linter({
cmd: 'pocketlint',
Expand Down Expand Up @@ -40,15 +41,15 @@ test('api: lintTextSync', function (t) {
t.equal(result.errorCount, 1, 'should have used single quotes')
})

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

test('api: parseOpts -- avoid self.eslintConfig global mutation', function (t) {
test('api: resolveEslintConfig -- avoid this.eslintConfig global mutation', function (t) {
t.plan(2)
const standard = getStandard()
const opts = standard.resolveEslintConfig({ globals: ['what'] })
Expand Down

0 comments on commit b92f2da

Please sign in to comment.