From 85432276a1f7c67f8db88ff1613f8cf4181df975 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/less/_main/rulesets.less | 1 + 3 files changed, 9 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..b27cdb0ca 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 = rule.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/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; }