| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- 'use strict';
- const atRuleParamIndex = require('../../utils/atRuleParamIndex');
- const declarationValueIndex = require('../../utils/declarationValueIndex');
- const functionArgumentsSearch = require('../../utils/functionArgumentsSearch');
- const getAtRuleParams = require('../../utils/getAtRuleParams');
- const getDeclarationValue = require('../../utils/getDeclarationValue');
- const isStandardSyntaxUrl = require('../../utils/isStandardSyntaxUrl');
- const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
- const optionsMatches = require('../../utils/optionsMatches');
- const report = require('../../utils/report');
- const ruleMessages = require('../../utils/ruleMessages');
- const validateOptions = require('../../utils/validateOptions');
- const ruleName = 'function-url-quotes';
- const messages = ruleMessages(ruleName, {
- expected: (functionName) => `Expected quotes around "${functionName}" function argument`,
- rejected: (functionName) => `Unexpected quotes around "${functionName}" function argument`,
- });
- const meta = {
- url: 'https://stylelint.io/user-guide/rules/function-url-quotes',
- fixable: true,
- };
- /** @type {import('stylelint').Rule} */
- const rule = (primary, secondaryOptions, context) => {
- return (root, result) => {
- const validOptions = validateOptions(
- result,
- ruleName,
- {
- actual: primary,
- possible: ['always', 'never'],
- },
- {
- actual: secondaryOptions,
- possible: {
- except: ['empty'],
- },
- optional: true,
- },
- );
- if (!validOptions) {
- return;
- }
- const exceptEmpty = optionsMatches(secondaryOptions, 'except', 'empty');
- const emptyArgumentPatterns = new Set(['', "''", '""']);
- root.walkAtRules(checkAtRuleParams);
- root.walkDecls(checkDeclParams);
- /**
- * @param {import('postcss').Declaration} decl
- */
- function checkDeclParams(decl) {
- if (!isStandardSyntaxDeclaration(decl)) return;
- const value = getDeclarationValue(decl);
- const startIndex = declarationValueIndex(decl);
- const parsed = functionArgumentsSearch(value, /^url$/i, (args, index, funcNode) => {
- checkArgs(decl, args, startIndex + index, funcNode);
- });
- if (context.fix) {
- decl.value = parsed.toString();
- }
- }
- /**
- * @param {import('postcss').AtRule} atRule
- */
- function checkAtRuleParams(atRule) {
- const params = getAtRuleParams(atRule);
- const startIndex = atRuleParamIndex(atRule);
- const parsed = functionArgumentsSearch(params, /^url$/i, (args, index, funcNode) => {
- checkArgs(atRule, args, startIndex + index, funcNode);
- });
- if (context.fix) {
- atRule.params = parsed.toString();
- }
- }
- /**
- * @param {import('postcss-value-parser').FunctionNode} funcNode
- */
- function addQuotes(funcNode) {
- for (const argNode of funcNode.nodes) {
- if (argNode.type === 'word') {
- argNode.value = `"${argNode.value}"`;
- }
- }
- }
- /**
- * @param {import('postcss-value-parser').FunctionNode} funcNode
- */
- function removeQuotes(funcNode) {
- for (const argNode of funcNode.nodes) {
- if (argNode.type === 'string') {
- // NOTE: We can ignore this error because the test passes.
- // @ts-expect-error -- TS2322: Type '"word"' is not assignable to type '"string"'.
- argNode.type = 'word';
- }
- }
- }
- /**
- * @param {import('postcss').Declaration | import('postcss').AtRule} node
- * @param {string} args
- * @param {number} index
- * @param {import('postcss-value-parser').FunctionNode} funcNode
- */
- function checkArgs(node, args, index, funcNode) {
- const functionName = funcNode.value.toLowerCase();
- let shouldHasQuotes = primary === 'always';
- const leftTrimmedArgs = args.trimStart();
- if (!isStandardSyntaxUrl(leftTrimmedArgs)) {
- return;
- }
- const complaintIndex = index + args.length - leftTrimmedArgs.length;
- const complaintEndIndex = index + args.length;
- const hasQuotes = leftTrimmedArgs.startsWith("'") || leftTrimmedArgs.startsWith('"');
- if (exceptEmpty && emptyArgumentPatterns.has(args.trim())) {
- shouldHasQuotes = !shouldHasQuotes;
- }
- if (shouldHasQuotes) {
- if (hasQuotes) {
- return;
- }
- if (context.fix) {
- addQuotes(funcNode);
- return;
- }
- complain(messages.expected(functionName), node, complaintIndex, complaintEndIndex);
- } else {
- if (!hasQuotes) {
- return;
- }
- if (context.fix) {
- removeQuotes(funcNode);
- return;
- }
- complain(messages.rejected(functionName), node, complaintIndex, complaintEndIndex);
- }
- }
- /**
- * @param {string} message
- * @param {import('postcss').Node} node
- * @param {number} index
- * @param {number} endIndex
- */
- function complain(message, node, index, endIndex) {
- report({
- message,
- node,
- index,
- endIndex,
- result,
- ruleName,
- });
- }
- };
- };
- rule.ruleName = ruleName;
- rule.messages = messages;
- rule.meta = meta;
- module.exports = rule;
|