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'); |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 4 | const { isRoot, isRule } = require('./typeGuards'); |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 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; |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 21 | const value = decl.value; |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 22 | |
| 23 | // Declarations belong in a declaration block or standard CSS source |
| 24 | if ( |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 25 | parent && |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 26 | isRoot(parent) && |
| 27 | parent.source && |
| 28 | !isStandardSyntaxLang( |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 29 | /** @type {import('postcss').Source & {lang?: string}} */ (parent.source).lang, |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 30 | ) |
| 31 | ) { |
| 32 | return false; |
| 33 | } |
| 34 | |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 35 | // SCSS var |
| 36 | if (isScssVariable(prop)) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 40 | // SCSS map and list declarations |
| 41 | if (value.startsWith('(') && value.endsWith(')')) { |
| 42 | return false; |
| 43 | } |
| 44 | |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 45 | // Less var (e.g. @var: x), but exclude variable interpolation (e.g. @{var}) |
| 46 | if (prop[0] === '@' && prop[1] !== '{') { |
| 47 | return false; |
| 48 | } |
| 49 | |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 50 | // Less map declaration |
| 51 | if (parent && parent.type === 'atrule' && parent.raws.afterName === ':') { |
| 52 | return false; |
| 53 | } |
| 54 | |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 55 | // Sass nested properties (e.g. border: { style: solid; color: red; }) |
| 56 | if ( |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 57 | parent && |
| 58 | isRule(parent) && |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 59 | parent.selector && |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 60 | parent.selector[parent.selector.length - 1] === ':' && |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 61 | parent.selector.substring(0, 2) !== '--' |
| 62 | ) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | // Less &:extend |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 67 | if ('extend' in decl && decl.extend) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
| 71 | return true; |
| 72 | }; |