deprecation-warnings.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @fileoverview Provide the function that emits deprecation warnings.
  3. * @author Toru Nagashima <http://github.com/mysticatea>
  4. */
  5. //------------------------------------------------------------------------------
  6. // Requirements
  7. //------------------------------------------------------------------------------
  8. import path from "path";
  9. //------------------------------------------------------------------------------
  10. // Private
  11. //------------------------------------------------------------------------------
  12. // Defitions for deprecation warnings.
  13. const deprecationWarningMessages = {
  14. ESLINT_LEGACY_ECMAFEATURES:
  15. "The 'ecmaFeatures' config file property is deprecated and has no effect.",
  16. ESLINT_PERSONAL_CONFIG_LOAD:
  17. "'~/.eslintrc.*' config files have been deprecated. " +
  18. "Please use a config file per project or the '--config' option.",
  19. ESLINT_PERSONAL_CONFIG_SUPPRESS:
  20. "'~/.eslintrc.*' config files have been deprecated. " +
  21. "Please remove it or add 'root:true' to the config files in your " +
  22. "projects in order to avoid loading '~/.eslintrc.*' accidentally."
  23. };
  24. const sourceFileErrorCache = new Set();
  25. /**
  26. * Emits a deprecation warning containing a given filepath. A new deprecation warning is emitted
  27. * for each unique file path, but repeated invocations with the same file path have no effect.
  28. * No warnings are emitted if the `--no-deprecation` or `--no-warnings` Node runtime flags are active.
  29. * @param {string} source The name of the configuration source to report the warning for.
  30. * @param {string} errorCode The warning message to show.
  31. * @returns {void}
  32. */
  33. function emitDeprecationWarning(source, errorCode) {
  34. const cacheKey = JSON.stringify({ source, errorCode });
  35. if (sourceFileErrorCache.has(cacheKey)) {
  36. return;
  37. }
  38. sourceFileErrorCache.add(cacheKey);
  39. const rel = path.relative(process.cwd(), source);
  40. const message = deprecationWarningMessages[errorCode];
  41. process.emitWarning(
  42. `${message} (found in "${rel}")`,
  43. "DeprecationWarning",
  44. errorCode
  45. );
  46. }
  47. //------------------------------------------------------------------------------
  48. // Public Interface
  49. //------------------------------------------------------------------------------
  50. export {
  51. emitDeprecationWarning
  52. };