From 8e488b2dcd9b9f05dab44df73ece0222b1619e85 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 15 Jul 2024 20:54:11 +0100 Subject: [PATCH] Fix no-prototype-builtins issues in Ruleset and ToCSSVisitor --- packages/less/src/less/tree/ruleset.js | 3 +-- .../less/src/less/visitors/to-css-visitor.js | 17 +++++++---------- packages/test-data/css/_main/rulesets.css | 1 + packages/test-data/less/_main/rulesets.less | 1 + 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/less/src/less/tree/ruleset.js b/packages/less/src/less/tree/ruleset.js index a3324cf07..f7aadd84b 100644 --- a/packages/less/src/less/tree/ruleset.js +++ b/packages/less/src/less/tree/ruleset.js @@ -295,8 +295,7 @@ Ruleset.prototype = Object.assign(new Node(), { if (r.type === 'Import' && r.root && r.root.variables) { const vars = r.root.variables(); for (const name in vars) { - // eslint-disable-next-line no-prototype-builtins - if (vars.hasOwnProperty(name)) { + if (Object.prototype.hasOwnProperty.call(vars, name)) { hash[name] = r.root.variable(name); } } diff --git a/packages/less/src/less/visitors/to-css-visitor.js b/packages/less/src/less/visitors/to-css-visitor.js index 54ddd6398..d1d200951 100644 --- a/packages/less/src/less/visitors/to-css-visitor.js +++ b/packages/less/src/less/visitors/to-css-visitor.js @@ -303,19 +303,16 @@ ToCSSVisitor.prototype = { // remove duplicates const ruleCache = {}; - let ruleList; - let rule; - let i; - - for (i = rules.length - 1; i >= 0 ; i--) { - rule = rules[i]; + for (let i = rules.length - 1; i >= 0 ; i--) { + let rule = rules[i]; if (rule instanceof tree.Declaration) { - if (!ruleCache[rule.name]) { + if (!Object.prototype.hasOwnProperty.call(ruleCache, rule.name)) { ruleCache[rule.name] = rule; } else { - ruleList = ruleCache[rule.name]; - if (ruleList instanceof tree.Declaration) { - ruleList = ruleCache[rule.name] = [ruleCache[rule.name].toCSS(this._context)]; + let ruleList = ruleCache[rule.name]; + if (!Array.isArray(ruleList)) { + const prevRuleCSS = ruleList.toCSS(this._context); + ruleList = ruleCache[rule.name] = [prevRuleCSS]; } const ruleCSS = rule.toCSS(this._context); if (ruleList.indexOf(ruleCSS) !== -1) { diff --git a/packages/test-data/css/_main/rulesets.css b/packages/test-data/css/_main/rulesets.css index 408c76aad..5b5c285db 100644 --- a/packages/test-data/css/_main/rulesets.css +++ b/packages/test-data/css/_main/rulesets.css @@ -1,5 +1,6 @@ #first > .one { font-size: 2em; + hasOwnProperty: blue; } #first > .one > #second .two > #deux { width: 50%; diff --git a/packages/test-data/less/_main/rulesets.less b/packages/test-data/less/_main/rulesets.less index e81192dbc..f724ec71d 100644 --- a/packages/test-data/less/_main/rulesets.less +++ b/packages/test-data/less/_main/rulesets.less @@ -27,4 +27,5 @@ } } font-size: 2em; + hasOwnProperty: blue; }