/
stylelintCommand.js
53 lines (42 loc) · 1.09 KB
/
stylelintCommand.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
'use strict';
const { assertString } = require('./validateTypes');
const DISABLE_COMMAND = 'stylelint-disable';
const DISABLE_LINE_COMMAND = 'stylelint-disable-line';
const DISABLE_NEXT_LINE_COMMAND = 'stylelint-disable-next-line';
const ENABLE_COMMAND = 'stylelint-enable';
const ALL_COMMANDS = new Set([
DISABLE_COMMAND,
DISABLE_LINE_COMMAND,
DISABLE_NEXT_LINE_COMMAND,
ENABLE_COMMAND,
]);
/** @typedef {import('postcss').Comment} Comment */
/**
* Extract a command from a given comment.
*
* @param {Comment} comment
* @returns {string}
*/
function extractStylelintCommand(comment) {
const [command] = comment.text.split(/\s/, 1);
assertString(command);
return command;
}
/**
* Tests if the given comment is a Stylelint command.
*
* @param {Comment} comment
* @returns {boolean}
*/
function isStylelintCommand(comment) {
const command = extractStylelintCommand(comment);
return command !== undefined && ALL_COMMANDS.has(command);
}
module.exports = {
DISABLE_COMMAND,
DISABLE_LINE_COMMAND,
DISABLE_NEXT_LINE_COMMAND,
ENABLE_COMMAND,
extractStylelintCommand,
isStylelintCommand,
};