UNPKG

1.36 kBJavaScriptView Raw
1'use strict';
2
3const isScssVariable = require('./isScssVariable');
4const { isRoot } = require('./typeGuards');
5
6/**
7 * @param {string} [lang]
8 */
9function isStandardSyntaxLang(lang) {
10 return lang && (lang === 'css' || lang === 'custom-template' || lang === 'template-literal');
11}
12
13/**
14 * Check whether a declaration is standard
15 *
16 * @param {import('postcss').Declaration} decl
17 */
18module.exports = function (decl) {
19 const prop = decl.prop;
20 const parent = decl.parent;
21
22 // Declarations belong in a declaration block or standard CSS source
23 if (
24 isRoot(parent) &&
25 parent.source &&
26 !isStandardSyntaxLang(
27 /** @type {import('postcss').NodeSource & {lang?: string}} */ (parent.source).lang,
28 )
29 ) {
30 return false;
31 }
32
33 // SCSS var
34 if (isScssVariable(prop)) {
35 return false;
36 }
37
38 // Less var (e.g. @var: x), but exclude variable interpolation (e.g. @{var})
39 if (prop[0] === '@' && prop[1] !== '{') {
40 return false;
41 }
42
43 // Sass nested properties (e.g. border: { style: solid; color: red; })
44 if (
45 // @ts-ignore TODO TYPES selector does not exists
46 parent.selector &&
47 // @ts-ignore
48 parent.selector[parent.selector.length - 1] === ':' &&
49 // @ts-ignore
50 parent.selector.substring(0, 2) !== '--'
51 ) {
52 return false;
53 }
54
55 // Less &:extend
56 // @ts-ignore TODO TYPES extend does not exists
57 if (decl.extend) {
58 return false;
59 }
60
61 return true;
62};