Skip to content

Commit

Permalink
refactor: directly execute tests with NodeJS
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Conaclos committed Jan 25, 2025
1 parent b357aa9 commit c0aa60d
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 95 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: "22.0.0"
node-version: "22.7.0"
cache: npm
- name: Build and test coverage
run: |
npm ci
npm install c8
npm run coverage
- name: Print report
run: |
ls coverage
cat coverage/lcov.info
- name: Coveralls
uses: coverallsapp/github-action@master
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: "20.0.0"
node-version: "22.7.0"
cache: npm
- name: Build
run: |
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"type": "module",
"bin": {
"bare": "dist/bin/cli.js"
"bare": "dist/bin/bare.js"
},
"exports": {
"./package.json": "./package.json",
Expand All @@ -53,13 +53,14 @@
],
"scripts": {
"build": "sh ./scripts/build.sh",
"check": "tsc --build && biome ci --error-on-warnings .",
"clean": "rm -rf dist coverage",
"coverage": "c8 --reporter=lcovonly npm test",
"format": "biome format --write .",
"lint": "biome lint .",
"prepare": "validate-commit-msg",
"prepublishOnly": "npm run clean && npm test",
"test": "sh ./scripts/test.sh",
"prepublishOnly": "npm run clean && npm run build && npm test",
"test": "node --test --experimental-strip-types && npm run check",
"version": "sh ./scripts/version.sh"
},
"devDependencies": {
Expand Down
9 changes: 3 additions & 6 deletions scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ set -eu
# https://kangax.github.io/compat-table/es2016plus
TARGET='node20.10.0'

# build .d.ts
tsc --build src/
# build ESM and .d.ts
npx tsc --build src/tsconfig.bin.json

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

# build ESM
esbuild 'src/**/*.ts' --target=$TARGET --outdir=dist --log-level=warning

# build CommonJS (fallback)
esbuild src/index.ts --bundle --target=$TARGET --platform=node > dist/index.cjs

# build standalone cli program
esbuild dist/bin/cli.js --bundle --target=$TARGET --minify --keep-names --platform=node > dist/bin/bare
esbuild dist/bin/bare.js --bundle --target=$TARGET --minify --keep-names --platform=node > dist/bin/bare
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

const CORPUS_DIR = "./tests-corpus"

Expand Down Expand Up @@ -39,8 +39,7 @@ for (let category of fs.readdirSync(CORPUS_DIR)) {
completedConfig,
)
fs.writeFileSync(astPath, JSON.stringify(ast, null, 2))
let out
out = transform(content, { ...config, schema, generator: "ts" })
let out = transform(content, { ...config, schema, generator: "ts" })
fs.writeFileSync(tsPath, out)
out = transform(content, { ...config, schema, generator: "js" })
fs.writeFileSync(jsPath, out)
Expand Down
12 changes: 0 additions & 12 deletions scripts/test.sh

This file was deleted.

4 changes: 2 additions & 2 deletions src/ast/bare-configure.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Copyright (c) 2022 Victorien Elvinger
//! Licensed under the MIT License (https://mit-license.org/)

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

export function configure(schema: ast.Ast, config: Config): ast.Ast {
const c: Configurator = {
Expand Down
2 changes: 1 addition & 1 deletion src/ast/bare-normalization.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Copyright (c) 2022 Victorien Elvinger
//! Licensed under the MIT License (https://mit-license.org/)

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

export function normalize(schema: ast.Ast): ast.Ast {
const n: Context = { defs: [], dedup: new Map(), aliasCount: 0 }
Expand Down
8 changes: 4 additions & 4 deletions src/ast/bare-semantic-checker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Copyright (c) 2022 Victorien Elvinger
//! Licensed under the MIT License (https://mit-license.org/)

import { CompilerError } from "../core/compiler-error.js"
import type { Config } from "../core/config.js"
import { CompilerError } from "../core/compiler-error.ts"
import type { Config } from "../core/config.ts"
import {
CAMEL_CASE_RE,
CONSTANT_CASE_RE,
PASCAL_CASE_RE,
} from "../utils/formatting.js"
import * as ast from "./bare-ast.js"
} from "../utils/formatting.ts"
import * as ast from "./bare-ast.ts"

export function checkSemantic(schema: ast.Ast, config: Config): ast.Ast {
if (schema.defs.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cli.ts → src/bin/bare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as fs from "node:fs"
import * as process from "node:process"
import * as util from "node:util"
import packageVersion from "../../VERSION.json" with { type: "json" }
import { CompilerError, Config, transform } from "../index.js"
import { CompilerError, Config, transform } from "../index.ts"

const HELP_TEXT = `
Usage: bare [options] [schema]
Expand Down
4 changes: 2 additions & 2 deletions src/generator/bare-ast-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Copyright (c) 2022 Victorien Elvinger
//! Licensed under the MIT License (https://mit-license.org/)

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

// This file is separated from ast folder because these utils are used for
// facilitating code generation.
Expand Down
4 changes: 2 additions & 2 deletions src/generator/bare-generator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Copyright (c) 2022 Victorien Elvinger
//! Licensed under the MIT License (https://mit-license.org/)

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

export function generateBare(schema: ast.Ast): string {
let result = ""
Expand Down
8 changes: 4 additions & 4 deletions src/generator/js-generator.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//! Copyright (c) 2022 Victorien Elvinger
//! Licensed under the MIT License (https://mit-license.org/)

import * as ast from "../ast/bare-ast.js"
import type { Config } from "../core/config.js"
import * as ast from "../ast/bare-ast.ts"
import type { Config } from "../core/config.ts"
import {
capitalize,
dent,
jsDoc,
jsRpr,
softSpace,
} from "../utils/formatting.js"
import * as utils from "./bare-ast-utils.js"
} from "../utils/formatting.ts"
import * as utils from "./bare-ast-utils.ts"

export function generate(schema: ast.Ast, config: Config): string {
const g: Gen = { config, symbols: ast.symbols(schema) }
Expand Down
28 changes: 14 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
//! Copyright (c) 2022 Victorien Elvinger
//! Licensed under the MIT License (https://mit-license.org/)

import { configure } from "./ast/bare-configure.js"
import { normalize } from "./ast/bare-normalization.js"
import { checkSemantic } from "./ast/bare-semantic-checker.js"
import { Config } from "./core/config.js"
import { generateBare } from "./generator/bare-generator.js"
import { generate } from "./generator/js-generator.js"
import { parse } from "./parser/bare-parser.js"
import { configure } from "./ast/bare-configure.ts"
import { normalize } from "./ast/bare-normalization.ts"
import { checkSemantic } from "./ast/bare-semantic-checker.ts"
import { Config } from "./core/config.ts"
import { generateBare } from "./generator/bare-generator.ts"
import { generate } from "./generator/js-generator.ts"
import { parse } from "./parser/bare-parser.ts"

export * from "./ast/bare-ast.js"
export * from "./ast/bare-configure.js"
export * from "./ast/bare-normalization.js"
export * from "./core/compiler-error.js"
export * from "./core/config.js"
export * from "./generator/js-generator.js"
export * from "./parser/bare-parser.js"
export * from "./ast/bare-ast.ts"
export * from "./ast/bare-configure.ts"
export * from "./ast/bare-normalization.ts"
export * from "./core/compiler-error.ts"
export * from "./core/config.ts"
export * from "./generator/js-generator.ts"
export * from "./parser/bare-parser.ts"

/**
* Turn the schema `content` into a target language, taking `conf` into account.
Expand Down
2 changes: 1 addition & 1 deletion src/parser/bare-lexer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

const SAMPLE = `
const C = { # struct
Expand Down
2 changes: 1 addition & 1 deletion src/parser/bare-lexer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Copyright (c) 2022 Victorien Elvinger
//! Licensed under the MIT License (https://mit-license.org/)

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

const WHITE_SPACE_PATTERN = /\s/
const PUNCTUATION_PATTERN = /[{}[\]()<>=|:,;.!?~+\\/$@#-]/
Expand Down
10 changes: 5 additions & 5 deletions src/parser/bare-parser.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! Copyright (c) 2022 Victorien Elvinger
//! Licensed under the MIT License (https://mit-license.org/)

import * as ast from "../ast/bare-ast.js"
import { CompilerError } from "../core/compiler-error.js"
import type { Config } from "../core/config.js"
import * as ast from "../ast/bare-ast.ts"
import { CompilerError } from "../core/compiler-error.ts"
import type { Config } from "../core/config.ts"
import {
ALL_CASE_RE,
CONSTANT_CASE_RE,
toPascalCase,
} from "../utils/formatting.js"
import * as lexer from "./bare-lexer.js"
} from "../utils/formatting.ts"
import * as lexer from "./bare-lexer.ts"

export function parse(content: string, config: Config): ast.Ast {
const p: Parser = { config, lex: lexer.create(content) }
Expand Down
18 changes: 0 additions & 18 deletions src/tsconfig-test.json

This file was deleted.

17 changes: 17 additions & 0 deletions src/tsconfig.bin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": false,
"rewriteRelativeImportExtensions": true,
"rootDir": ".",
"outDir": "../dist"
},
"include": [
"./bin/*.ts"
],
"references": [
{
"path": "."
}
]
}
9 changes: 5 additions & 4 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
{
"extends": "../tsconfig-base.json",
"extends": "../tsconfig.json",
"compilerOptions": {
"composite": true,
"checkJs": false,
"isolatedDeclarations": true,
"emitDeclarationOnly": true,
"noEmit": false,
"rewriteRelativeImportExtensions": true,
"types": [],
"rootDir": ".",
"outDir": "../dist"
},
"exclude": [
"./bin/cli.ts",
"./bin/**",
"./**/*.test.ts"
]
}
2 changes: 1 addition & 1 deletion tests-corpus/index.test.js → tests-corpus/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
configure,
parse,
transform,
} from "@bare-ts/tools"
} from "../src/index.ts"

const CORPUS_DIR = "./tests-corpus"
const INVALID_BARE_DIR = `${CORPUS_DIR}/invalid-bare-schema`
Expand Down
6 changes: 0 additions & 6 deletions tests-corpus/tsconfig.json

This file was deleted.

7 changes: 4 additions & 3 deletions tsconfig-base.json → tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@
],
"module": "NodeNext",
"target": "ES2022",
"types": [
"node"
],

"noEmit": true,

"types": [],

"allowImportingTsExtensions": true,
"esModuleInterop": true,
"isolatedModules": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"verbatimModuleSyntax": true,

"allowUnreachableCode": false,
"checkJs": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
Expand Down

0 comments on commit c0aa60d

Please sign in to comment.