项目原始demo,不改动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

преди 4 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @fileoverview Tools for obtaining SourceCode objects.
  3. * @author Ian VanSchooten
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const CLIEngine = require("../cli-engine"),
  10. globUtil = require("./glob-util"),
  11. baseDefaultOptions = require("../../conf/default-cli-options");
  12. const debug = require("debug")("eslint:source-code-util");
  13. //------------------------------------------------------------------------------
  14. // Helpers
  15. //------------------------------------------------------------------------------
  16. /**
  17. * Get the SourceCode object for a single file
  18. * @param {string} filename The fully resolved filename to get SourceCode from.
  19. * @param {Object} options A CLIEngine options object.
  20. * @returns {Array} Array of the SourceCode object representing the file
  21. * and fatal error message.
  22. */
  23. function getSourceCodeOfFile(filename, options) {
  24. debug("getting sourceCode of", filename);
  25. const opts = Object.assign({}, options, { rules: {} });
  26. const cli = new CLIEngine(opts);
  27. const results = cli.executeOnFiles([filename]);
  28. if (results && results.results[0] && results.results[0].messages[0] && results.results[0].messages[0].fatal) {
  29. const msg = results.results[0].messages[0];
  30. throw new Error(`(${filename}:${msg.line}:${msg.column}) ${msg.message}`);
  31. }
  32. const sourceCode = cli.linter.getSourceCode();
  33. return sourceCode;
  34. }
  35. //------------------------------------------------------------------------------
  36. // Public Interface
  37. //------------------------------------------------------------------------------
  38. /**
  39. * This callback is used to measure execution status in a progress bar
  40. * @callback progressCallback
  41. * @param {number} The total number of times the callback will be called.
  42. */
  43. /**
  44. * Gets the SourceCode of a single file, or set of files.
  45. * @param {string[]|string} patterns A filename, directory name, or glob, or an array of them
  46. * @param {Object} [providedOptions] A CLIEngine options object. If not provided, the default cli options will be used.
  47. * @param {progressCallback} [providedCallback] Callback for reporting execution status
  48. * @returns {Object} The SourceCode of all processed files.
  49. */
  50. function getSourceCodeOfFiles(patterns, providedOptions, providedCallback) {
  51. const sourceCodes = {};
  52. const globPatternsList = typeof patterns === "string" ? [patterns] : patterns;
  53. let options, callback;
  54. const defaultOptions = Object.assign({}, baseDefaultOptions, { cwd: process.cwd() });
  55. if (typeof providedOptions === "undefined") {
  56. options = defaultOptions;
  57. callback = null;
  58. } else if (typeof providedOptions === "function") {
  59. callback = providedOptions;
  60. options = defaultOptions;
  61. } else if (typeof providedOptions === "object") {
  62. options = Object.assign({}, defaultOptions, providedOptions);
  63. callback = providedCallback;
  64. }
  65. debug("constructed options:", options);
  66. const resolvedPatterns = globUtil.resolveFileGlobPatterns(globPatternsList, options);
  67. const filenames = globUtil.listFilesToProcess(resolvedPatterns, options)
  68. .filter(fileInfo => !fileInfo.ignored)
  69. .reduce((files, fileInfo) => files.concat(fileInfo.filename), []);
  70. if (filenames.length === 0) {
  71. debug(`Did not find any files matching pattern(s): ${resolvedPatterns}`);
  72. }
  73. filenames.forEach(filename => {
  74. const sourceCode = getSourceCodeOfFile(filename, options);
  75. if (sourceCode) {
  76. debug("got sourceCode of", filename);
  77. sourceCodes[filename] = sourceCode;
  78. }
  79. if (callback) {
  80. callback(filenames.length); // eslint-disable-line callback-return
  81. }
  82. });
  83. return sourceCodes;
  84. }
  85. module.exports = {
  86. getSourceCodeOfFiles
  87. };