preload-languages.test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Grammar preload set for a file list (#1628).
  3. *
  4. * Path-only detection calls every `.h` file C, but parse-time detection reads
  5. * the source and can reclassify it as C++ or Objective-C. Workers only ever
  6. * receive the grammars named by this set, so a header that turns out to be
  7. * Objective-C in a project with no `.m` file had no parser to go to and the
  8. * file failed outright with `Failed to get parser for language: objc`.
  9. */
  10. import { describe, it, expect } from 'vitest';
  11. import { preloadLanguagesForFiles } from '../src/extraction';
  12. describe('grammar preload set (#1628)', () => {
  13. it('covers both ambiguous readings of a .h file, C++ and Objective-C', () => {
  14. const langs = preloadLanguagesForFiles(['repro.h']);
  15. // Path-only detection says C…
  16. expect(langs).toContain('c');
  17. // …and parse-time detection may say either of these instead.
  18. expect(langs).toContain('cpp');
  19. expect(langs).toContain('objc');
  20. });
  21. it('adds nothing for a project with no C-family headers', () => {
  22. const langs = preloadLanguagesForFiles(['a.ts', 'b.py']);
  23. expect(langs).not.toContain('c');
  24. expect(langs).not.toContain('cpp');
  25. expect(langs).not.toContain('objc');
  26. });
  27. it('does not duplicate a language the files already need', () => {
  28. const langs = preloadLanguagesForFiles(['repro.h', 'seed.m', 'other.cpp']);
  29. expect(langs.filter((l) => l === 'objc')).toHaveLength(1);
  30. expect(langs.filter((l) => l === 'cpp')).toHaveLength(1);
  31. });
  32. it('honors extension overrides when detecting the base set', () => {
  33. const langs = preloadLanguagesForFiles(['weird.frob'], { '.frob': 'python' });
  34. expect(langs).toContain('python');
  35. });
  36. });