From 0868d6c88cb303cf56bc5a46122cbaf015d36394 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Mon, 11 Mar 2024 11:49:47 +0100 Subject: [PATCH] add bail test --- packages/example-tada/introspection.ts | 4 ++ .../fixtures/bail.tsx | 40 +++++++++++++++++ test/e2e/unused-fieds.test.ts | 44 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 test/e2e/fixture-project-unused-fields/fixtures/bail.tsx diff --git a/packages/example-tada/introspection.ts b/packages/example-tada/introspection.ts index ceaf865f..af8e4f43 100644 --- a/packages/example-tada/introspection.ts +++ b/packages/example-tada/introspection.ts @@ -435,6 +435,10 @@ const introspection = { { "kind": "SCALAR", "name": "Boolean" + }, + { + "kind": "SCALAR", + "name": "Any" } ], "directives": [] diff --git a/test/e2e/fixture-project-unused-fields/fixtures/bail.tsx b/test/e2e/fixture-project-unused-fields/fixtures/bail.tsx new file mode 100644 index 00000000..834dcd51 --- /dev/null +++ b/test/e2e/fixture-project-unused-fields/fixtures/bail.tsx @@ -0,0 +1,40 @@ +import * as React from 'react'; +import { useQuery } from 'urql'; +import { graphql } from './gql'; +// @ts-expect-error +import { Pokemon } from './fragment'; + +const PokemonQuery = graphql(` + query Po($id: ID!) { + pokemon(id: $id) { + id + fleeRate + ...pokemonFields + attacks { + special { + name + damage + } + } + weight { + minimum + maximum + } + name + __typename + } + } +`); + +const Pokemons = () => { + const [result] = useQuery({ + query: PokemonQuery, + variables: { id: '' } + }); + + const pokemon = React.useMemo(() => result.data?.pokemon, []) + + // @ts-expect-error + return ; +} + diff --git a/test/e2e/unused-fieds.test.ts b/test/e2e/unused-fieds.test.ts index d44160b0..0e222cfe 100644 --- a/test/e2e/unused-fieds.test.ts +++ b/test/e2e/unused-fieds.test.ts @@ -14,6 +14,7 @@ describe('unused fields', () => { 'immediate-destructuring.tsx' ); const outfileDestructuring = path.join(projectPath, 'destructuring.tsx'); + const outfileBail = path.join(projectPath, 'bail.tsx'); const outfileFragmentDestructuring = path.join( projectPath, 'fragment-destructuring.tsx' @@ -30,6 +31,11 @@ describe('unused fields', () => { fileContent: '// empty', scriptKindName: 'TS', } satisfies ts.server.protocol.OpenRequestArgs); + server.sendCommand('open', { + file: outfileBail, + fileContent: '// empty', + scriptKindName: 'TS', + } satisfies ts.server.protocol.OpenRequestArgs); server.sendCommand('open', { file: outfileFragment, fileContent: '// empty', @@ -60,6 +66,13 @@ describe('unused fields', () => { 'utf-8' ), }, + { + file: outfileBail, + fileContent: fs.readFileSync( + path.join(projectPath, 'fixtures/bail.tsx'), + 'utf-8' + ), + }, { file: outfileFragment, fileContent: fs.readFileSync( @@ -111,6 +124,10 @@ describe('unused fields', () => { file: outfileDestructuringFromStart, tmpfile: outfileDestructuringFromStart, } satisfies ts.server.protocol.SavetoRequestArgs); + server.sendCommand('saveto', { + file: outfileBail, + tmpfile: outfileBail, + } satisfies ts.server.protocol.SavetoRequestArgs); }); afterAll(() => { @@ -120,6 +137,7 @@ describe('unused fields', () => { fs.unlinkSync(outfilePropAccess); fs.unlinkSync(outfileFragmentDestructuring); fs.unlinkSync(outfileDestructuringFromStart); + fs.unlinkSync(outfileBail); } catch {} }); @@ -361,4 +379,30 @@ describe('unused fields', () => { ] `); }, 30000); + + it('Bails unused fields when memo func is used', async () => { + const res = server.responses.filter( + resp => + resp.type === 'event' && + resp.event === 'semanticDiag' && + resp.body?.file === outfileBail + ); + expect(res[0].body.diagnostics).toMatchInlineSnapshot(` + [ + { + "category": "error", + "code": 2578, + "end": { + "line": 4, + "offset": 20, + }, + "start": { + "line": 4, + "offset": 1, + }, + "text": "Unused '@ts-expect-error' directive.", + }, + ] + `); + }, 30000); });