Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame^] | 3 | const isScssVariable = require('./isScssVariable'); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 4 | const { isRoot } = require('./typeGuards'); |
| 5 | |
| 6 | /** |
| 7 | * @param {string} [lang] |
| 8 | */ |
| 9 | function 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 Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame^] | 16 | * @param {import('postcss').Declaration | import('postcss-less').Declaration} decl |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 17 | */ |
| 18 | module.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 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame^] | 33 | // SCSS var |
| 34 | if (isScssVariable(prop)) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 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 |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame^] | 56 | if ('extend' in decl && decl.extend) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
| 60 | return true; |
| 61 | }; |