lintSource.cjs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const path = require('node:path');
  5. const postcss = require('postcss');
  6. const descriptionlessDisables = require('./descriptionlessDisables.cjs');
  7. const getConfigForFile = require('./getConfigForFile.cjs');
  8. const getPostcssResult = require('./getPostcssResult.cjs');
  9. const invalidScopeDisables = require('./invalidScopeDisables.cjs');
  10. const isPathIgnored = require('./isPathIgnored.cjs');
  11. const isPathNotFoundError = require('./utils/isPathNotFoundError.cjs');
  12. const lintPostcssResult = require('./lintPostcssResult.cjs');
  13. const needlessDisables = require('./needlessDisables.cjs');
  14. const reportDisables = require('./reportDisables.cjs');
  15. const unscopedDisables = require('./unscopedDisables.cjs');
  16. /** @import {Result} from 'postcss' */
  17. /** @import {GetLintSourceOptions as Options, InternalApi as StylelintInternalApi, PostcssResult, StylelintPostcssResult} from 'stylelint' */
  18. /**
  19. * Run stylelint on a PostCSS Result, either one that is provided
  20. * or one that we create
  21. * @param {StylelintInternalApi} stylelint
  22. * @param {Options} options
  23. * @returns {Promise<PostcssResult>}
  24. */
  25. async function lintSource(stylelint, options = {}) {
  26. if (!options.filePath && options.code === undefined && !options.existingPostcssResult) {
  27. return Promise.reject(new Error('You must provide filePath, code, or existingPostcssResult'));
  28. }
  29. const isCodeNotFile = options.code !== undefined;
  30. const inputFilePath = isCodeNotFile ? options.codeFilename : options.filePath;
  31. if (inputFilePath !== undefined && !path.isAbsolute(inputFilePath)) {
  32. if (isCodeNotFile) {
  33. return Promise.reject(new Error('codeFilename must be an absolute path'));
  34. }
  35. return Promise.reject(new Error('filePath must be an absolute path'));
  36. }
  37. const isIgnored = await isPathIgnored(stylelint, inputFilePath).catch((err) => {
  38. if (isCodeNotFile && isPathNotFoundError(err)) return false;
  39. throw err;
  40. });
  41. if (isIgnored) {
  42. return createEmptyPostcssResult(inputFilePath, options.existingPostcssResult);
  43. }
  44. const configForFile = await getConfigForFile({
  45. stylelint,
  46. searchPath: inputFilePath,
  47. filePath: inputFilePath,
  48. });
  49. if (!configForFile) {
  50. return Promise.reject(new Error('Config file not found'));
  51. }
  52. const config = configForFile.config;
  53. const existingPostcssResult = options.existingPostcssResult;
  54. if (options.cache) {
  55. stylelint._fileCache.calcHashOfConfig(config);
  56. if (options.filePath && !stylelint._fileCache.hasFileChanged(options.filePath)) {
  57. return createEmptyPostcssResult(inputFilePath, existingPostcssResult);
  58. }
  59. }
  60. const postcssResult =
  61. existingPostcssResult ||
  62. (await getPostcssResult(stylelint, {
  63. code: options.code,
  64. codeFilename: options.codeFilename,
  65. filePath: inputFilePath,
  66. customSyntax: config.customSyntax,
  67. }));
  68. const stylelintPostcssResult = Object.assign(postcssResult, {
  69. stylelint: {
  70. ruleSeverities: {},
  71. customMessages: {},
  72. customUrls: {},
  73. ruleMetadata: {},
  74. fixersData: {},
  75. rangesOfComputedEditInfos: [],
  76. disabledRanges: {},
  77. },
  78. });
  79. await lintPostcssResult(stylelint._options, stylelintPostcssResult, config);
  80. reportDisables(stylelintPostcssResult);
  81. needlessDisables(stylelintPostcssResult);
  82. invalidScopeDisables(stylelintPostcssResult);
  83. descriptionlessDisables(stylelintPostcssResult);
  84. unscopedDisables(stylelintPostcssResult);
  85. return stylelintPostcssResult;
  86. }
  87. /**
  88. * @returns {StylelintPostcssResult}
  89. */
  90. function createEmptyStylelintPostcssResult() {
  91. return {
  92. ruleSeverities: {},
  93. customMessages: {},
  94. customUrls: {},
  95. ruleMetadata: {},
  96. fixersData: {},
  97. rangesOfComputedEditInfos: [],
  98. disabledRanges: {},
  99. ignored: true,
  100. stylelintError: false,
  101. stylelintWarning: false,
  102. };
  103. }
  104. /**
  105. * @param {string | undefined} filePath
  106. * @param {Options['existingPostcssResult']} existingPostcssResult
  107. * @returns {PostcssResult}
  108. */
  109. function createEmptyPostcssResult(filePath, existingPostcssResult) {
  110. return Object.assign(existingPostcssResult ?? postcss().process('', { from: filePath }).sync(), {
  111. stylelint: createEmptyStylelintPostcssResult(),
  112. });
  113. }
  114. module.exports = lintSource;