From 741bf0a16b0b6809c769eba11c30a51af7092ccf Mon Sep 17 00:00:00 2001 From: Rasmus Schultz Date: Sun, 13 Aug 2023 17:35:41 +0200 Subject: [PATCH] refactor: move core features from `@parser` into `@core` --- src/__tests__/@helpers/index.ts | 4 +-- src/__tests__/core.spec-d.ts | 33 +++++++++++++++++++ src/__tests__/core.spec.ts | 8 +++++ src/__tests__/{parsers => core}/run.spec.ts | 0 .../{parsers => core}/tryRun.spec.ts | 0 src/__tests__/index.spec.ts | 6 +++- src/__tests__/parsers.spec-d.ts | 32 +----------------- src/core.ts | 2 ++ src/{parsers => core}/run.ts | 0 src/{parsers => core}/tryRun.ts | 0 src/parsers.ts | 3 +- tsconfig.json | 2 ++ 12 files changed, 54 insertions(+), 36 deletions(-) create mode 100644 src/__tests__/core.spec-d.ts create mode 100644 src/__tests__/core.spec.ts rename src/__tests__/{parsers => core}/run.spec.ts (100%) rename src/__tests__/{parsers => core}/tryRun.spec.ts (100%) create mode 100644 src/core.ts rename src/{parsers => core}/run.ts (100%) rename src/{parsers => core}/tryRun.ts (100%) diff --git a/src/__tests__/@helpers/index.ts b/src/__tests__/@helpers/index.ts index 5fdb54f..5de382f 100644 --- a/src/__tests__/@helpers/index.ts +++ b/src/__tests__/@helpers/index.ts @@ -66,6 +66,8 @@ export function testSuccess>(input: string, value: should.matchState(actual, expected) } +export const expectedCore = ['run', 'tryRun'] as const + export const expectedCombinators = [ 'attempt', 'chainl', @@ -104,9 +106,7 @@ export const expectedParsers = [ 'oneOf', 'regexp', 'rest', - 'run', 'string', - 'tryRun', 'ustring', 'whitespace', 'whole' diff --git a/src/__tests__/core.spec-d.ts b/src/__tests__/core.spec-d.ts new file mode 100644 index 0000000..c1ce2e1 --- /dev/null +++ b/src/__tests__/core.spec-d.ts @@ -0,0 +1,33 @@ +import * as p from '@parsers' +import { describe, expectTypeOf, it } from '@testing' +import { Result, Success } from '@types' + +describe('run', () => { + const { run } = p + + it('run should have correct inferred signature', () => { + expectTypeOf().returns.toMatchTypeOf<{ with: (input: string) => Result }>() + expectTypeOf>().returns.toMatchTypeOf<{ + with: (input: string) => Result + }>() + expectTypeOf>().returns.not.toMatchTypeOf<{ + with: (input: string) => Result + }>() + }) +}) + +describe('tryRun', () => { + const { tryRun } = p + + it('tryRun should have correct inferred signature', () => { + expectTypeOf().returns.toMatchTypeOf<{ + with: (input: string) => Success + }>() + expectTypeOf>().returns.toMatchTypeOf<{ + with: (input: string) => Success + }>() + expectTypeOf>().returns.not.toMatchTypeOf<{ + with: (input: string) => Success + }>() + }) +}) diff --git a/src/__tests__/core.spec.ts b/src/__tests__/core.spec.ts new file mode 100644 index 0000000..0e31a05 --- /dev/null +++ b/src/__tests__/core.spec.ts @@ -0,0 +1,8 @@ +import * as exposed from '@core' +import { should, expectedCore, describe, it } from '@testing' + +describe('parsers exports', () => { + it('should expose parsers', () => { + should.expose(exposed, ...expectedCore) + }) +}) diff --git a/src/__tests__/parsers/run.spec.ts b/src/__tests__/core/run.spec.ts similarity index 100% rename from src/__tests__/parsers/run.spec.ts rename to src/__tests__/core/run.spec.ts diff --git a/src/__tests__/parsers/tryRun.spec.ts b/src/__tests__/core/tryRun.spec.ts similarity index 100% rename from src/__tests__/parsers/tryRun.spec.ts rename to src/__tests__/core/tryRun.spec.ts diff --git a/src/__tests__/index.spec.ts b/src/__tests__/index.spec.ts index 1f826f0..e7160af 100644 --- a/src/__tests__/index.spec.ts +++ b/src/__tests__/index.spec.ts @@ -1,7 +1,11 @@ import * as exposed from '@lib' -import { should, expectedParsers, expectedCombinators, describe, it } from '@testing' +import { should, expectedCore, expectedParsers, expectedCombinators, describe, it } from '@testing' describe('index exports', () => { + it('should re-export core', () => { + should.expose(exposed, ...expectedCore) + }) + it('should re-export combinators', () => { should.expose(exposed, ...expectedCombinators) }) diff --git a/src/__tests__/parsers.spec-d.ts b/src/__tests__/parsers.spec-d.ts index b3e96d6..c481e79 100644 --- a/src/__tests__/parsers.spec-d.ts +++ b/src/__tests__/parsers.spec-d.ts @@ -1,6 +1,6 @@ import * as p from '@parsers' import { describe, expectTypeOf, it } from '@testing' -import { Parser, SucceedingParser, Result, Success } from '@types' +import { Parser, SucceedingParser } from '@types' type UnknownParser = Parser type NullParser = Parser @@ -154,20 +154,6 @@ describe('rest', () => { }) }) -describe('run', () => { - const { run } = p - - it('run should have correct inferred signature', () => { - expectTypeOf().returns.toMatchTypeOf<{ with: (input: string) => Result }>() - expectTypeOf>().returns.toMatchTypeOf<{ - with: (input: string) => Result - }>() - expectTypeOf>().returns.not.toMatchTypeOf<{ - with: (input: string) => Result - }>() - }) -}) - describe('string', () => { const { string, ustring } = p @@ -184,22 +170,6 @@ describe('string', () => { }) }) -describe('tryRun', () => { - const { tryRun } = p - - it('tryRun should have correct inferred signature', () => { - expectTypeOf().returns.toMatchTypeOf<{ - with: (input: string) => Success - }>() - expectTypeOf>().returns.toMatchTypeOf<{ - with: (input: string) => Success - }>() - expectTypeOf>().returns.not.toMatchTypeOf<{ - with: (input: string) => Success - }>() - }) -}) - describe('whitespace', () => { const { whitespace } = p diff --git a/src/core.ts b/src/core.ts new file mode 100644 index 0000000..2db6579 --- /dev/null +++ b/src/core.ts @@ -0,0 +1,2 @@ +export * from '@core/run' +export * from '@core/tryRun' diff --git a/src/parsers/run.ts b/src/core/run.ts similarity index 100% rename from src/parsers/run.ts rename to src/core/run.ts diff --git a/src/parsers/tryRun.ts b/src/core/tryRun.ts similarity index 100% rename from src/parsers/tryRun.ts rename to src/core/tryRun.ts diff --git a/src/parsers.ts b/src/parsers.ts index aa9613d..b8bb0de 100644 --- a/src/parsers.ts +++ b/src/parsers.ts @@ -1,3 +1,4 @@ +export * from '@core' // TODO keep this here for BC? or move to a dedicated core export? export * from '@parsers/any' export * from '@parsers/defer' export * from '@parsers/eof' @@ -9,7 +10,5 @@ export * from '@parsers/numbers' export * from '@parsers/oneOf' export * from '@parsers/regexp' export * from '@parsers/rest' -export * from '@parsers/run' -export * from '@parsers/tryRun' export * from '@parsers/string' export * from '@parsers/whitespace' diff --git a/tsconfig.json b/tsconfig.json index 92f800a..36c7929 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,6 +13,8 @@ "paths": { "@combinators": ["src/combinators"], "@combinators/*": ["src/combinators/*"], + "@core": ["src/core"], + "@core/*": ["src/core/*"], "@lib": ["src"], "@lib/*": ["src/*"], "@parsers": ["src/parsers"],