Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite recursive conditional types to be tail-recursive #256

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@
"object-curly-newline" : ["error"],
"object-curly-spacing" : ["error"],
"prefer-object-spread" : ["error"],
"quotes" : ["error", "single"],
"quotes" : ["error", "single", { "avoidEscape": true }],
"semi" : ["error", "never"],
"no-trailing-spaces" : ["error"]
// "multiline-comment-style" : ["error", "starred-block"]
}
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
"test:lint": "bash scripts/test/lint.sh",
"test:types": "bash scripts/test/types.sh"
},
"dependencies": {},
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
"@types/node": "^16.10.3",
"@typescript-eslint/parser": "^4.14.0",
"eledoc": "^0.2.0",
"eslint": "^7.18.0",
Expand All @@ -59,6 +59,6 @@
"ts-node": "^9.1.0",
"tslib": "^2.1.0",
"typedoc": "^0.20.0",
"typescript": "latest"
"typescript": "^4.5.0-beta"
}
}
55 changes: 5 additions & 50 deletions scripts/tools/regex-update-file.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,16 @@
// @ts-ignore
import * as fs from 'fs'
// @ts-ignore
import * as rl from 'readline'

// regex-update-file
// regex-find-file

const updateMatchTo = (line: string, match: RegExp, to: string) => {
const result = match.exec(line) || {groups: {}}
const groups = result.groups

// if user is not using groups just do
if (!groups) return line.replace(match, to)

Object.keys(groups).forEach((key) => {
const groupMatch = new RegExp(`<${key}>`, 'gu')

to = to.replace(groupMatch, groups[key])
line = line.replace(match, to)
})

return line
}


const replaceInFile = (
file : string,
path : string,
match: RegExp,
to : string,
) => {
let content = ''
let didMatch = false

const filePath = `${path}/${file}`
const streamFD = fs.createReadStream(filePath)

rl.createInterface(streamFD).
on('line', (line: string) => {
if (line.match(match)) {
line = updateMatchTo(line, match, to)

didMatch = true
}

content += `${line}\n`
}).
on('close', () => {
if (didMatch) {
fs.writeFileSync(filePath, content)

console.info(`wrote: ${filePath}`)
}

streamFD.close()
})
fs.writeFileSync(filePath, fs.readFileSync(filePath, 'utf8').replace(match, to))
}

const isPathIncluded = (
Expand Down Expand Up @@ -84,7 +40,7 @@ const replaceInDir = (
replc : string,
include: string[],
exclude: string[],
) => fs.readdir(path, 'utf8', (error: Error, docs: string[]) => {
) => fs.readdir(path, 'utf8', (error, docs) => {
if (error)
console.error(error)
else {
Expand Down Expand Up @@ -127,8 +83,7 @@ const paths = (paths: string[]) => {
possibilities = [
...possibilities, // merge previous
...path.split('/'). // split the path
// @ts-ignore
map((path, index, array) => { // get combination
map((_, index, array) => { // get combination
return array.slice(0, index + 1)
}). // got combinations
map((splitPath, index, splitPaths) => { // put it back as path
Expand All @@ -144,7 +99,7 @@ const paths = (paths: string[]) => {

const replace = (
path: string,
match: string,
match: string | RegExp,
replc: string,
include: string[],
exclude: string[],
Expand All @@ -164,7 +119,7 @@ const replace = (

// console.log(include)

return replaceInDir(path, new RegExp(match, 'u'), replc, include, exclude)
return replaceInDir(path, typeof match === 'string'? new RegExp(match, 'ug'): match, replc, include, exclude)
}

export default replace
41 changes: 41 additions & 0 deletions scripts/tools/update-IterationMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
the maximum stack depth for recursive conditional types often changes - see https://github.com/microsoft/TypeScript/pull/45711
You can use this script to easily change the range of numbers defined in IterationMap. for example:
`ts-node scripts/tools/update-IterationMap.ts -1000 1000` will change the range from -1000 to 1000
*/

import replace from './regex-update-file'
import {Iteration} from '../../sources/Iteration/Iteration'

const start = Number(process.argv[2])
const end = Number(process.argv[3]);

[start, end].forEach((num) => {
if (isNaN(num))
throw new Error('not a number')
else
return num
})

const iterations: string[] = ["'__': [number, '-' | '0' | '+', '__', '__', '__'],"]

type Sign = Iteration[1]

for (let i = start; i <= end; i++) {
let sign: Sign
if (i < 0)
sign = '-'
else if (i === 0)
sign = '0'
else
sign = '+'
iterations.push(`'${i}': [${i}, '${sign}', '${i === start? '__': i - 1}', '${i === end? '__': i + 1}', '${i * -1}'],`)
}

replace('./sources/Iteration', /type IterationMap = \{(.|\n)*\}/um,
`type IterationMap = {
${iterations.join('\n ')}
}`,
['Iteration.ts'],
[],
)
Loading