lintSource.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. 'use strict';
  2. const lintPostcssResult = require('./lintPostcssResult');
  3. const path = require('path');
  4. /** @typedef {import('stylelint').StylelintInternalApi} StylelintInternalApi */
  5. /** @typedef {import('stylelint').GetLintSourceOptions} Options */
  6. /** @typedef {import('postcss').Result} Result */
  7. /** @typedef {import('stylelint').PostcssResult} PostcssResult */
  8. /** @typedef {import('stylelint').StylelintPostcssResult} StylelintPostcssResult */
  9. /**
  10. * Run stylelint on a PostCSS Result, either one that is provided
  11. * or one that we create
  12. * @param {StylelintInternalApi} stylelint
  13. * @param {Options} options
  14. * @returns {Promise<PostcssResult>}
  15. */
  16. module.exports = function lintSource(stylelint, options = {}) {
  17. if (!options.filePath && options.code === undefined && !options.existingPostcssResult) {
  18. return Promise.reject(new Error('You must provide filePath, code, or existingPostcssResult'));
  19. }
  20. const isCodeNotFile = options.code !== undefined;
  21. const inputFilePath = isCodeNotFile ? options.codeFilename : options.filePath;
  22. if (inputFilePath !== undefined && !path.isAbsolute(inputFilePath)) {
  23. if (isCodeNotFile) {
  24. return Promise.reject(new Error('codeFilename must be an absolute path'));
  25. }
  26. return Promise.reject(new Error('filePath must be an absolute path'));
  27. }
  28. const getIsIgnored = stylelint.isPathIgnored(inputFilePath).catch((err) => {
  29. if (isCodeNotFile && err.code === 'ENOENT') return false;
  30. throw err;
  31. });
  32. return getIsIgnored.then((isIgnored) => {
  33. if (isIgnored) {
  34. /** @type {PostcssResult} */
  35. let postcssResult;
  36. if (options.existingPostcssResult) {
  37. postcssResult = Object.assign(options.existingPostcssResult, {
  38. stylelint: createEmptyStylelintPostcssResult(),
  39. });
  40. } else {
  41. postcssResult = createEmptyPostcssResult(inputFilePath);
  42. }
  43. return postcssResult;
  44. }
  45. const configSearchPath = stylelint._options.configFile || inputFilePath;
  46. const getConfig = stylelint.getConfigForFile(configSearchPath).catch((err) => {
  47. if (isCodeNotFile && err.code === 'ENOENT') return stylelint.getConfigForFile(process.cwd());
  48. throw err;
  49. });
  50. return getConfig.then((result) => {
  51. if (!result) {
  52. throw new Error('Config file not found');
  53. }
  54. const config = result.config;
  55. const existingPostcssResult = options.existingPostcssResult;
  56. const stylelintResult = {
  57. ruleSeverities: {},
  58. customMessages: {},
  59. disabledRanges: {},
  60. };
  61. if (existingPostcssResult) {
  62. const stylelintPostcssResult = Object.assign(existingPostcssResult, {
  63. stylelint: stylelintResult,
  64. });
  65. return lintPostcssResult(stylelint._options, stylelintPostcssResult, config).then(
  66. () => stylelintPostcssResult,
  67. );
  68. }
  69. return stylelint
  70. ._getPostcssResult({
  71. code: options.code,
  72. codeFilename: options.codeFilename,
  73. filePath: inputFilePath,
  74. codeProcessors: config.codeProcessors,
  75. })
  76. .then((postcssResult) => {
  77. const stylelintPostcssResult = Object.assign(postcssResult, {
  78. stylelint: stylelintResult,
  79. });
  80. return lintPostcssResult(stylelint._options, stylelintPostcssResult, config).then(
  81. () => stylelintPostcssResult,
  82. );
  83. });
  84. });
  85. });
  86. };
  87. /**
  88. * @returns {StylelintPostcssResult}
  89. */
  90. function createEmptyStylelintPostcssResult() {
  91. return {
  92. ruleSeverities: {},
  93. customMessages: {},
  94. disabledRanges: {},
  95. ignored: true,
  96. stylelintError: false,
  97. };
  98. }
  99. /**
  100. * @param {string} [filePath]
  101. * @returns {PostcssResult}
  102. */
  103. function createEmptyPostcssResult(filePath) {
  104. return {
  105. root: {
  106. source: {
  107. input: { file: filePath },
  108. },
  109. },
  110. messages: [],
  111. opts: undefined,
  112. stylelint: createEmptyStylelintPostcssResult(),
  113. warn: () => {},
  114. };
  115. }