Skip to content

Commit

Permalink
feat: factor eslint config to eslint-config-ipfs (#638)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala committed Sep 15, 2020
1 parent b782b88 commit 0fb42b9
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 124 deletions.
13 changes: 3 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@
"@commitlint/travis-cli": "^8.3.5",
"@electron/get": "^1.10.0",
"@polka/send-type": "^0.5.2",
"@typescript-eslint/eslint-plugin": "^3.3.0",
"@typescript-eslint/parser": "^3.3.0",
"babel-loader": "^8.0.5",
"buffer": "^5.6.0",
"bytes": "^3.1.0",
Expand All @@ -76,13 +74,8 @@
"dirty-chai": "^2.0.1",
"documentation": "^13.0.1",
"electron-mocha": "^8.2.0",
"eslint": "^6.3.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-no-only-tests": "^2.4.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"eslint": "^6.8.0",
"eslint-config-ipfs": "^0.1.0",
"execa": "^4.0.0",
"extract-zip": "^2.0.1",
"findup-sync": "^4.0.0",
Expand Down Expand Up @@ -145,7 +138,7 @@
"not ie < 11"
],
"eslintConfig": {
"extends": "./src/config/eslintrc.js"
"extends": "ipfs"
},
"contributors": [
"dignifiedquire <dignifiedquire@gmail.com>",
Expand Down
51 changes: 0 additions & 51 deletions src/config/eslintrc-js.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/config/eslintrc-ts.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/config/eslintrc.js

This file was deleted.

6 changes: 3 additions & 3 deletions src/dependency-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const defaultInput = [
/**
* Check dependencies
*
* @param {Object} argv
* @param {ExecaOptions} execaOptions
* @returns {ExecaChildProcess}
* @param {object} argv - Command line arguments passed to the process.
* @param {ExecaOptions} execaOptions - execa options.
* @returns {ExecaChildProcess} - Child process that does dependency check.
*/
const check = (argv = { _: [] }, execaOptions) => {
const input = argv._.slice(1)
Expand Down
6 changes: 3 additions & 3 deletions src/docs/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const { fromRoot, hasFile } = require('../utils')
/**
* Build docs
*
* @param {Object} argv
* @param {ExecaOptions} execaOptions
* @returns {ExecaChildProcess}
* @param {object} argv - Command line arguments passed to the process.
* @param {ExecaOptions} execaOptions - execa options.
* @returns {ExecaChildProcess} - Child process that builds docs.
*/
const docs = (argv, execaOptions = {}) => {
const forwardOptions = argv['--'] ? argv['--'] : []
Expand Down
2 changes: 1 addition & 1 deletion src/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function checkDependencyVersions () {
function runLinter (opts = {}) {
const cli = new CLIEngine({
useEslintrc: true,
baseConfig: require('./config/eslintrc.js'),
baseConfig: { extends: 'ipfs' },
fix: opts.fix
})

Expand Down
25 changes: 5 additions & 20 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exports.fromAegir = (...p) => path.join(__dirname, '..', ...p)
/**
* Get package version
*
* @returns {string} version
* @returns {string} - version
*/
exports.pkgVersion = async () => {
const {
Expand All @@ -51,36 +51,24 @@ exports.pkgVersion = async () => {
/**
* Gets the top level path of the project aegir is executed in.
*
* @returns {string}
* @returns {string} - base path
*/
exports.getBasePath = () => {
return process.cwd()
}

/**
* @returns {string}
*/
exports.getPathToPkg = () => {
return path.join(exports.getBasePath(), PKG_FILE)
}

/**
* @returns {string}
*/
exports.getPathToDist = () => {
return path.join(exports.getBasePath(), DIST_FOLDER)
}

/**
* @returns {string}
*/
exports.getUserConfigPath = () => {
return findUp('.aegir.js')
}

/**
* @returns {Object}
*/
exports.getUserConfig = () => {
let conf = {}
try {
Expand All @@ -98,18 +86,15 @@ exports.getUserConfig = () => {
/**
* Converts the given name from something like `peer-id` to `PeerId`.
*
* @param {string} name
*
* @returns {string}
* @param {string} name - lib name in kebab
* @returns {string} - lib name in pascal
*/
exports.getLibraryName = (name) => {
return pascalcase(name)
}

/**
* Get the absolute path to `node_modules` for aegir itself
*
* @returns {string}
*/
exports.getPathToNodeModules = () => {
return path.resolve(__dirname, '../node_modules')
Expand All @@ -118,7 +103,7 @@ exports.getPathToNodeModules = () => {
/**
* Get the config for Listr.
*
* @returns {Object}
* @returns {{renderer: 'verbose'}} - config for Listr
*/
exports.getListrConfig = () => {
return {
Expand Down
94 changes: 89 additions & 5 deletions test/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ const fs = require('fs')
const rimraf = require('rimraf')

const TEMP_FOLDER = path.join(__dirname, '../node_modules/.temp-test')
const setupProjectWithDeps = async (deps) => {

const setupProject = async (project) => {
const tmpDir = path.join(TEMP_FOLDER, `test-${Math.random()}`)
await fs.promises.mkdir(tmpDir)
await fs.promises.writeFile(path.join(tmpDir, 'package.json'), JSON.stringify({
name: 'my-project',
dependencies: deps
}))
for (const [name, content] of Object.entries(project)) {
await fs.promises.writeFile(path.join(tmpDir, name), content)
}
process.chdir(tmpDir)
}

const setupProjectWithDeps = deps => setupProject({
'package.json': JSON.stringify({
name: 'my-project',
dependencies: deps
})
})

const dependenciesShouldPassLinting = (deps) => {
return setupProjectWithDeps(deps)
.then(() => lint())
Expand All @@ -33,6 +41,24 @@ const dependenciesShouldFailLinting = (deps) => {
})
}

const projectShouldPassLint = async (project) => {
await setupProject(project)
await lint()
}

const projectShouldFailLint = async (project) => {
await setupProject(project)
let failed = false
try {
await lint({ silent: true })
} catch (error) {
failed = true
expect(error.message).to.contain('Lint errors')
}

expect(failed).to.equal(true, 'Should have failed!')
}

describe('lint', () => {
const cwd = process.cwd()
before(() => {
Expand Down Expand Up @@ -166,4 +192,62 @@ describe('lint', () => {
process.chdir(path.join(__dirname, './fixtures/js+ts/'))
await lint()
})

it('should pass if no .eslintrc found and does not follows ipfs eslint rules', async () => {
await projectShouldFailLint({
'package.json': JSON.stringify({
name: 'no-config-fail',
main: 'index.js'
}),
'index.js': '"use strict"\nmodule.exports = () => {}\n'
})
})

it('should pass if no .eslintrc found but code follows ipfs eslint rules', async () => {
await projectShouldPassLint({
'package.json': JSON.stringify({
name: 'no-config-fail',
main: 'index.js'
}),
'index.js': '\'use strict\'\nmodule.exports = () => {}\n'
})
})

it('should fail if .eslintrc overules ipfs and code does not follow it', async () => {
await projectShouldFailLint({
'package.json': JSON.stringify({
name: 'with-config-fail',
main: 'index.js'
}),
'index.js': '\'use strict\'\nmodule.exports = () => {}\n',
'.eslintrc': JSON.stringify({
extends: 'ipfs',
rules: {
quotes: [
'error',
'double'
]
}
})
})
})

it('should pass if .eslintrc overules ipfs and code follows it', async () => {
await projectShouldPassLint({
'package.json': JSON.stringify({
name: 'with-config-fail',
main: 'index.js'
}),
'index.js': '"use strict"\nmodule.exports = () => {}\n',
'.eslintrc': JSON.stringify({
extends: 'ipfs',
rules: {
quotes: [
'error',
'double'
]
}
})
})
})
})

0 comments on commit 0fb42b9

Please sign in to comment.