Skip to content

Commit

Permalink
feat: support ts 5.5 ${configDir} replacement (#172)
Browse files Browse the repository at this point in the history
* feat: support ts 5.5  replacement

* fix: generate types

* fix: ensure posix paths in output

* fix: add snapshots for node22 errors

* fix: regenerate types map

* fix: action syntax

* fix: actions syntax again?

* refactor: reuse token replace function to save space

* fix: windows paths again
  • Loading branch information
dominikg authored May 23, 2024
1 parent 10d41e7 commit 9a31f3f
Show file tree
Hide file tree
Showing 37 changed files with 271 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-ants-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'tsconfck': minor
---

add support for typescript 5.5 ${configDir} replacement
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
strategy:
matrix:
# pseudo-matrix for convenience, NEVER use more than a single combination
node: [18]
node: [20]
os: [ubuntu-latest]
steps:
- name: checkout
Expand Down
16 changes: 14 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
# pseudo-matrix for convenience, NEVER use more than a single combination
node: [18]
node: [20]
os: [ubuntu-latest]
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -57,11 +57,19 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [18]
node: [20]
os: [ubuntu-latest, macos-latest, windows-latest]
ts: ['current']
include:
- node: 18
os: ubuntu-latest
ts: 'current'
- node: 22
os: ubuntu-latest
ts: 'current'
- node: 20
os: ubuntu-latest
ts: 'beta'
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
Expand All @@ -81,5 +89,9 @@ jobs:
- name: install
run: |
pnpm install --frozen-lockfile --prefer-offline --ignore-scripts
- id: install_ts_beta
name: install ts beta
if: matrix.ts == 'beta'
run: pnpm --dir packages/tsconfck install -D typescript@beta
- name: run tests
run: pnpm test
14 changes: 10 additions & 4 deletions packages/tsconfck/src/parse-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
native2posix,
resolveReferencedTSConfigFiles,
resolveSolutionTSConfig,
resolveTSConfigJson
resolveTSConfigJson,
replaceTokens
} from './util.js';
import { findNative } from './find-native.js';

Expand Down Expand Up @@ -102,17 +103,20 @@ function parseFile(tsconfigFile, ts, options, skipCache) {
undefined,
posixTSConfigFile
);

checkErrors(nativeResult, tsconfigFile);

/** @type {import('./public.d.ts').TSConfckParseNativeResult} */
const result = {
tsconfigFile,
tsconfig: result2tsconfig(nativeResult, ts),
tsconfig: result2tsconfig(nativeResult, ts, tsconfigFile),
result: nativeResult
};

if (!skipCache) {
cache?.setParseResult(tsconfigFile, Promise.resolve(result));
}

return result;
}

Expand Down Expand Up @@ -166,9 +170,10 @@ function checkErrors(nativeResult, tsconfigFile) {
*
* @param {any} result
* @param {any} ts typescript
* @param {string} tsconfigFile
* @returns {object} tsconfig with merged compilerOptions and enums restored to their string form
*/
function result2tsconfig(result, ts) {
function result2tsconfig(result, ts, tsconfigFile) {
// dereference result.raw so changes below don't modify original
const tsconfig = JSON.parse(JSON.stringify(result.raw));
// for some reason the extended compilerOptions are not available in result.raw but only in result.options
Expand Down Expand Up @@ -243,7 +248,8 @@ function result2tsconfig(result, ts) {
// delete if it is false to match content of tsconfig
delete tsconfig.compileOnSave;
}
return tsconfig;
// ts itself has not replaced all tokens at this point, make sure they are
return replaceTokens(tsconfig, path.dirname(tsconfigFile));
}

export class TSConfckParseNativeError extends Error {
Expand Down
4 changes: 3 additions & 1 deletion packages/tsconfck/src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
makePromise,
native2posix,
resolve2posix,
replaceTokens,
resolveReferencedTSConfigFiles,
resolveSolutionTSConfig,
resolveTSConfigJson
Expand Down Expand Up @@ -136,7 +137,8 @@ function normalizeTSConfig(tsconfig, dir) {
if (tsconfig.compilerOptions?.baseUrl && !path.isAbsolute(tsconfig.compilerOptions.baseUrl)) {
tsconfig.compilerOptions.baseUrl = resolve2posix(dir, tsconfig.compilerOptions.baseUrl);
}
return tsconfig;

return replaceTokens(tsconfig, dir);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions packages/tsconfck/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,17 @@ function pattern2regex(resolvedPattern, allowJs) {

return new RegExp(regexStr);
}

/**
* replace tokens like ${configDir}
* @param {any} tsconfig
* @param {string} configDir
* @returns {any}
*/
export function replaceTokens(tsconfig, configDir) {
return JSON.parse(
JSON.stringify(tsconfig)
// replace ${configDir}, accounting for rebaseRelative emitted ../${configDir}
.replaceAll(/"(?:\.\.\/)*\${configDir}/g, `"${native2posix(configDir)}`)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {
return 'foo';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["../tsconfig.base.json"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {
return 'foo';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["../tsconfig.base.json"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {
return 'foo';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"include": ["${configDir}/**/*"],
"exclude": ["${configDir}/src/**/*.spec.ts"],
"compilerOptions": {
"paths": {
"$src": ["${configDir}/src/**/*"]
},
"outDir": "${configDir}/build"
}
}
6 changes: 6 additions & 0 deletions packages/tsconfck/tests/parse-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { transform as esbuildTransform } from 'esbuild';
import ts from 'typescript';
import { TSConfckCache } from '../src/cache.js';

const ts_ver = ts.version.slice(3).split('.');
const IS_TS_5_5 = ts_ver[0] >= 5 && ts_ver[1] >= 5;

describe('parse', () => {
it('should be a function', () => {
expect(parseNative).toBeTypeOf('function');
Expand Down Expand Up @@ -51,6 +54,9 @@ describe('parse', () => {
it('should resolve with expected valid tsconfig.json', async () => {
const samples = await globFixtures('parse/valid/**/tsconfig.json');
for (const filename of samples) {
if (!IS_TS_5_5 && filename.includes('configDir')) {
continue; // configDir doesn't work in 5.4
}
const actual = await parseNative(filename);
expect(actual.tsconfigFile).toBe(filename);
await expectToMatchSnap(actual.tsconfig, `input: ${filename}`, filename, 'parse-native');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TS 1005: ',' expected.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parsing <fixture-dir>/parse/invalid/bom/tsconfig.json failed: SyntaxError: Unexpected token 'x', "{
"extends": x""
}
" is not valid JSON
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TS 1136: Property assignment expected.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
parsing <fixture-dir>/parse/invalid/comma/tsconfig.json failed: SyntaxError: Expected double-quoted property name in JSON at position 69 (line 5 column 6)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TS 1005: ',' expected.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
parsing <fixture-dir>/parse/invalid/comments/tsconfig.json failed: SyntaxError: Expected ',' or '}' after property value in JSON at position 236 (line 14 column 7)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TS 18000: Circularity detected while resolving configuration: <fixture-dir>/parse/invalid/extends-array-circular/tsconfig.json -> <fixture-dir>/parse/invalid/extends-array-circular/tsconfig.a.json -> <fixture-dir>/parse/invalid/extends-array-circular/tsconfig.a.b.json -> <fixture-dir>/parse/invalid/extends-array-circular/tsconfig.a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Circular dependency in "extends": <fixture-dir>/parse/invalid/extends-array-circular/tsconfig.json -> <fixture-dir>/parse/invalid/extends-array-circular/tsconfig.b.json -> <fixture-dir>/parse/invalid/extends-array-circular/tsconfig.a.b.json -> <fixture-dir>/parse/invalid/extends-array-circular/tsconfig.a.json -> <fixture-dir>/parse/invalid/extends-array-circular/tsconfig.a.b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TS 18000: Circularity detected while resolving configuration: <fixture-dir>/parse/invalid/extends-circular/tsconfig.json -> <fixture-dir>/parse/invalid/extends-circular/tsconfig.circular.json -> <fixture-dir>/parse/invalid/extends-circular/deep/tsconfig.circular2.json -> <fixture-dir>/parse/invalid/extends-circular/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Circular dependency in "extends": <fixture-dir>/parse/invalid/extends-circular/tsconfig.json -> <fixture-dir>/parse/invalid/extends-circular/tsconfig.circular.json -> <fixture-dir>/parse/invalid/extends-circular/deep/tsconfig.circular2.json -> <fixture-dir>/parse/invalid/extends-circular/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TS 6053: File './dir' not found.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
failed to resolve "extends":"./dir" in <fixture-dir>/parse/invalid/extends-fallback-not-found/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TS 5083: Cannot read file '<fixture-dir>/parse/invalid/tsconfig.doesnotexist.json'.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
failed to resolve "extends":"../tsconfig.doesnotexist.json" in <fixture-dir>/parse/invalid/extends-not-found/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TS 1005: ',' expected.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
parsing <fixture-dir>/parse/invalid/mixed/tsconfig.json failed: SyntaxError: Expected ',' or '}' after property value in JSON at position 297 (line 16 column 5)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": [
"../tsconfig.base.json"
],
"include": [
"<fixture-dir>/parse/valid/configDir/a/**/*"
],
"exclude": [
"<fixture-dir>/parse/valid/configDir/a/src/**/*.spec.ts"
],
"compilerOptions": {
"paths": {
"$src": [
"<fixture-dir>/parse/valid/configDir/a/src/**/*"
]
},
"outDir": "<fixture-dir>/parse/valid/configDir/a/build"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": [
"../tsconfig.base.json"
],
"include": [
"<fixture-dir>/parse/valid/configDir/**/*"
],
"exclude": [
"<fixture-dir>/parse/valid/configDir/src/**/*.spec.ts"
],
"compilerOptions": {
"paths": {
"$src": [
"<fixture-dir>/parse/valid/configDir/src/**/*"
]
},
"outDir": "<fixture-dir>/parse/valid/configDir/build"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["../tsconfig.base.json"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": [
"../tsconfig.base.json"
],
"include": [
"<fixture-dir>/parse/valid/configDir/b/**/*"
],
"exclude": [
"<fixture-dir>/parse/valid/configDir/b/src/**/*.spec.ts"
],
"compilerOptions": {
"paths": {
"$src": [
"<fixture-dir>/parse/valid/configDir/b/src/**/*"
]
},
"outDir": "<fixture-dir>/parse/valid/configDir/b/build"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": [
"../tsconfig.base.json"
],
"include": [
"<fixture-dir>/parse/valid/configDir/**/*"
],
"exclude": [
"<fixture-dir>/parse/valid/configDir/src/**/*.spec.ts"
],
"compilerOptions": {
"paths": {
"$src": [
"<fixture-dir>/parse/valid/configDir/src/**/*"
]
},
"outDir": "<fixture-dir>/parse/valid/configDir/build"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["../tsconfig.base.json"]
}
2 changes: 1 addition & 1 deletion packages/tsconfck/types/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@
null,
null
],
"mappings": ";;;;;;;;iBAUsBA,IAAIA;;;;;;;;iBCYJC,OAAOA;;;;;;;iBCTbC,MAAMA;;;;;;;;;;iBCDAC,UAAUA;cCXnBC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC0BJC,KAAKA;cA2VdC,kBAAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC3VTC,WAAWA;cA6NpBC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;;;WCtPpBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAkCnBC,oBAAoBA;;;;WAIpBC,sBAAsBA;;;;;;;;;;;;;;;WAetBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA6BnBC,0BAA0BA;;;;;;;;;;;;WAY1BC,yBAAyBA"
"mappings": ";;;;;;;;iBAUsBA,IAAIA;;;;;;;;iBCYJC,OAAOA;;;;;;;iBCTbC,MAAMA;;;;;;;;;;iBCDAC,UAAUA;cCXnBC,aAAaA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC2BJC,KAAKA;cA4VdC,kBAAkBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBC5VTC,WAAWA;cAkOpBC,wBAAwBA;;;;;;;;;;;;;;;;;;;;;;;;;WC5PpBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WAkCnBC,oBAAoBA;;;;WAIpBC,sBAAsBA;;;;;;;;;;;;;;;WAetBC,mBAAmBA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;WA6BnBC,0BAA0BA;;;;;;;;;;;;WAY1BC,yBAAyBA"
}
Loading

0 comments on commit 9a31f3f

Please sign in to comment.