Skip to content

Commit

Permalink
Merge pull request #119 from stylelint/syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ntwb authored Jun 8, 2020
2 parents ec802e8 + dcc47a7 commit f6f3672
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 50 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ 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.syntax

Type: `"css" | "css-in-js" | "html" | "less" | "markdown" | "sass" | "scss" | "sugarss"`
Default: `""`

Set stylelint [`syntax`](https://stylelint.io/user-guide/usage/options#syntax) option. Specify a syntax. Only use this option if you want to force a specific syntax.

#### stylelint.customSyntax

Type: `string`
Expand Down
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,22 @@
"default": "",
"description": "A path to the directory that relative paths defining \"extends\" and \"plugins\" are relative to."
},
"stylelint.syntax": {
"type": "string",
"default": "",
"enum": [
"css",
"css-in-js",
"html",
"less",
"markdown",
"sass",
"scss",
"sugarss",
""
],
"description": "Specify a syntax. Only use this option if you want to force a specific syntax."
},
"stylelint.customSyntax": {
"type": "string",
"default": "",
Expand Down
7 changes: 7 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ let configOverrides;
let configBasedir;
/** @type {PackageManager} */
let packageManager;
/** @type { "css-in-js" | "html" | "less" | "markdown" | "sass" | "scss" | "sugarss" | undefined } */
let syntax;
/** @type {string} */
let customSyntax;
/** @type {boolean} */
Expand Down Expand Up @@ -116,6 +118,10 @@ async function buildStylelintOptions(document, baseOptions = {}) {
options.reportInvalidScopeDisables = reportInvalidScopeDisables;
}

if (syntax) {
options.syntax = syntax;
}

const workspaceFolder = await getWorkspaceFolder(document);
const documentPath = parseUri(document.uri).fsPath;

Expand Down Expand Up @@ -317,6 +323,7 @@ connection.onDidChangeConfiguration(({ settings }) => {
config = settings.stylelint.config;
configOverrides = settings.stylelint.configOverrides;
configBasedir = settings.stylelint.configBasedir;
syntax = settings.stylelint.syntax || undefined;
customSyntax = settings.stylelint.customSyntax;
ignoreDisables = settings.stylelint.ignoreDisables;
reportNeedlessDisables = settings.stylelint.reportNeedlessDisables;
Expand Down
7 changes: 7 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
'use strict';

const { languages } = require('vscode');

module.exports = {
normalizeDiagnostic,
getStylelintDiagnostics,
};

function normalizeDiagnostic(message) {
return { ...message, code: normalizeCode(message.code), range: normalizeRange(message.range) };
}

function getStylelintDiagnostics(uri) {
return languages.getDiagnostics(uri).filter((d) => d.source === 'stylelint');
}

function normalizeRange(range) {
const obj = {
start: {
Expand Down
8 changes: 4 additions & 4 deletions test/ws-config-basedir-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
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 { extensions, workspace, window, Uri, commands } = require('vscode');
const { normalizeDiagnostic, getStylelintDiagnostics } = require('../utils');

const run = () =>
test('vscode-stylelint with "stylelint.configBasedir"', async (t) => {
Expand All @@ -19,10 +19,10 @@ const run = () =>

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

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

t.deepEqual(
diagnostics.map(normalizeDiagnostic),
Expand Down
8 changes: 4 additions & 4 deletions test/ws-ignore-disables-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
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 { extensions, workspace, window, Uri, commands } = require('vscode');
const { normalizeDiagnostic, getStylelintDiagnostics } = require('../utils');

const run = () =>
test('vscode-stylelint with "stylelint.ignoreDisables"', async (t) => {
Expand All @@ -19,10 +19,10 @@ const run = () =>

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

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

t.deepEqual(
diagnostics.map(normalizeDiagnostic),
Expand Down
16 changes: 8 additions & 8 deletions test/ws-lint-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
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 { extensions, workspace, window, Uri, commands } = require('vscode');
const { normalizeDiagnostic, getStylelintDiagnostics } = require('../utils');

const run = () =>
test('vscode-stylelint lint test', async (t) => {
Expand All @@ -19,11 +19,11 @@ const run = () =>

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

// Check the result.
t.deepEqual(
languages.getDiagnostics(cssDocument.uri).map(normalizeDiagnostic),
getStylelintDiagnostics(cssDocument.uri).map(normalizeDiagnostic),
[
{
range: { start: { line: 2, character: 2 }, end: { line: 2, character: 2 } },
Expand All @@ -49,11 +49,11 @@ const run = () =>
await window.showTextDocument(scssDocument);

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

// Check the result.
t.deepEqual(
languages.getDiagnostics(scssDocument.uri).map(normalizeDiagnostic),
getStylelintDiagnostics(scssDocument.uri).map(normalizeDiagnostic),
[
{
range: { start: { line: 2, character: 2 }, end: { line: 2, character: 2 } },
Expand All @@ -79,11 +79,11 @@ const run = () =>
await window.showTextDocument(mdDocument);

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

// Check the result.
t.deepEqual(
languages.getDiagnostics(mdDocument.uri).map(normalizeDiagnostic),
getStylelintDiagnostics(mdDocument.uri).map(normalizeDiagnostic),
[
{
range: { start: { line: 4, character: 2 }, end: { line: 4, character: 2 } },
Expand Down
8 changes: 4 additions & 4 deletions test/ws-report-invalid-scope-disables-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
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 { extensions, workspace, window, Uri, commands } = require('vscode');
const { normalizeDiagnostic, getStylelintDiagnostics } = require('../utils');

const run = () =>
test('vscode-stylelint with "stylelint.reportInvalidScopeDisables"', async (t) => {
Expand All @@ -19,10 +19,10 @@ const run = () =>

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

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

t.deepEqual(
diagnostics.map(normalizeDiagnostic),
Expand Down
8 changes: 4 additions & 4 deletions test/ws-report-needless-disables-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
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 { extensions, workspace, window, Uri, commands } = require('vscode');
const { normalizeDiagnostic, getStylelintDiagnostics } = require('../utils');

const run = () =>
test('vscode-stylelint with "stylelint.reportNeedlessDisables"', async (t) => {
Expand All @@ -19,10 +19,10 @@ const run = () =>

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

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

t.deepEqual(
diagnostics.map(normalizeDiagnostic),
Expand Down
16 changes: 4 additions & 12 deletions test/ws-rule-doc-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
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 { extensions, workspace, window, Uri, commands } = require('vscode');
const { normalizeDiagnostic, getStylelintDiagnostics } = require('../utils');

const run = () =>
test('vscode-stylelint lint test', async (t) => {
Expand All @@ -19,19 +19,11 @@ const run = () =>

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

// Check the result.
t.deepEqual(
languages
.getDiagnostics(cssDocument.uri)
.filter((d) => d.source === 'stylelint')
.map(normalizeDiagnostic),
getStylelintDiagnostics(cssDocument.uri).map(normalizeDiagnostic),
[
{
range: { start: { line: 0, character: 5 }, end: { line: 0, character: 5 } },
Expand Down
8 changes: 4 additions & 4 deletions test/ws-stylelint-path-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
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 { extensions, workspace, window, Uri, commands } = require('vscode');
const { normalizeDiagnostic, getStylelintDiagnostics } = require('../utils');

const run = () =>
test('vscode-stylelint with "stylelint.stylelintPath"', async (t) => {
Expand All @@ -19,10 +19,10 @@ const run = () =>

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

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

t.deepEqual(
diagnostics.map(normalizeDiagnostic),
Expand Down
3 changes: 3 additions & 0 deletions test/ws-syntax-test/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"stylelint.syntax": "sass"
}
54 changes: 54 additions & 0 deletions test/ws-syntax-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

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

const run = () =>
test('vscode-stylelint with "stylelint.syntax"', 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(() => getStylelintDiagnostics(cssDocument.uri).length > 0, { timeout: 5000 });

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

t.deepEqual(
diagnostics.map(normalizeDiagnostic),
[
{
range: { start: { line: 2, character: 2 }, end: { line: 2, character: 2 } },
message: 'Expected indentation of 4 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.syntax" is defined.',
);

t.end();
});

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

module.exports = {
rules: {
indentation: [4],
},
};
3 changes: 3 additions & 0 deletions test/ws-syntax-test/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* prettier-ignore */
a
color: #fff;
Loading

0 comments on commit f6f3672

Please sign in to comment.