index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. const isStandardSyntaxAtRule = require('../../utils/isStandardSyntaxAtRule');
  3. const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
  4. const optionsMatches = require('../../utils/optionsMatches');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const { isRegExp, isString } = require('../../utils/validateTypes');
  9. const ruleName = 'no-invalid-position-at-import-rule';
  10. const messages = ruleMessages(ruleName, {
  11. rejected: 'Unexpected invalid position @import rule',
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/list/no-invalid-position-at-import-rule',
  15. };
  16. /** @type {import('stylelint').Rule} */
  17. const rule = (primary, options) => {
  18. return (root, result) => {
  19. const validOptions = validateOptions(
  20. result,
  21. ruleName,
  22. { actual: primary },
  23. {
  24. actual: options,
  25. possible: {
  26. ignoreAtRules: [isString, isRegExp],
  27. },
  28. optional: true,
  29. },
  30. );
  31. if (!validOptions) {
  32. return;
  33. }
  34. let invalidPosition = false;
  35. root.walk((node) => {
  36. const nodeName = ('name' in node && node.name && node.name.toLowerCase()) || '';
  37. if (
  38. (node.type === 'atrule' &&
  39. nodeName !== 'charset' &&
  40. nodeName !== 'import' &&
  41. !optionsMatches(options, 'ignoreAtRules', node.name) &&
  42. isStandardSyntaxAtRule(node)) ||
  43. (node.type === 'rule' && isStandardSyntaxRule(node))
  44. ) {
  45. invalidPosition = true;
  46. return;
  47. }
  48. if (node.type === 'atrule' && nodeName === 'import' && invalidPosition) {
  49. report({
  50. message: messages.rejected,
  51. node,
  52. result,
  53. ruleName,
  54. });
  55. }
  56. });
  57. };
  58. };
  59. rule.ruleName = ruleName;
  60. rule.messages = messages;
  61. rule.meta = meta;
  62. module.exports = rule;