/stylelint-vscode

stylelint wrapper to easily integrate with Visual Studio Code

Primary LanguageJavaScriptMIT LicenseMIT

stylelint-vscode

NPM version Build Status Coverage Status

stylelint wrapper to easily integrate with Visual Studio Code language server

const stylelintVSCode = require('stylelint-vscode');

const code = `
p {
  line-height: .8;
  color: red;
}`;

stylelintVSCode({
  code,
  config: {
    rules: {
      'number-leading-zero': 'always',
      'color-named': ['never', {severity: 'warning'}]
    }
  }
}).then(diagnostics => {
  diagnostics;
  /* =>
    [{
      message: 'Expected a leading zero (number-leading-zero)',
      severity: 1,
      range: {
        start: {line: 2, character: 14},
        end: {line: 2, character: 14}
      },
      source: 'stylelint'
    }, {
      message: 'Unexpected named color "red" (color-no-named)',
      severity: 2,
      range: {
        start: {line: 3, character: 9},
        end: {line: 3, character: 9}
      },
      source: 'stylelint'
    }]
  */
});

Installation

Use npm.

npm install stylelint-vscode

API

const stylelintVSCode = require('stylelint-vscode');

stylelintVSCode(options)

options: Object (directly passed to stylelint.lint)
Return: Promise instance

It works like stylelint.lint, except for:

  • It will be resolved with an array of VS Code's Diagnostic instances.
  • It will be rejected (not resolved) when it takes invalid configs.
    • In this case, it joins config errors into a single error object by using array-to-error.
  • It suppresses No configuration found error.
    • Doing nothing when there is no configuration is a common behavior of editor plugins.
  • code option is required and files option is not supported.
    • Because extension authors can derive file contents via TextDocument#getText() and there is no need to let stylelint read physical files.
const stylelintVSCode = require('stylelint-vscode');

stylelintVSCode({
  code: '{foo}'
}).then(diagnostics => {
  diagnostics;
  /* =>
    [{
      message: 'Unknown word',
      severity: 1,
      range: {
        start: {line: 0, character: 1},
        end: {line: 0, character: 1}
      },
      source: 'stylelint'
    }]
  */
});

stylelintVSCode({
  code: 'a {}',
  config: {
    rules: {
      indentation: 2,
      'function-comma-space-before': 'foo'
    }
  }
}).catch(err => {
  err.name;
  //=> 'SyntaxError'

  err.message;
  //=> 'Expected option value for rule "indentation"\nInvalid option value "foo" for rule "function-comma-space-before"'

  err.reasons;
  /* =>
    [
      'Expected option value for rule "indentation"',
      'Invalid option value "foo" for rule "function-comma-space-before"'
    ]
  */
});

License

Copyright (c) 2015 - 2017 Shinnosuke Watanabe

Licensed under the MIT License.