JSMonk / hegel

An advanced static type checker
https://hegel.js.org
MIT License
2.09k stars 59 forks source link

Add @hegel-expect-error comment #138

Open JSMonk opened 4 years ago

JSMonk commented 4 years ago

Add comment which notify, that error at the line is expected and show error if the error will be not presented at the line

thecotne commented 4 years ago

i am thinking to make writing tests much simpler by just having test project that should type check

for example instead of this

test("Issue #113: Rest arguments don't work with generics 01", async () => {
  const sourceAST = prepareAST(`
    function a<T>(...items: Array<T>) {
      return items;
    }

    const arr01: Array<string> = a('str01', 'str03')
    const arr02: Array<number> = a(2, 1, 3)
    const arr03: Array<boolean> = a(false, true, false, true)
  `);
  const [, errors] = await createTypeGraph(
    [sourceAST],
    getModuleAST,
    false,
    mixTypeDefinitions()
  );

  expect(errors.length).toBe(0);
});

we would have this

function a<T>(...items: Array<T>) {
  return items;
}

const arr01: Array<string> = a('str01', 'str03')
const arr02: Array<number> = a(2, 1, 3)
const arr03: Array<boolean> = a(false, true, false, true)

and instead of this

test("Creating global module variable with any type", async () => {
  const sourceAST = prepareAST(`
    const a: any = 2;
  `);
  const [, errors] = await createTypeGraph([sourceAST]);
  expect(errors.length).toBe(1);
  const [error] = errors;
  expect(error).toBeInstanceOf(HegelError);
  expect(error.message).toEqual(
    'There is no "any" type in Hegel. Use "unknown" instead.'
  );
  expect(error.loc).toEqual({
    start: { line: 2, column: 15 },
    end: { line: 2, column: 18 }
  });
});

we would have this

// @hegel-expect-error
const a: any = 2;

also we can save error messages from @hegel-expect-error to jest snapshot and make sure that error messages don't change unnoticed much like how flow is tested but i think it can be improved and made more simple to use