From b7a30d8a13dce8cabc1d160cac634acb5cf30524 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Fri, 19 Apr 2024 16:15:39 +0200 Subject: [PATCH 1/3] Support at-rules in `no-global-function-names` --- .../__tests__/index.js | 55 +++++++++++++++++++ src/rules/no-global-function-names/index.js | 15 ++++- 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/src/rules/no-global-function-names/__tests__/index.js b/src/rules/no-global-function-names/__tests__/index.js index 54752038..72af14fd 100644 --- a/src/rules/no-global-function-names/__tests__/index.js +++ b/src/rules/no-global-function-names/__tests__/index.js @@ -290,6 +290,26 @@ testRule({ @debug string.unique-id() `, description: "string.unique-id" + }, + { + code: ` + @use "sass:meta" + @function is-number($value) { + @return meta.type-of($value) == 'number' + } + `, + description: "Allowed non-global function in @return" + }, + { + code: ` + @use "sass:meta" + @mixin foo($name) { + @if meta.type-of($name) != 'string' { + @error 'name must be a string, but was ' + meta.type-of($name) + } + } + `, + description: "Allowed non-global function in @if and @error" } ], @@ -491,6 +511,41 @@ testRule({ "Expected color.adjust($color, $lightness: $amount) instead of lighten($color, $amount)" ), description: "lighten" + }, + { + code: ` + @function is-number($value) { + @return type-of($value) == 'number' + } + `, + line: 3, + column: 17, + message: messages.rejected("type-of"), + description: "Global function in @return" + }, + { + code: ` + @mixin foo($name) { + @if type-of($name) != 'string' { + @error 'name must be a test(string), but was' + type-of($name) + } + } + `, + warnings: [ + { + line: 3, + column: 13, + message: messages.rejected("type-of"), + description: "Global function in @if" + }, + { + line: 4, + column: 59, + message: messages.rejected("type-of"), + description: "Global function in @error" + } + ], + description: "Global function in @if and @error" } ] }); diff --git a/src/rules/no-global-function-names/index.js b/src/rules/no-global-function-names/index.js index 0fa707e9..e8e99a2c 100644 --- a/src/rules/no-global-function-names/index.js +++ b/src/rules/no-global-function-names/index.js @@ -2,6 +2,7 @@ const valueParser = require("postcss-value-parser"); const { utils } = require("stylelint"); +const getAtRuleParams = require("stylelint/lib/utils/getAtRuleParams.cjs"); const namespace = require("../../utils/namespace"); const ruleUrl = require("../../utils/ruleUrl"); @@ -173,7 +174,15 @@ function rule(value) { } root.walkDecls(decl => { - valueParser(decl.value).walk(node => { + checkValue(decl, decl.value); + }); + root.walkAtRules(atRule => { + const params = getAtRuleParams(atRule); + checkValue(atRule, params); + }); + + function checkValue(parentNode, value) { + valueParser(value).walk(node => { const cleanValue = node.value.replace(interpolationPrefix, ""); // Verify that we're only looking at functions. @@ -184,14 +193,14 @@ function rule(value) { if (rules[cleanValue]) { utils.report({ message: messages.rejected(cleanValue), - node: decl, + node: parentNode, word: cleanValue, result, ruleName }); } }); - }); + } }; } From febc4c61667bcd71a429de0e3d8813743c07fa15 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Thu, 2 May 2024 13:23:27 +0200 Subject: [PATCH 2/3] Copy upstream Stylelint util --- src/rules/no-global-function-names/index.js | 2 +- src/utils/getAtRuleParams.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 src/utils/getAtRuleParams.js diff --git a/src/rules/no-global-function-names/index.js b/src/rules/no-global-function-names/index.js index e8e99a2c..f6238135 100644 --- a/src/rules/no-global-function-names/index.js +++ b/src/rules/no-global-function-names/index.js @@ -2,7 +2,7 @@ const valueParser = require("postcss-value-parser"); const { utils } = require("stylelint"); -const getAtRuleParams = require("stylelint/lib/utils/getAtRuleParams.cjs"); +const getAtRuleParams = require("../../utils/getAtRuleParams"); const namespace = require("../../utils/namespace"); const ruleUrl = require("../../utils/ruleUrl"); diff --git a/src/utils/getAtRuleParams.js b/src/utils/getAtRuleParams.js new file mode 100644 index 00000000..dbeaca1c --- /dev/null +++ b/src/utils/getAtRuleParams.js @@ -0,0 +1,11 @@ +"use strict"; + +/** + * @param {import('postcss').AtRule} atRule + * @returns {string} + */ +function getAtRuleParams(atRule) { + return atRule.raws.params?.raw ?? atRule.params; +} + +module.exports = getAtRuleParams; From b03dbc03b1ecf80d89bb3102dbd41ff5b7c8ff81 Mon Sep 17 00:00:00 2001 From: Flo Edelmann Date: Thu, 2 May 2024 13:24:20 +0200 Subject: [PATCH 3/3] Format test file with `npm run prettify` --- .../__tests__/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/rules/at-import-partial-extension-allowed-list/__tests__/index.js b/src/rules/at-import-partial-extension-allowed-list/__tests__/index.js index f534ba95..e51585c1 100644 --- a/src/rules/at-import-partial-extension-allowed-list/__tests__/index.js +++ b/src/rules/at-import-partial-extension-allowed-list/__tests__/index.js @@ -83,8 +83,7 @@ testRule({ line: 2, column: 20, message: messages.rejected("scssy"), - description: - "One file, ext not from an allowed list, space at the end." + description: "One file, ext not from an allowed list, space at the end." }, { code: `