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

Commit

Permalink
fix($formatOnSave): Fix error when no .eslintignore is found
Browse files Browse the repository at this point in the history
We were not properly handling the case where no `.eslintignore` was found when attempting to
determine whether to run `formatOnSave`. Now, if no `.eslintignore` is found, we short circuit and
assume the file is not ignored.

Fixes #67 (again)
  • Loading branch information
robwise committed Mar 6, 2017
1 parent 00ec9fa commit 94d7750
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
16 changes: 12 additions & 4 deletions dist/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ var getNearestEslintignorePath = function getNearestEslintignorePath(filePath) {
};

var getFilePathRelativeToEslintignore = function getFilePathRelativeToEslintignore(filePath) {
return flow(getNearestEslintignorePath, getDirFromFilePath, function (eslintignoreDir) {
return path.relative(eslintignoreDir, filePath);
})(filePath);
var nearestEslintignorePath = getNearestEslintignorePath(filePath);

if (!nearestEslintignorePath) return undefined;

var eslintignoreDir = getDirFromFilePath(nearestEslintignorePath);

return path.relative(eslintignoreDir, filePath);
};

var getLinesFromFilePath = function getLinesFromFilePath(filePath) {
Expand Down Expand Up @@ -94,7 +98,11 @@ var shouldUseEslint = function shouldUseEslint() {
};

var isFilePathEslintignored = function isFilePathEslintignored(filePath) {
return someGlobsMatchFilePath(getIgnoredGlobsFromNearestEslintIgnore(filePath), getFilePathRelativeToEslintignore(filePath));
var filePathRelativeToEslintignore = getFilePathRelativeToEslintignore(filePath);

if (!filePathRelativeToEslintignore) return false;

return someGlobsMatchFilePath(getIgnoredGlobsFromNearestEslintIgnore(filePath), filePathRelativeToEslintignore);
};

var isFormatOnSaveEnabled = function isFormatOnSaveEnabled() {
Expand Down
23 changes: 17 additions & 6 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ const getDirFromFilePath = (filePath: FilePath): FilePath => path.parse(filePath
const getNearestEslintignorePath = (filePath: FilePath): ?FilePath =>
findCached(getDirFromFilePath(filePath), '.eslintignore');

const getFilePathRelativeToEslintignore = (filePath: FilePath): FilePath =>
flow(getNearestEslintignorePath, getDirFromFilePath, eslintignoreDir =>
path.relative(eslintignoreDir, filePath))(filePath);
const getFilePathRelativeToEslintignore = (filePath: FilePath): ?FilePath => {
const nearestEslintignorePath = getNearestEslintignorePath(filePath);

if (!nearestEslintignorePath) return undefined;

const eslintignoreDir = getDirFromFilePath(nearestEslintignorePath);

return path.relative(eslintignoreDir, filePath);
};

const getLinesFromFilePath = (filePath: FilePath) =>
fs.readFileSync(filePath, 'utf8').split(LINE_SEPERATOR_REGEX);
Expand Down Expand Up @@ -58,11 +64,16 @@ const isCurrentScopeEmbeddedScope = (editor: TextEditor) => EMBEDDED_SCOPES.incl

const shouldUseEslint = () => getConfigOption('useEslint');

const isFilePathEslintignored = (filePath: FilePath) =>
someGlobsMatchFilePath(
const isFilePathEslintignored = (filePath: FilePath) => {
const filePathRelativeToEslintignore = getFilePathRelativeToEslintignore(filePath);

if (!filePathRelativeToEslintignore) return false;

return someGlobsMatchFilePath(
getIgnoredGlobsFromNearestEslintIgnore(filePath),
getFilePathRelativeToEslintignore(filePath),
filePathRelativeToEslintignore,
);
};

const isFormatOnSaveEnabled = () => getConfigOption('formatOnSaveOptions.enabled');

Expand Down
25 changes: 25 additions & 0 deletions src/helpers.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
const path = require('path');
const atomLinter = require('atom-linter');

const textEditor = require('../tests/mocks/textEditor');
const {
Expand All @@ -15,6 +16,8 @@ const {
getPrettierOptions,
} = require('./helpers');

jest.mock('atom-linter');

describe('getConfigOption', () => {
test('retrieves a config option from the prettier-atom config', () => {
const mockGet = jest.fn(() => 'foo');
Expand Down Expand Up @@ -132,21 +135,43 @@ describe('shouldUseEslint', () => {
});

describe('isFilePathEslintignored', () => {
test('is false if no .eslintignore file can be found', () => {
atomLinter.findCached.mockImplementation(() => null);
const filePath = path.join(__dirname, '..', 'tests', 'fixtures', 'matchesEslintignore.js');

const actual = isFilePathEslintignored(filePath);
const expected = false;

expect(actual).toBe(expected);
});

test('is false if the filePath does not match a glob in the nearest eslintignore', () => {
atomLinter.findCached.mockImplementation(() =>
path.join(__dirname, '..', 'tests', 'fixtures', '.eslintignore'));
const filePath = path.join(__dirname, '..', 'tests', 'fixtures', 'doesNotMatchEslintignore.js');

const actual = isFilePathEslintignored(filePath);
const expected = false;

expect(atomLinter.findCached).toHaveBeenCalledWith(
path.join(__dirname, '..', 'tests', 'fixtures'),
'.eslintignore',
);
expect(actual).toBe(expected);
});

test('is true if the filePath does match a glob in the nearest eslintignore', () => {
atomLinter.findCached.mockImplementation(() =>
path.join(__dirname, '..', 'tests', 'fixtures', '.eslintignore'));
const filePath = path.join(__dirname, '..', 'tests', 'fixtures', 'matchesEslintignore.js');

const actual = isFilePathEslintignored(filePath);
const expected = true;

expect(atomLinter.findCached).toHaveBeenCalledWith(
path.join(__dirname, '..', 'tests', 'fixtures'),
'.eslintignore',
);
expect(actual).toBe(expected);
});
});
Expand Down

0 comments on commit 94d7750

Please sign in to comment.