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; |
| 21 | |
| 22 | // Declarations belong in a declaration block or standard CSS source |
| 23 | if ( |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 24 | parent && |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 25 | isRoot(parent) && |
| 26 | parent.source && |
| 27 | !isStandardSyntaxLang( |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 28 | /** @type {import('postcss').Source & {lang?: string}} */ (parent.source).lang, |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 29 | ) |
| 30 | ) { |
| 31 | return false; |
| 32 | } |
| 33 | |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 34 | // SCSS var; covers map and list declarations |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 35 | if (isScssVariable(prop)) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 36 | 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 Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 44 | // Less map declaration |
| 45 | if (parent && parent.type === 'atrule' && parent.raws.afterName === ':') { |
| 46 | return false; |
| 47 | } |
| 48 | |
Tim van der Lippe | 4cb0974 | 2022-01-07 14:25:03 +0100 | [diff] [blame^] | 49 | // 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 Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 60 | // Sass nested properties (e.g. border: { style: solid; color: red; }) |
| 61 | if ( |
Tim van der Lippe | 16b8228 | 2021-11-08 13:50:26 +0000 | [diff] [blame] | 62 | parent && |
| 63 | isRule(parent) && |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 64 | parent.selector && |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 65 | parent.selector[parent.selector.length - 1] === ':' && |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 66 | parent.selector.substring(0, 2) !== '--' |
| 67 | ) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // Less &:extend |
Tim van der Lippe | efb716a | 2020-12-01 12:54:04 +0000 | [diff] [blame] | 72 | if ('extend' in decl && decl.extend) { |
Mathias Bynens | 79e2cf0 | 2020-05-29 16:46:17 +0200 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | }; |