Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function-no-unknown false negatives for functions with namespace #2

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/rules/function-no-unknown/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ testRule({
code: "a { color: color.unknown(#6b717f, $red: 15); }",
message: messages.rejected("color.unknown"),
line: 1,
column: 18
column: 12
},
{
code: `
Expand All @@ -123,7 +123,7 @@ testRule({
`,
message: messages.rejected("othermodule.myfunction"),
line: 5,
column: 31,
column: 19,
description: "non-matching @use namespace"
},
{
Expand All @@ -136,7 +136,7 @@ testRule({
`,
message: messages.rejected("c.myfunction"),
line: 5,
column: 21,
column: 19,
description: "non-matching @use namespace, 'as' keyword"
},
{
Expand Down
92 changes: 58 additions & 34 deletions src/rules/function-no-unknown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const meta = {
url: ruleUrl(ruleName)
};

function isNamespacedFunction(fn) {
const namespacedFunc = /^\w+\.\w+$/;
return namespacedFunc.test(fn);
function extractNamespaceFromFunction(fn) {
const matched = fn.match(/^(\w+)\.\w+$/);
return matched ? matched[1] : undefined;
}

function isAtUseAsSyntax(nodes) {
Expand Down Expand Up @@ -78,51 +78,75 @@ function rule(primaryOption, secondaryOptions) {
ignoreFunctions
});

const atUseNamespaces = new Set();
root.walkAtRules(/^use$/i, atRule => {
const { nodes } = valueParser(atRule.params);
atUseNamespaces.add(getAtUseNamespace(nodes));
});

const namespaceWarnings = new Set();

utils.checkAgainstRule(
{
ruleName: ruleToCheckAgainst,
ruleSettings: [primaryOption, newSecondaryOptions],
root
},
warning => {
const { node, index } = warning;
const { node: decl } = warning;

// NOTE: Using `valueParser` is necessary for extracting a function name. This may be a performance waste.
valueParser(node.value).walk(valueNode => {
valueParser(decl.value).walk(valueNode => {
const { type, value: funcName } = valueNode;

if (type !== "function" || funcName.trim() === "") {
return;
}

if (isNamespacedFunction(funcName)) {
const atUseNamespaces = [];

root.walkAtRules(/^use$/i, atRule => {
const { nodes } = valueParser(atRule.params);
atUseNamespaces.push(getAtUseNamespace(nodes));
});

if (atUseNamespaces.length) {
const [namespace] = funcName.split(".");
if (atUseNamespaces.includes(namespace)) {
return;
}
}
}

if (!ignoreFunctionsAsSet.has(funcName)) {
utils.report({
message: messages.rejected(funcName),
ruleName,
result,
node,
index
});
}
if (type !== "function" || funcName.trim() === "") return;

// TODO: For backward compatibility with Stylelint 15.7.0 or less.
// We can remove this code when dropping support for old version.
const namespace = extractNamespaceFromFunction(funcName);
if (namespace && atUseNamespaces.has(namespace)) return;

if (ignoreFunctionsAsSet.has(funcName)) return;

utils.report({
message: messages.rejected(funcName),
ruleName,
result,
node: decl,
word: funcName
});

namespaceWarnings.add(warning);
});
}
);

// NOTE: Since Stylelint 15.8.0, the built-in `function-no-unknown` rule has ignored SCSS functions with namespace.
// See https://github.com/stylelint/stylelint/releases/tag/15.8.0
// See https://github.com/stylelint/stylelint/pull/6921
if (namespaceWarnings.size === 0) {
root.walkDecls(decl => {
valueParser(decl.value).walk(valueNode => {
const { type, value: funcName } = valueNode;

if (type !== "function" || funcName.trim() === "") return;

const namespace = extractNamespaceFromFunction(funcName);
if (!namespace) return;
if (atUseNamespaces.has(namespace)) return;

if (ignoreFunctionsAsSet.has(funcName)) return;

utils.report({
message: messages.rejected(funcName),
ruleName,
result,
node: decl,
word: funcName
});
});
});
}
};
}

Expand Down