Skip to content

Commit

Permalink
Merge pull request #117 from stylelint/config-basedir
Browse files Browse the repository at this point in the history
Add "stylelint.configBasedir" option
  • Loading branch information
ntwb committed Jun 8, 2020
2 parents 115118b + 5a82c89 commit 9a0c69d
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ Default: `null`

Set stylelint [`config`](https://stylelint.io/user-guide/usage/node-api#config) option. Note that when this option is enabled, stylelint doesn't load configuration files.

#### stylelint.configBasedir

Type: `string`
Default: `""`

Set stylelint [`configBasedir`](https://stylelint.io/user-guide/usage/options#configbasedir) option. The path to the directory that relative paths defining "extends" and "plugins" are relative to. Only necessary if these values are relative paths.

#### stylelint.customSyntax

Type: `string`
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
"default": null,
"description": "A partial stylelint config whose properties override the existing ones."
},
"stylelint.configBasedir": {
"type": "string",
"default": "",
"description": "A path to the directory that relative paths defining \"extends\" and \"plugins\" are relative to."
},
"stylelint.customSyntax": {
"type": "string",
"default": "",
Expand Down
11 changes: 11 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ const StylelintSourceFixAll = `${CodeActionKind.SourceFixAll}.stylelint`;
let config;
/** @type {StylelintConfiguration} */
let configOverrides;
/** @type {string} */
let configBasedir;
/** @type {PackageManager} */
let packageManager;
/** @type {string} */
Expand Down Expand Up @@ -117,6 +119,14 @@ async function buildStylelintOptions(document, baseOptions = {}) {
: customSyntax;
}

if (configBasedir) {
if (isAbsolute(configBasedir)) {
options.configBasedir = configBasedir;
} else {
options.configBasedir = join(workspaceFolder || '', configBasedir);
}
}

if (documentPath) {
if (workspaceFolder && pathIsInside(documentPath, workspaceFolder)) {
options.ignorePath = join(workspaceFolder, '.stylelintignore');
Expand Down Expand Up @@ -300,6 +310,7 @@ connection.onDidChangeConfiguration(({ settings }) => {

config = settings.stylelint.config;
configOverrides = settings.stylelint.configOverrides;
configBasedir = settings.stylelint.configBasedir;
customSyntax = settings.stylelint.customSyntax;
reportNeedlessDisables = settings.stylelint.reportNeedlessDisables;
reportInvalidScopeDisables = settings.stylelint.reportInvalidScopeDisables;
Expand Down
3 changes: 3 additions & 0 deletions test/ws-config-basedir-test/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stylelint.configBasedir": "config-basedir"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
rules: {
indentation: [8],
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
rules: {
'color-hex-case': ['upper'],
},
};
68 changes: 68 additions & 0 deletions test/ws-config-basedir-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const path = require('path');
const pWaitFor = require('p-wait-for');
const test = require('tape');
const { extensions, workspace, window, Uri, commands, languages } = require('vscode');
const { normalizeDiagnostic } = require('../utils');

const run = () =>
test('vscode-stylelint with "stylelint.configBasedir"', async (t) => {
await commands.executeCommand('vscode.openFolder', Uri.file(__dirname));

const vscodeStylelint = extensions.getExtension('stylelint.vscode-stylelint');

// Open the './test.css' file.
const cssDocument = await workspace.openTextDocument(path.resolve(__dirname, 'test.css'));

await window.showTextDocument(cssDocument);

// Wait for diagnostics result.
await pWaitFor(() => vscodeStylelint.isActive, { timeout: 2000 });
await pWaitFor(() => languages.getDiagnostics(cssDocument.uri).length > 0, { timeout: 5000 });

// Check the result.
const diagnostics = languages.getDiagnostics(cssDocument.uri);

t.deepEqual(
diagnostics.map(normalizeDiagnostic),
[
{
range: { start: { line: 2, character: 9 }, end: { line: 2, character: 9 } },
message: 'Expected "#fff" to be "#FFF" (color-hex-case)',
severity: 0,
code: {
value: 'color-hex-case',
target: {
scheme: 'https',
authority: 'stylelint.io',
path: '/user-guide/rules/color-hex-case',
},
},
source: 'stylelint',
},
{
range: { start: { line: 2, character: 2 }, end: { line: 2, character: 2 } },
message: 'Expected indentation of 8 spaces (indentation)',
severity: 0,
code: {
value: 'indentation',
target: {
scheme: 'https',
authority: 'stylelint.io',
path: '/user-guide/rules/indentation',
},
},
source: 'stylelint',
},
],
'should work even if "stylelint.configBasedir" is defined.',
);

t.end();
});

exports.run = (root, done) => {
test.onFinish(done);
run();
};
5 changes: 5 additions & 0 deletions test/ws-config-basedir-test/stylelint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

module.exports = {
extends: ['./stylelint-config1', './stylelint-config2'],
};
4 changes: 4 additions & 0 deletions test/ws-config-basedir-test/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* prettier-ignore */
a {
color: #fff;
}

0 comments on commit 9a0c69d

Please sign in to comment.