index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // @ts-nocheck
  2. 'use strict';
  3. const _ = require('lodash');
  4. const isCustomProperty = require('../../utils/isCustomProperty');
  5. const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
  6. const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
  7. const optionsMatches = require('../../utils/optionsMatches');
  8. const postcss = require('postcss');
  9. const properties = require('known-css-properties').all;
  10. const report = require('../../utils/report');
  11. const ruleMessages = require('../../utils/ruleMessages');
  12. const validateOptions = require('../../utils/validateOptions');
  13. const ruleName = 'property-no-unknown';
  14. const messages = ruleMessages(ruleName, {
  15. rejected: (property) => `Unexpected unknown property "${property}"`,
  16. });
  17. function rule(actual, options) {
  18. const allValidProperties = new Set(properties);
  19. return (root, result) => {
  20. const validOptions = validateOptions(
  21. result,
  22. ruleName,
  23. { actual },
  24. {
  25. actual: options,
  26. possible: {
  27. ignoreProperties: [_.isString, _.isRegExp],
  28. checkPrefixed: _.isBoolean,
  29. ignoreSelectors: [_.isString, _.isRegExp],
  30. },
  31. optional: true,
  32. },
  33. );
  34. if (!validOptions) {
  35. return;
  36. }
  37. const shouldCheckPrefixed = _.get(options, 'checkPrefixed');
  38. root.walkDecls((decl) => {
  39. const prop = decl.prop;
  40. if (!isStandardSyntaxProperty(prop)) {
  41. return;
  42. }
  43. if (!isStandardSyntaxDeclaration(decl)) {
  44. return;
  45. }
  46. if (isCustomProperty(prop)) {
  47. return;
  48. }
  49. if (!shouldCheckPrefixed && postcss.vendor.prefix(prop)) {
  50. return;
  51. }
  52. if (optionsMatches(options, 'ignoreProperties', prop)) {
  53. return;
  54. }
  55. const { selector } = decl.parent;
  56. if (selector && optionsMatches(options, 'ignoreSelectors', selector)) {
  57. return;
  58. }
  59. if (allValidProperties.has(prop.toLowerCase())) {
  60. return;
  61. }
  62. report({
  63. message: messages.rejected(prop),
  64. node: decl,
  65. result,
  66. ruleName,
  67. });
  68. });
  69. };
  70. }
  71. rule.ruleName = ruleName;
  72. rule.messages = messages;
  73. module.exports = rule;