index.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import valueParser from 'postcss-value-parser';
  2. import { atRuleParamIndex } from '../../utils/nodeFieldIndices.mjs';
  3. import { atRuleRegexes } from '../../utils/regexes.mjs';
  4. import getAtRuleParams from '../../utils/getAtRuleParams.mjs';
  5. import report from '../../utils/report.mjs';
  6. import ruleMessages from '../../utils/ruleMessages.mjs';
  7. import setAtRuleParams from '../../utils/setAtRuleParams.mjs';
  8. import validateOptions from '../../utils/validateOptions.mjs';
  9. const ruleName = 'import-notation';
  10. const messages = ruleMessages(ruleName, {
  11. expected: (unfixed, fixed) => `Expected "${unfixed}" to be "${fixed}"`,
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/import-notation',
  15. fixable: true,
  16. };
  17. /** @typedef {import('postcss').AtRule} AtRule */
  18. /**
  19. * @param {AtRule} node
  20. * @param {string} fixed
  21. * @param {number} index
  22. */
  23. const fixer = (node, fixed, index) => () => {
  24. const restAtRuleParams = node.params.slice(index);
  25. setAtRuleParams(node, `${fixed}${restAtRuleParams}`);
  26. };
  27. /** @type {import('stylelint').CoreRules[ruleName]} */
  28. const rule = (primary) => {
  29. return (root, result) => {
  30. const validOptions = validateOptions(result, ruleName, {
  31. actual: primary,
  32. possible: ['string', 'url'],
  33. });
  34. if (!validOptions) return;
  35. root.walkAtRules(atRuleRegexes.importName, checkAtRuleImportParams);
  36. /** @param {AtRule} atRule */
  37. function checkAtRuleImportParams(atRule) {
  38. const params = getAtRuleParams(atRule);
  39. const index = atRuleParamIndex(atRule);
  40. const parsed = valueParser(params);
  41. for (const node of parsed.nodes) {
  42. const { sourceEndIndex, type, value } = node;
  43. const endIndex = index + sourceEndIndex;
  44. const problem = { node: atRule, index, endIndex, result, ruleName };
  45. if (primary === 'string') {
  46. if (type !== 'function' || value.toLowerCase() !== 'url') continue;
  47. const urlFunctionFull = valueParser.stringify(node);
  48. const urlFunctionArguments = valueParser.stringify(node.nodes);
  49. const quotedUrlFunctionFirstArgument =
  50. node.nodes[0]?.type === 'word' ? `"${urlFunctionArguments}"` : urlFunctionArguments;
  51. const fix = fixer(atRule, quotedUrlFunctionFirstArgument, sourceEndIndex);
  52. const message = messages.expected;
  53. const messageArgs = [urlFunctionFull, quotedUrlFunctionFirstArgument];
  54. report({ ...problem, message, messageArgs, fix: { apply: fix, node: problem.node } });
  55. return;
  56. }
  57. if (primary === 'url') {
  58. if (type === 'space') return;
  59. if (type !== 'word' && type !== 'string') continue;
  60. const path = valueParser.stringify(node);
  61. const urlFunctionFull = `url(${path})`;
  62. const quotedNodeValue =
  63. type === 'word' ? `"${value}"` : `${node.quote}${value}${node.quote}`;
  64. const fix = fixer(atRule, urlFunctionFull, sourceEndIndex);
  65. const message = messages.expected;
  66. const messageArgs = [quotedNodeValue, urlFunctionFull];
  67. report({ ...problem, message, messageArgs, fix: { apply: fix, node: problem.node } });
  68. return;
  69. }
  70. }
  71. }
  72. };
  73. };
  74. rule.ruleName = ruleName;
  75. rule.messages = messages;
  76. rule.meta = meta;
  77. export default rule;