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