blob: d8291befef519be620ac6836b1d407f9fae6e6cd [file] [log] [blame]
Mathias Bynens79e2cf02020-05-29 16:46:17 +02001'use strict';
2
Tim van der Lippeefb716a2020-12-01 12:54:04 +00003const isScssVariable = require('./isScssVariable');
Tim van der Lippe16b82282021-11-08 13:50:26 +00004const { isRoot, isRule } = require('./typeGuards');
Mathias Bynens79e2cf02020-05-29 16:46:17 +02005
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 *
Tim van der Lippeefb716a2020-12-01 12:54:04 +000016 * @param {import('postcss').Declaration | import('postcss-less').Declaration} decl
Mathias Bynens79e2cf02020-05-29 16:46:17 +020017 */
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 (
Tim van der Lippe16b82282021-11-08 13:50:26 +000024 parent &&
Mathias Bynens79e2cf02020-05-29 16:46:17 +020025 isRoot(parent) &&
26 parent.source &&
27 !isStandardSyntaxLang(
Tim van der Lippe16b82282021-11-08 13:50:26 +000028 /** @type {import('postcss').Source & {lang?: string}} */ (parent.source).lang,
Mathias Bynens79e2cf02020-05-29 16:46:17 +020029 )
30 ) {
31 return false;
32 }
33
Tim van der Lippe4cb09742022-01-07 14:25:03 +010034 // SCSS var; covers map and list declarations
Tim van der Lippeefb716a2020-12-01 12:54:04 +000035 if (isScssVariable(prop)) {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020036 return false;
37 }
38
39 // Less var (e.g. @var: x), but exclude variable interpolation (e.g. @{var})
40 if (prop[0] === '@' && prop[1] !== '{') {
41 return false;
42 }
43
Tim van der Lippe16b82282021-11-08 13:50:26 +000044 // Less map declaration
45 if (parent && parent.type === 'atrule' && parent.raws.afterName === ':') {
46 return false;
47 }
48
Tim van der Lippe4cb09742022-01-07 14:25:03 +010049 // Less map (e.g. #my-map() { myprop: red; })
50 if (
51 parent &&
52 isRule(parent) &&
53 parent.selector &&
54 parent.selector.startsWith('#') &&
55 parent.selector.endsWith('()')
56 ) {
57 return false;
58 }
59
Mathias Bynens79e2cf02020-05-29 16:46:17 +020060 // Sass nested properties (e.g. border: { style: solid; color: red; })
61 if (
Tim van der Lippe16b82282021-11-08 13:50:26 +000062 parent &&
63 isRule(parent) &&
Mathias Bynens79e2cf02020-05-29 16:46:17 +020064 parent.selector &&
Mathias Bynens79e2cf02020-05-29 16:46:17 +020065 parent.selector[parent.selector.length - 1] === ':' &&
Mathias Bynens79e2cf02020-05-29 16:46:17 +020066 parent.selector.substring(0, 2) !== '--'
67 ) {
68 return false;
69 }
70
71 // Less &:extend
Tim van der Lippeefb716a2020-12-01 12:54:04 +000072 if ('extend' in decl && decl.extend) {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020073 return false;
74 }
75
76 return true;
77};