Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Ignore home config from disableWhenNoEslintConfig #778

Merged
merged 4 commits into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/is-config-at-home-root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isConfigAtHomeRoot = isConfigAtHomeRoot;

var _userHome = require('user-home');

var _userHome2 = _interopRequireDefault(_userHome);

var _path = require('path');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
* Check if a config is directly inside a user's home directory.
* Such config files are used by ESLint as a fallback, only for situations
* when there is no other config file between a file being linted and root.
*
* @param {string} configPath - The path of the config file being checked
* @return {Boolean} True if the file is directly in the current user's home
*/
/* eslint-disable import/prefer-default-export */

function isConfigAtHomeRoot(configPath) {
return (0, _path.dirname)(configPath) === _userHome2.default;
}
5 changes: 4 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var _helpers = require('./helpers');

var _workerHelpers = require('./worker-helpers');

var _isConfigAtHomeRoot = require('./is-config-at-home-root');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
Expand Down Expand Up @@ -58,7 +60,8 @@ module.exports = {
// Do not try to fix if linting should be disabled
const fileDir = _path2.default.dirname(filePath);
const configPath = (0, _workerHelpers.getConfigPath)(fileDir);
if (configPath === null && disableWhenNoEslintConfig) return;
const noProjectConfig = configPath === null || (0, _isConfigAtHomeRoot.isConfigAtHomeRoot)(configPath);
if (noProjectConfig && disableWhenNoEslintConfig) return;

this.worker.request('job', {
type: 'fix',
Expand Down
5 changes: 4 additions & 1 deletion lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var _workerHelpers = require('./worker-helpers');

var Helpers = _interopRequireWildcard(_workerHelpers);

var _isConfigAtHomeRoot = require('./is-config-at-home-root');

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Expand All @@ -32,7 +34,8 @@ const ignoredMessages = [
'File ignored by default. Use a negated ignore pattern (like "--ignore-pattern \'!<relative' + '/path/to/filename>\'") to override.', 'File ignored by default. Use "--ignore-pattern \'!node_modules/*\'" to override.', 'File ignored by default. Use "--ignore-pattern \'!bower_components/*\'" to override.'];

function lintJob(argv, contents, eslint, configPath, config) {
if (configPath === null && config.disableWhenNoEslintConfig) {
const noProjectConfig = configPath === null || (0, _isConfigAtHomeRoot.isConfigAtHomeRoot)(configPath);
if (noProjectConfig && config.disableWhenNoEslintConfig) {
return [];
}
eslint.execute(argv, contents);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@
"eslint": "^3.6.0",
"eslint-rule-documentation": "^1.0.0",
"process-communication": "^1.1.0",
"resolve-env": "^1.0.0"
"resolve-env": "^1.0.0",
"user-home": "^2.0.0"
},
"devDependencies": {
"babel-cli": "^6.18.0",
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/files/badInline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* eslint no-undef: error */

foo = 42;
84 changes: 84 additions & 0 deletions spec/linter-eslint-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const fixturesDir = path.join(__dirname, 'fixtures')

const goodPath = path.join(fixturesDir, 'files', 'good.js')
const badPath = path.join(fixturesDir, 'files', 'bad.js')
const badInlinePath = path.join(fixturesDir, 'files', 'badInline.js')
const emptyPath = path.join(fixturesDir, 'files', 'empty.js')
const fixPath = path.join(fixturesDir, 'files', 'fix.js')
const configPath = path.join(fixturesDir, 'configs', '.eslintrc.yml')
Expand All @@ -24,6 +25,19 @@ const modifiedIgnoreSpacePath = path.join(fixturesDir,
'modified-ignore-rule', 'foo-space.js')
const endRangePath = path.join(fixturesDir, 'end-range', 'no-unreachable.js')

function copyFileToTempDir(fileToCopyPath, tempFixtureDir) {
return new Promise((resolve) => {
const tempFixturePath = path.join(tempFixtureDir, path.basename(fileToCopyPath))
const wr = fs.createWriteStream(tempFixturePath)
wr.on('close', () =>
atom.workspace.open(tempFixturePath).then((openEditor) => {
resolve(openEditor)
})
)
fs.createReadStream(fileToCopyPath).pipe(wr)
})
}

describe('The eslint provider for Linter', () => {
const { spawnWorker } = require('../lib/helpers')

Expand Down Expand Up @@ -361,4 +375,74 @@ describe('The eslint provider for Linter', () => {
)
)
)

describe('when setting `disableWhenNoEslintConfig` is false', () => {
let editor
let didError
let gotLintingErrors
const tempFixtureDir = fs.mkdtempSync(tmpdir() + path.sep)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is the exact same in both test cases, you could move it into copyFileToTempDir, if you want to leave it as a parameter though you might as well just call the function copyFileToDir 😛.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I passed it in so that I could rimraf it after the test. Guess I could indeed rename it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arcanemagus I ended up moving it to the function after all. Seemed … cleaner, I guess.


beforeEach(() => {
atom.config.set('linter-eslint.disableWhenNoEslintConfig', false)

waitsForPromise(() =>
copyFileToTempDir(badInlinePath, tempFixtureDir)
.then((openEditor) => { editor = openEditor })
)
})

afterEach(() => {
rimraf.sync(tempFixtureDir)
})

it('errors when no config file is found', () => {
lint(editor)
.then((messages) => {
// Older versions of ESLint will report an error
// (or if current user running tests has a config in their home directory)
const expectedHtml = '<a href=http://eslint.org/docs/rules/no-undef ' +
'class="badge badge-flexible eslint">no-undef</a> &#39;foo&#39; is not defined.'
expect(messages.length).toBe(1)
expect(messages[0].html).toBe(expectedHtml)
gotLintingErrors = true
})
.catch((err) => {
// Newer versions of ESLint will throw an exception
expect(err.message).toBe('No ESLint configuration found.')
didError = true
})

waitsFor(
() => didError || gotLintingErrors,
'An error should have been thrown or linting performed'
)
})
})

describe('when `disableWhenNoEslintConfig` is true', () => {
let editor
const tempFixtureDir = fs.mkdtempSync(tmpdir() + path.sep)

beforeEach(() => {
atom.config.set('linter-eslint.disableWhenNoEslintConfig', true)

waitsForPromise(() =>
copyFileToTempDir(badInlinePath, tempFixtureDir)
.then((openEditor) => { editor = openEditor })
)
})

afterEach(() => {
rimraf.sync(tempFixtureDir)
})

it('does not report errors when no config file is found', () =>
waitsForPromise(() =>
lint(editor)
.then((messages) => {
expect(messages.length).toBe(0)
})
)
)
})
})
16 changes: 16 additions & 0 deletions src/is-config-at-home-root.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable import/prefer-default-export */

import userHome from 'user-home'
import { dirname } from 'path'

/**
* Check if a config is directly inside a user's home directory.
* Such config files are used by ESLint as a fallback, only for situations
* when there is no other config file between a file being linted and root.
*
* @param {string} configPath - The path of the config file being checked
* @return {Boolean} True if the file is directly in the current user's home
*/
export function isConfigAtHomeRoot(configPath) {
return (dirname(configPath) === userHome)
}
6 changes: 4 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { CompositeDisposable, } from 'atom'

import {
spawnWorker, showError, idsToIgnoredRules, processESLintMessages,
generateDebugString
generateDebugString,
} from './helpers'
import { getConfigPath } from './worker-helpers'
import { isConfigAtHomeRoot } from './is-config-at-home-root'

// Configuration
const scopes = []
Expand Down Expand Up @@ -54,7 +55,8 @@ module.exports = {
// Do not try to fix if linting should be disabled
const fileDir = Path.dirname(filePath)
const configPath = getConfigPath(fileDir)
if (configPath === null && disableWhenNoEslintConfig) return
const noProjectConfig = (configPath === null || isConfigAtHomeRoot(configPath))
if (noProjectConfig && disableWhenNoEslintConfig) return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm personally not a fan of one line if statements, but I'll leave that up to you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's how it was before, so I'm just going to leave it for now. :)


this.worker.request('job', {
type: 'fix',
Expand Down
4 changes: 3 additions & 1 deletion src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Path from 'path'
import { create } from 'process-communication'
import { FindCache, findCached } from 'atom-linter'
import * as Helpers from './worker-helpers'
import { isConfigAtHomeRoot } from './is-config-at-home-root'

process.title = 'linter-eslint helper'

Expand All @@ -24,7 +25,8 @@ const ignoredMessages = [
]

function lintJob(argv, contents, eslint, configPath, config) {
if (configPath === null && config.disableWhenNoEslintConfig) {
const noProjectConfig = (configPath === null || isConfigAtHomeRoot(configPath))
if (noProjectConfig && config.disableWhenNoEslintConfig) {
return []
}
eslint.execute(argv, contents)
Expand Down