spec-checker.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // This is functionally identical to the known words checker except that it
  2. // is intended to be immutable while running tests. This means the constructor
  3. // adds the words instead of calling `add` directly.
  4. class SpecChecker {
  5. static initClass() {
  6. this.prototype.spelling = null;
  7. this.prototype.checker = null;
  8. }
  9. constructor(id, isNegative, knownWords) {
  10. // Set up the spelling manager we'll be using.
  11. this.id = id;
  12. this.isNegative = isNegative;
  13. const spellingManager = require('spelling-manager');
  14. this.spelling = new spellingManager.TokenSpellingManager();
  15. this.checker = new spellingManager.BufferSpellingChecker(this.spelling);
  16. // Set our known words.
  17. this.setKnownWords(knownWords);
  18. }
  19. deactivate() {}
  20. getId() {
  21. return 'spell-check:spec:' + this.id;
  22. }
  23. getName() {
  24. return 'Spec Checker';
  25. }
  26. getPriority() {
  27. return 10;
  28. }
  29. isEnabled() {
  30. return true;
  31. }
  32. getStatus() {
  33. return 'Working correctly.';
  34. }
  35. providesSpelling(args) {
  36. return true;
  37. }
  38. providesSuggestions(args) {
  39. return false;
  40. }
  41. providesAdding(args) {
  42. return false;
  43. }
  44. check(args, text) {
  45. const ranges = [];
  46. const checked = this.checker.check(text);
  47. for (let token of checked) {
  48. if (token.status === 1) {
  49. ranges.push({ start: token.start, end: token.end });
  50. }
  51. }
  52. if (this.isNegative) {
  53. return { incorrect: ranges };
  54. } else {
  55. return { correct: ranges };
  56. }
  57. }
  58. setKnownWords(knownWords) {
  59. // Clear out the old list.
  60. this.spelling.sensitive = {};
  61. this.spelling.insensitive = {};
  62. // Add the new ones into the list.
  63. if (knownWords) {
  64. return knownWords.map((ignore) => this.spelling.add(ignore));
  65. }
  66. }
  67. }
  68. SpecChecker.initClass();
  69. module.exports = SpecChecker;