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

Default Config & Disable If No Project Config settings #101

Merged
merged 3 commits into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions spec/fixtures/default_config/coffeelint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"max_line_length": {
"value": 10,
"level": "warn"
}
}
2 changes: 2 additions & 0 deletions spec/fixtures/no_config/no_config.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This is a very long line that will trigger max_line_length if the default config is used
a = 1
54 changes: 54 additions & 0 deletions spec/linter-coffeelint-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const validPath = join(fixturesPath, 'valid', 'valid.coffee');
const validCoffeelintPath = join(fixturesPath, 'valid_coffeelint', 'valid.coffee');
const arrowSpacingPath = join(fixturesPath, 'arrow_spacing', 'arrow_spacing.coffee');
const arrowSpacingWarningPath = join(fixturesPath, 'arrow_spacing_warning', 'arrow_spacing.coffee');
const noConfigPath = join(fixturesPath, 'no_config', 'no_config.coffee');
const linter = require('../src').provideLinter();

describe('The CoffeeLint provider for Linter', () => {
Expand Down Expand Up @@ -72,5 +73,58 @@ describe('The CoffeeLint provider for Linter', () => {
expect(messages[0].location.file).toBe(validCoffeelintPath);
expect(messages[0].location.position).toEqual([[0, 0], [0, 41]]);
});

describe('defaultConfig', () => {
it('uses default config when none is found', async () => {
atom.config.set('linter-coffeelint.defaultConfig', join(fixturesPath, 'default_config', 'coffeelint.json'));

const editor = await atom.workspace.open(noConfigPath);
const messages = await linter.lint(editor);

expect(messages.length).toBe(1);
expect(messages[0].severity).toBe('warning');
expect(messages[0].excerpt).toBe('Line exceeds maximum allowed length. Length is 90, max is 10. (max_line_length)');
expect(messages[0].location.file).toBe(noConfigPath);
expect(messages[0].location.position).toEqual([[0, 0], [0, 90]]);
});

it('checks for coffeelint.json if defaultConfig is directory', async () => {
atom.config.set('linter-coffeelint.defaultConfig', join(fixturesPath, 'default_config'));

const editor = await atom.workspace.open(noConfigPath);
const messages = await linter.lint(editor);

expect(messages.length).toBe(1);
expect(messages[0].severity).toBe('warning');
expect(messages[0].excerpt).toBe('Line exceeds maximum allowed length. Length is 90, max is 10. (max_line_length)');
expect(messages[0].location.file).toBe(noConfigPath);
expect(messages[0].location.position).toEqual([[0, 0], [0, 90]]);
});
});

describe('disableIfNoConfig', () => {
it('uses regular coffeelint config by default', async () => {
atom.config.set('linter-coffeelint.defaultConfig', join(fixturesPath, 'default_config'));

const editor = await atom.workspace.open(noConfigPath);
const messages = await linter.lint(editor);

expect(messages.length).toBe(1);
expect(messages[0].severity).toBe('warning');
expect(messages[0].excerpt).toBe('Line exceeds maximum allowed length. Length is 90, max is 10. (max_line_length)');
expect(messages[0].location.file).toBe(noConfigPath);
expect(messages[0].location.position).toEqual([[0, 0], [0, 90]]);
});

it('finds nothing wrong if no config is found', async () => {
atom.config.set('linter-coffeelint.defaultConfig', join(fixturesPath, 'default_config'));
atom.config.set('linter-coffeelint.disableIfNoConfig', true);

const editor = await atom.workspace.open(noConfigPath);
const messages = await linter.lint(editor);

expect(messages.length).toBe(0);
});
});
});
});
40 changes: 32 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ const loadDeps = () => {
};

module.exports = {
config: {
defaultConfig: {
title: 'Default Config Path',
Copy link
Member

Choose a reason for hiding this comment

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

I'd add a description clarifying that this is only used if coffeelint can't find a configuration on it's own.

type: 'string',
default: atom.getConfigDirPath(),
Copy link
Member

Choose a reason for hiding this comment

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

I would just leave this blank, if a user wants to fill this in they can enter the path to wherever it should be themselves. The default value as is is wrong and just encourages people to put non-Atom configuration files in there.

},
disableIfNoConfig: {
title: 'Disable If No Project Config File Is Found',
type: 'boolean',
default: false,
},
},

activate() {
this.idleCallbacks = new Set();
let depsCallbackID;
Expand Down Expand Up @@ -66,6 +79,9 @@ module.exports = {
cursor.getScopeDescriptor().getScopesArray().some(scope =>
scope === 'source.litcoffee'));

const defaultConfig = atom.config.get('linter-coffeelint.defaultConfig');
const disableIfNoConfig = atom.config.get('linter-coffeelint.disableIfNoConfig');

const transform = ({
level, message, rule, lineNumber, context,
}) => {
Expand All @@ -88,14 +104,22 @@ module.exports = {
loadDeps();

return new Promise((resolve, reject) => {
const task = Task.once(workerFile, filePath, source, isLiterate, (results) => {
this.workers.delete(task);
try {
resolve(results.map(transform));
} catch (e) {
reject(e);
}
});
const task = Task.once(
workerFile,
filePath,
source,
isLiterate,
defaultConfig,
disableIfNoConfig,
(results) => {
this.workers.delete(task);
try {
resolve(results.map(transform));
} catch (e) {
reject(e);
}
},
);
this.workers.add(task);
task.on('task:error', e => reject(e));
});
Expand Down
14 changes: 13 additions & 1 deletion src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const configImportsModules = (config) => {
Object.prototype.hasOwnProperty.call(config[ruleName], 'module'));
};

module.exports = (filePath, source, isLiterate) => {
module.exports = (filePath, source, isLiterate, defaultConfig, disableIfNoConfig) => {
const fileDir = path.dirname(filePath);
process.chdir(fileDir);

Expand Down Expand Up @@ -79,6 +79,18 @@ module.exports = (filePath, source, isLiterate) => {
}
}

if (!config) {
if (disableIfNoConfig) {
return [];
} else if (defaultConfig) {
let configFile = defaultConfig;
if (!configFile.endsWith('coffeelint.json')) {
configFile = path.join(configFile, 'coffeelint.json');
}
config = configFinder.getConfig(configFile);
}
}

const results = coffeelint.lint(source, config, isLiterate);

if (showUpgradeError) {
Expand Down