index.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { parse } from "@babel/core";
  2. import syntaxDecorators from "../lib";
  3. function makeParser(code, options) {
  4. return () =>
  5. parse(code, {
  6. babelrc: false,
  7. configFile: false,
  8. plugins: [[syntaxDecorators, options]],
  9. });
  10. }
  11. describe("'legacy' option", function() {
  12. test("must be boolean", function() {
  13. expect(makeParser("", { legacy: "legacy" })).toThrow();
  14. });
  15. test.skip("'legacy': false", function() {
  16. expect(makeParser("({ @dec fn() {} })", { legacy: false })).toThrow();
  17. });
  18. test("'legacy': true", function() {
  19. expect(makeParser("({ @dec fn() {} })", { legacy: true })).not.toThrow();
  20. });
  21. test.skip("defaults to 'false'", function() {
  22. expect(makeParser("({ @dec fn() {} })", {})).toThrow();
  23. });
  24. test("it must be true", function() {
  25. expect(makeParser("", { legacy: false })).toThrow();
  26. });
  27. });
  28. describe("'decoratorsBeforeExport' option", function() {
  29. test.skip("must be boolean", function() {
  30. expect(makeParser("", { decoratorsBeforeExport: "before" })).toThrow();
  31. });
  32. test("is incompatible with legacy", function() {
  33. expect(
  34. makeParser("", { decoratorsBeforeExport: false, legacy: true }),
  35. ).toThrow();
  36. });
  37. const BEFORE = "@dec export class Foo {}";
  38. const AFTER = "export @dec class Foo {}";
  39. // These are skipped
  40. run(BEFORE, undefined, true);
  41. run(AFTER, undefined, false);
  42. run(BEFORE, true, false);
  43. run(AFTER, true, true);
  44. run(BEFORE, false, true);
  45. run(AFTER, false, false);
  46. function run(code, before, throws) {
  47. const name =
  48. (before === undefined ? "default" : before) +
  49. " - decorators " +
  50. (code === BEFORE ? "before" : "after") +
  51. "export";
  52. test.skip(name, function() {
  53. const expectTheParser = expect(
  54. makeParser(code, { decoratorsBeforeExport: before }),
  55. );
  56. throws ? expectTheParser.toThrow() : expectTheParser.not.toThrow();
  57. });
  58. }
  59. });