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

Commit e83677b

Browse files
authored
Merge pull request #354 from AtomLinter/arcanemagus/dont-merge-configs
Stop merging stylelint-config-standard when enabled
2 parents 6f06f24 + 32f64cd commit e83677b

File tree

4 files changed

+78
-52
lines changed

4 files changed

+78
-52
lines changed

lib/helpers.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
'use babel';
22

3+
import { dirname } from 'path';
34
import stylelint from 'stylelint';
5+
import assignDeep from 'assign-deep';
46
import escapeHTML from 'escape-html';
57
import { generateRange } from 'atom-linter';
8+
import presetConfig from 'stylelint-config-standard';
69

710
export function startMeasure(baseName) {
811
const markName = `${baseName}-start`;
@@ -164,3 +167,34 @@ export const runStylelint = async (editor, stylelintOptions, filePath, settings)
164167
}
165168
return parseResults(editor, results, filePath, settings.showIgnored);
166169
};
170+
171+
export function getDefaultConfig(syntax, filePath) {
172+
const defaultConfig = assignDeep({}, presetConfig);
173+
174+
if (syntax === 'sugarss') {
175+
// `stylelint-config-standard` isn't fully compatible with SugarSS
176+
// See here for details:
177+
// https://github.com/stylelint/stylelint-config-standard#using-the-config-with-sugarss-syntax
178+
defaultConfig.rules['block-closing-brace-empty-line-before'] = null;
179+
defaultConfig.rules['block-closing-brace-newline-after'] = null;
180+
defaultConfig.rules['block-closing-brace-newline-before'] = null;
181+
defaultConfig.rules['block-closing-brace-space-before'] = null;
182+
defaultConfig.rules['block-opening-brace-newline-after'] = null;
183+
defaultConfig.rules['block-opening-brace-space-after'] = null;
184+
defaultConfig.rules['block-opening-brace-space-before'] = null;
185+
defaultConfig.rules['declaration-block-semicolon-newline-after'] = null;
186+
defaultConfig.rules['declaration-block-semicolon-space-after'] = null;
187+
defaultConfig.rules['declaration-block-semicolon-space-before'] = null;
188+
defaultConfig.rules['declaration-block-trailing-semicolon'] = null;
189+
}
190+
191+
// Base the config in the project directory
192+
let [configBasedir] = atom.project.relativizePath(filePath);
193+
if (configBasedir === null) {
194+
// Falling back to the file directory if no project is found
195+
configBasedir = dirname(filePath);
196+
}
197+
defaultConfig.configBasedir = configBasedir;
198+
199+
return defaultConfig;
200+
}

lib/index.js

Lines changed: 35 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ let helpers;
88
let dirname;
99
let stylelint;
1010
let assignDeep;
11-
let presetConfig;
1211

1312
function loadDeps() {
1413
if (!helpers) {
@@ -23,9 +22,6 @@ function loadDeps() {
2322
if (!assignDeep) {
2423
assignDeep = require('assign-deep');
2524
}
26-
if (!presetConfig) {
27-
presetConfig = require('stylelint-config-standard');
28-
}
2925
}
3026

3127
export default {
@@ -44,11 +40,21 @@ export default {
4440

4541
this.subscriptions = new CompositeDisposable();
4642
this.subscriptions.add(
47-
atom.config.observe('linter-stylelint.useStandard', (value) => {
48-
this.useStandard = value;
49-
}),
5043
atom.config.observe('linter-stylelint.disableWhenNoConfig', (value) => {
5144
this.disableWhenNoConfig = value;
45+
if (this.useStandard && this.disableWhenNoConfig) {
46+
// Disable using the standard if it is desired to stop linting with
47+
// no configuration
48+
atom.config.set('linter-stylelint.useStandard', false);
49+
}
50+
}),
51+
atom.config.observe('linter-stylelint.useStandard', (value) => {
52+
this.useStandard = value;
53+
if (this.useStandard && this.disableWhenNoConfig) {
54+
// Disable disabling linting when there is no configuration as the
55+
// standard configuration will always be available.
56+
atom.config.set('linter-stylelint.disableWhenNoConfig', false);
57+
}
5258
}),
5359
atom.config.observe('linter-stylelint.showIgnored', (value) => {
5460
this.showIgnored = value;
@@ -86,7 +92,6 @@ export default {
8692
loadDeps();
8793

8894
helpers.startMeasure('linter-stylelint: Lint');
89-
const scopes = editor.getLastCursor().getScopeDescriptor().getScopesArray();
9095

9196
const filePath = editor.getPath();
9297
const text = editor.getText();
@@ -96,55 +101,26 @@ export default {
96101
return [];
97102
}
98103

99-
// Setup base config if useStandard() is true
100-
const defaultConfig = {
101-
rules: {}
102-
};
103-
104-
// Base the config in the project directory
105-
let [configBasedir] = atom.project.relativizePath(filePath);
106-
if (configBasedir === null) {
107-
// Falling back to the file directory if no project is found
108-
configBasedir = dirname(filePath);
109-
}
110-
111-
const rules = this.useStandard ? assignDeep({}, presetConfig) : defaultConfig;
112-
104+
const rules = {};
113105
const options = {
114106
code: text,
115107
codeFilename: filePath,
116-
config: rules,
117-
configBasedir
108+
config: { rules }
118109
};
119110

120-
if (this.coreIgnored) {
121-
// When Atom (and thus Linter) is set to allow ignored files, tell
122-
// Stylelint to do the same.
123-
options.disableDefaultIgnores = true;
124-
}
125-
111+
const scopes = editor.getLastCursor().getScopeDescriptor().getScopesArray();
126112
if (scopes.includes('source.css.scss') || scopes.includes('source.scss')) {
127113
options.syntax = 'scss';
128-
}
129-
if (scopes.includes('source.css.less') || scopes.includes('source.less')) {
114+
} else if (scopes.includes('source.css.less') || scopes.includes('source.less')) {
130115
options.syntax = 'less';
131-
}
132-
if (scopes.includes('source.css.postcss.sugarss')) {
116+
} else if (scopes.includes('source.css.postcss.sugarss')) {
133117
options.syntax = 'sugarss';
134-
// `stylelint-config-standard` isn't fully compatible with SugarSS
135-
// See here for details:
136-
// https://github.com/stylelint/stylelint-config-standard#using-the-config-with-sugarss-syntax
137-
options.config.rules['block-closing-brace-empty-line-before'] = null;
138-
options.config.rules['block-closing-brace-newline-after'] = null;
139-
options.config.rules['block-closing-brace-newline-before'] = null;
140-
options.config.rules['block-closing-brace-space-before'] = null;
141-
options.config.rules['block-opening-brace-newline-after'] = null;
142-
options.config.rules['block-opening-brace-space-after'] = null;
143-
options.config.rules['block-opening-brace-space-before'] = null;
144-
options.config.rules['declaration-block-semicolon-newline-after'] = null;
145-
options.config.rules['declaration-block-semicolon-space-after'] = null;
146-
options.config.rules['declaration-block-semicolon-space-before'] = null;
147-
options.config.rules['declaration-block-trailing-semicolon'] = null;
118+
}
119+
120+
if (this.coreIgnored) {
121+
// When Atom (and thus Linter) is set to allow ignored files, tell
122+
// Stylelint to do the same.
123+
options.disableDefaultIgnores = true;
148124
}
149125

150126
helpers.startMeasure('linter-stylelint: Create Linter');
@@ -172,13 +148,21 @@ export default {
172148
helpers.endMeasure('linter-stylelint: Config');
173149

174150
if (foundConfig) {
151+
// We have a configuration from Stylelint
175152
options.config = assignDeep(rules, foundConfig.config);
176153
options.configBasedir = dirname(foundConfig.filepath);
177-
}
178-
179-
if (!foundConfig && this.disableWhenNoConfig) {
154+
} else if (this.disableWhenNoConfig) {
155+
// No configuration, and linting without one is disabled
180156
helpers.endMeasure('linter-stylelint: Lint');
181157
return [];
158+
} else if (this.useStandard) {
159+
// No configuration, but using the standard is enabled
160+
const defaultConfig = helpers.getDefaultConfig(options.syntax, filePath);
161+
assignDeep(rules, defaultConfig.rules);
162+
if (defaultConfig.extends) {
163+
options.config.extends = defaultConfig.extends;
164+
}
165+
options.configBasedir = defaultConfig.configBasedir;
182166
}
183167

184168
helpers.startMeasure('linter-stylelint: Check ignored');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"configSchema": {
3434
"useStandard": {
3535
"title": "Use standard",
36-
"description": "Use the stylelint-config-standard lint configuration",
36+
"description": "Use the stylelint-config-standard lint configuration when no other configuration is found. Disables the \"Disable when no config\" option.",
3737
"type": "boolean",
3838
"default": false
3939
},

spec/linter-stylelint-spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ describe('The stylelint provider for Linter', () => {
143143
});
144144

145145
describe('ignores files when files are specified in ignoreFiles and', () => {
146+
beforeEach(() => {
147+
atom.config.set('linter-stylelint.useStandard', true);
148+
});
149+
146150
it('shows a message when asked to', async () => {
147151
atom.config.set('linter-stylelint.showIgnored', true);
148152
const editor = await atom.workspace.open(ignorePath);
@@ -166,6 +170,7 @@ describe('The stylelint provider for Linter', () => {
166170
});
167171

168172
it("doesn't persist settings across runs", async () => {
173+
atom.config.set('linter-stylelint.useStandard', true);
169174
atom.config.set('linter-stylelint.disableWhenNoConfig', false);
170175
// The config for this folder breaks the block-no-empty rule
171176
const invalidEditor = await atom.workspace.open(invalidRulePath);
@@ -187,6 +192,7 @@ describe('The stylelint provider for Linter', () => {
187192

188193
describe('works with Less files and', () => {
189194
beforeEach(async () => {
195+
atom.config.set('linter-stylelint.useStandard', true);
190196
atom.config.set('linter-stylelint.disableWhenNoConfig', false);
191197
await atom.packages.activatePackage('language-less');
192198
});
@@ -214,6 +220,7 @@ describe('The stylelint provider for Linter', () => {
214220

215221
describe('works with PostCSS files and', () => {
216222
beforeEach(async () => {
223+
atom.config.set('linter-stylelint.useStandard', true);
217224
atom.config.set('linter-stylelint.disableWhenNoConfig', false);
218225
await atom.packages.activatePackage('language-postcss');
219226
});
@@ -241,6 +248,7 @@ describe('The stylelint provider for Linter', () => {
241248

242249
describe('works with SugarSS files and', () => {
243250
beforeEach(async () => {
251+
atom.config.set('linter-stylelint.useStandard', true);
244252
atom.config.set('linter-stylelint.disableWhenNoConfig', false);
245253
await atom.packages.activatePackage('language-postcss');
246254
});

0 commit comments

Comments
 (0)