Skip to content

Commit 3cfc63c

Browse files
committed
refactor: directly execute tests with NodeJS
NodeJS introduced experimental flags to run TypeScript. This change uses this capability. This allows bypassing the build step for running tests. We have to change all imports of TypeScript files to use the `.ts` extension. Unfortuantely ESbuild is not able to rewrite the extensions. Fortunately, TSC 5.7 is capable of rewritting extensions. Thus this change switches to TSC for transpiling the sources. We still keep ESbuild for bundling CJS and the standalone executable. I took the opprotunity of replacing all tsconfigs with a global one. This allows easy referencing from test files to source files without involving TypeScript project references. Note that safety is preserved because we still use dedicated tsconfigs for building destination files: - `src/tsconfig.json` - `src/tsconfig.bin.json` This allows expressing restrictions. For instance our source files don't depend on NodeJS, while our test files do.
1 parent b357aa9 commit 3cfc63c

23 files changed

+79
-95
lines changed

.github/workflows/ci.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ jobs:
1616
- uses: actions/checkout@v4
1717
- uses: actions/setup-node@v3
1818
with:
19-
node-version: "22.0.0"
19+
node-version: "22.9.0"
2020
cache: npm
2121
- name: Build and test coverage
2222
run: |
2323
npm ci
2424
npm install c8
2525
npm run coverage
26+
- name: Print report
27+
run: |
28+
ls coverage
29+
cat coverage/lcov.info
2630
- name: Coveralls
2731
uses: coverallsapp/github-action@master
2832
with:

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
- uses: actions/checkout@v4
1616
- uses: actions/setup-node@v3
1717
with:
18-
node-version: "20.0.0"
18+
node-version: "22.9.0"
1919
cache: npm
2020
- name: Build
2121
run: |

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
},
2828
"type": "module",
2929
"bin": {
30-
"bare": "dist/bin/cli.js"
30+
"bare": "dist/bin/bare.js"
3131
},
3232
"exports": {
3333
"./package.json": "./package.json",
@@ -53,13 +53,14 @@
5353
],
5454
"scripts": {
5555
"build": "sh ./scripts/build.sh",
56+
"check": "tsc --build && biome ci --error-on-warnings .",
5657
"clean": "rm -rf dist coverage",
5758
"coverage": "c8 --reporter=lcovonly npm test",
5859
"format": "biome format --write .",
5960
"lint": "biome lint .",
6061
"prepare": "validate-commit-msg",
61-
"prepublishOnly": "npm run clean && npm test",
62-
"test": "sh ./scripts/test.sh",
62+
"prepublishOnly": "npm run clean && npm run build && npm test",
63+
"test": "node --test --experimental-strip-types && npm run check",
6364
"version": "sh ./scripts/version.sh"
6465
},
6566
"devDependencies": {

scripts/build.sh

+3-6
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,13 @@ set -eu
55
# https://kangax.github.io/compat-table/es2016plus
66
TARGET='node20.10.0'
77

8-
# build .d.ts
9-
tsc --build src/
8+
# build ESM and .d.ts
9+
npx tsc --build src/tsconfig.bin.json
1010

1111
cp -f dist/index.d.ts dist/index.d.cts
1212

13-
# build ESM
14-
esbuild 'src/**/*.ts' --target=$TARGET --outdir=dist --log-level=warning
15-
1613
# build CommonJS (fallback)
1714
esbuild src/index.ts --bundle --target=$TARGET --platform=node > dist/index.cjs
1815

1916
# build standalone cli program
20-
esbuild dist/bin/cli.js --bundle --target=$TARGET --minify --keep-names --platform=node > dist/bin/bare
17+
esbuild dist/bin/bare.js --bundle --target=$TARGET --minify --keep-names --platform=node > dist/bin/bare

scripts/generate-tests-corpus.js renamed to scripts/generate-tests-corpus.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import * as fs from "node:fs"
44
import * as path from "node:path"
5-
import { Config, configure, parse, transform } from "@bare-ts/tools"
5+
import { Config, configure, parse, transform } from "../src/index.ts"
66

77
const CORPUS_DIR = "./tests-corpus"
88

@@ -39,8 +39,7 @@ for (let category of fs.readdirSync(CORPUS_DIR)) {
3939
completedConfig,
4040
)
4141
fs.writeFileSync(astPath, JSON.stringify(ast, null, 2))
42-
let out
43-
out = transform(content, { ...config, schema, generator: "ts" })
42+
let out = transform(content, { ...config, schema, generator: "ts" })
4443
fs.writeFileSync(tsPath, out)
4544
out = transform(content, { ...config, schema, generator: "js" })
4645
fs.writeFileSync(jsPath, out)

scripts/test.sh

-12
This file was deleted.

src/ast/bare-configure.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Copyright (c) 2022 Victorien Elvinger
22
//! Licensed under the MIT License (https://mit-license.org/)
33

4-
import type { Config } from "../core/config.js"
5-
import * as ast from "./bare-ast.js"
4+
import type { Config } from "../core/config.ts"
5+
import * as ast from "./bare-ast.ts"
66

77
export function configure(schema: ast.Ast, config: Config): ast.Ast {
88
const c: Configurator = {

src/ast/bare-normalization.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Copyright (c) 2022 Victorien Elvinger
22
//! Licensed under the MIT License (https://mit-license.org/)
33

4-
import * as ast from "./bare-ast.js"
4+
import * as ast from "./bare-ast.ts"
55

66
export function normalize(schema: ast.Ast): ast.Ast {
77
const n: Context = { defs: [], dedup: new Map(), aliasCount: 0 }

src/ast/bare-semantic-checker.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Copyright (c) 2022 Victorien Elvinger
22
//! Licensed under the MIT License (https://mit-license.org/)
33

4-
import { CompilerError } from "../core/compiler-error.js"
5-
import type { Config } from "../core/config.js"
4+
import { CompilerError } from "../core/compiler-error.ts"
5+
import type { Config } from "../core/config.ts"
66
import {
77
CAMEL_CASE_RE,
88
CONSTANT_CASE_RE,
99
PASCAL_CASE_RE,
10-
} from "../utils/formatting.js"
11-
import * as ast from "./bare-ast.js"
10+
} from "../utils/formatting.ts"
11+
import * as ast from "./bare-ast.ts"
1212

1313
export function checkSemantic(schema: ast.Ast, config: Config): ast.Ast {
1414
if (schema.defs.length === 0) {

src/bin/cli.ts renamed to src/bin/bare.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as fs from "node:fs"
77
import * as process from "node:process"
88
import * as util from "node:util"
99
import packageVersion from "../../VERSION.json" with { type: "json" }
10-
import { CompilerError, Config, transform } from "../index.js"
10+
import { CompilerError, Config, transform } from "../index.ts"
1111

1212
const HELP_TEXT = `
1313
Usage: bare [options] [schema]

src/generator/bare-ast-utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Copyright (c) 2022 Victorien Elvinger
22
//! Licensed under the MIT License (https://mit-license.org/)
33

4-
import * as ast from "../ast/bare-ast.js"
5-
import { CompilerError } from "../core/compiler-error.js"
4+
import * as ast from "../ast/bare-ast.ts"
5+
import { CompilerError } from "../core/compiler-error.ts"
66

77
// This file is separated from ast folder because these utils are used for
88
// facilitating code generation.

src/generator/bare-generator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Copyright (c) 2022 Victorien Elvinger
22
//! Licensed under the MIT License (https://mit-license.org/)
33

4-
import type * as ast from "../ast/bare-ast.js"
5-
import { dent, toConstantCase } from "../utils/formatting.js"
4+
import type * as ast from "../ast/bare-ast.ts"
5+
import { dent, toConstantCase } from "../utils/formatting.ts"
66

77
export function generateBare(schema: ast.Ast): string {
88
let result = ""

src/generator/js-generator.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
//! Copyright (c) 2022 Victorien Elvinger
22
//! Licensed under the MIT License (https://mit-license.org/)
33

4-
import * as ast from "../ast/bare-ast.js"
5-
import type { Config } from "../core/config.js"
4+
import * as ast from "../ast/bare-ast.ts"
5+
import type { Config } from "../core/config.ts"
66
import {
77
capitalize,
88
dent,
99
jsDoc,
1010
jsRpr,
1111
softSpace,
12-
} from "../utils/formatting.js"
13-
import * as utils from "./bare-ast-utils.js"
12+
} from "../utils/formatting.ts"
13+
import * as utils from "./bare-ast-utils.ts"
1414

1515
export function generate(schema: ast.Ast, config: Config): string {
1616
const g: Gen = { config, symbols: ast.symbols(schema) }

src/index.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
//! Copyright (c) 2022 Victorien Elvinger
22
//! Licensed under the MIT License (https://mit-license.org/)
33

4-
import { configure } from "./ast/bare-configure.js"
5-
import { normalize } from "./ast/bare-normalization.js"
6-
import { checkSemantic } from "./ast/bare-semantic-checker.js"
7-
import { Config } from "./core/config.js"
8-
import { generateBare } from "./generator/bare-generator.js"
9-
import { generate } from "./generator/js-generator.js"
10-
import { parse } from "./parser/bare-parser.js"
4+
import { configure } from "./ast/bare-configure.ts"
5+
import { normalize } from "./ast/bare-normalization.ts"
6+
import { checkSemantic } from "./ast/bare-semantic-checker.ts"
7+
import { Config } from "./core/config.ts"
8+
import { generateBare } from "./generator/bare-generator.ts"
9+
import { generate } from "./generator/js-generator.ts"
10+
import { parse } from "./parser/bare-parser.ts"
1111

12-
export * from "./ast/bare-ast.js"
13-
export * from "./ast/bare-configure.js"
14-
export * from "./ast/bare-normalization.js"
15-
export * from "./core/compiler-error.js"
16-
export * from "./core/config.js"
17-
export * from "./generator/js-generator.js"
18-
export * from "./parser/bare-parser.js"
12+
export * from "./ast/bare-ast.ts"
13+
export * from "./ast/bare-configure.ts"
14+
export * from "./ast/bare-normalization.ts"
15+
export * from "./core/compiler-error.ts"
16+
export * from "./core/config.ts"
17+
export * from "./generator/js-generator.ts"
18+
export * from "./parser/bare-parser.ts"
1919

2020
/**
2121
* Turn the schema `content` into a target language, taking `conf` into account.

src/parser/bare-lexer.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import * as assert from "node:assert/strict"
55
import { test } from "node:test"
6-
import * as lex from "./bare-lexer.js"
6+
import * as lex from "./bare-lexer.ts"
77

88
const SAMPLE = `
99
const C = { # struct

src/parser/bare-lexer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Copyright (c) 2022 Victorien Elvinger
22
//! Licensed under the MIT License (https://mit-license.org/)
33

4-
import { CompilerError } from "../core/compiler-error.js"
4+
import { CompilerError } from "../core/compiler-error.ts"
55

66
const WHITE_SPACE_PATTERN = /\s/
77
const PUNCTUATION_PATTERN = /[{}[\]()<>=|:,;.!?~+\\/$@#-]/

src/parser/bare-parser.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//! Copyright (c) 2022 Victorien Elvinger
22
//! Licensed under the MIT License (https://mit-license.org/)
33

4-
import * as ast from "../ast/bare-ast.js"
5-
import { CompilerError } from "../core/compiler-error.js"
6-
import type { Config } from "../core/config.js"
4+
import * as ast from "../ast/bare-ast.ts"
5+
import { CompilerError } from "../core/compiler-error.ts"
6+
import type { Config } from "../core/config.ts"
77
import {
88
ALL_CASE_RE,
99
CONSTANT_CASE_RE,
1010
toPascalCase,
11-
} from "../utils/formatting.js"
12-
import * as lexer from "./bare-lexer.js"
11+
} from "../utils/formatting.ts"
12+
import * as lexer from "./bare-lexer.ts"
1313

1414
export function parse(content: string, config: Config): ast.Ast {
1515
const p: Parser = { config, lex: lexer.create(content) }

src/tsconfig-test.json

-18
This file was deleted.

src/tsconfig.bin.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": false,
5+
"rewriteRelativeImportExtensions": true,
6+
"rootDir": ".",
7+
"outDir": "../dist"
8+
},
9+
"include": [
10+
"./bin/*.ts"
11+
],
12+
"references": [
13+
{
14+
"path": "."
15+
}
16+
]
17+
}

src/tsconfig.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
{
2-
"extends": "../tsconfig-base.json",
2+
"extends": "../tsconfig.json",
33
"compilerOptions": {
44
"composite": true,
5-
"checkJs": false,
65
"isolatedDeclarations": true,
7-
"emitDeclarationOnly": true,
86
"noEmit": false,
7+
"rewriteRelativeImportExtensions": true,
8+
"types": [],
9+
"rootDir": ".",
910
"outDir": "../dist"
1011
},
1112
"exclude": [
12-
"./bin/cli.ts",
13+
"./bin/**",
1314
"./**/*.test.ts"
1415
]
1516
}

tests-corpus/index.test.js renamed to tests-corpus/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
configure,
1212
parse,
1313
transform,
14-
} from "@bare-ts/tools"
14+
} from "../src/index.ts"
1515

1616
const CORPUS_DIR = "./tests-corpus"
1717
const INVALID_BARE_DIR = `${CORPUS_DIR}/invalid-bare-schema`

tests-corpus/tsconfig.json

-6
This file was deleted.

tsconfig-base.json renamed to tsconfig.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
],
77
"module": "NodeNext",
88
"target": "ES2022",
9+
"types": [
10+
"node"
11+
],
912

1013
"noEmit": true,
1114

12-
"types": [],
13-
15+
"allowImportingTsExtensions": true,
1416
"esModuleInterop": true,
1517
"isolatedModules": true,
1618
"resolveJsonModule": true,
1719
"skipLibCheck": true,
1820
"verbatimModuleSyntax": true,
1921

2022
"allowUnreachableCode": false,
21-
"checkJs": true,
2223
"noFallthroughCasesInSwitch": true,
2324
"noImplicitOverride": true,
2425
"noImplicitReturns": true,

0 commit comments

Comments
 (0)