Ohm-Management - Projektarbeit B-ME
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.

source-code-utils.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. globUtils = require("./glob-utils"),
  11. baseDefaultOptions = require("../../conf/default-cli-options");
  12. const debug = require("debug")("eslint:source-code-utils");
  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 filenames = globUtils.listFilesToProcess(globPatternsList, options)
  67. .filter(fileInfo => !fileInfo.ignored)
  68. .reduce((files, fileInfo) => files.concat(fileInfo.filename), []);
  69. if (filenames.length === 0) {
  70. debug(`Did not find any files matching pattern(s): ${globPatternsList}`);
  71. }
  72. filenames.forEach(filename => {
  73. const sourceCode = getSourceCodeOfFile(filename, options);
  74. if (sourceCode) {
  75. debug("got sourceCode of", filename);
  76. sourceCodes[filename] = sourceCode;
  77. }
  78. if (callback) {
  79. callback(filenames.length); // eslint-disable-line callback-return
  80. }
  81. });
  82. return sourceCodes;
  83. }
  84. module.exports = {
  85. getSourceCodeOfFiles
  86. };