-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor type postprocessing script. Add type tests.
- Loading branch information
1 parent
270f379
commit f38437c
Showing
9 changed files
with
1,998 additions
and
409 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
dist | ||
scripts |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// While TypesSript compiler might be a tool-of-choice for this task, | ||
// we've indeed decided to let UnionReplacer show off its power. | ||
|
||
const UnionReplacer = require('../../dist/union-replacer.cjs'); | ||
|
||
const TUPLE_ARRAY_TYPE_RE = /^([ \t]*(?:declare\s+)?type\s+(\w+)\s*=\s*)\(([^;\r\n]*)\)\[\];/gm; | ||
const OP_BRACKETS = ['[', '{', '<', '(']; | ||
const CL_BRACKETS = [']', '}', '>', ')']; | ||
const BRACKET_MAP = new Map( | ||
CL_BRACKETS.map((bracket, index) => [bracket, OP_BRACKETS[index]]), | ||
); | ||
const [OP_BRACKETS_RE, CL_BRACKETS_RE] = [OP_BRACKETS, CL_BRACKETS].map( | ||
(list) => new RegExp(`[${list.map((br) => `\\${br}`).join('')}]`), | ||
); | ||
const unionToListReplacer = new UnionReplacer([ | ||
[/=>/, '$&'], | ||
[OP_BRACKETS_RE, function opBracket(m) { this.open(m); return m; }], | ||
[CL_BRACKETS_RE, function clBracket(m) { this.close(m); return m; }], | ||
[/\s*\|\s*/, function separator(m) { return this.atRoot() ? ', ' : m; }], | ||
]); | ||
|
||
/* eslint-disable lines-between-class-members */ | ||
class UnionToListConverter { | ||
constructor() { this.nestLevels = {}; } | ||
open(bracket) { this.nestLevels[bracket] += 1; } | ||
close(bracket) { this.nestLevels[BRACKET_MAP[bracket]] -= 1; } | ||
atRoot() { return Object.values(this.nestLevels).every((count) => count === 0); } | ||
convert(unionTypeDef) { | ||
OP_BRACKETS.forEach((bracket) => { this.nestLevels[bracket] = 0; }); | ||
return unionToListReplacer.replace(unionTypeDef, this); | ||
} | ||
} | ||
|
||
/** | ||
* Convert tuple-like arrays to tuples. | ||
* | ||
* @param {string} tsd - Typescript declarations to perform conversion on. | ||
* @param {RegExp} nameRe - Pattern determining tuple type names to convert. | ||
* @returns {string} Converted typescript declarations. | ||
*/ | ||
function tsdConvertTupleArrays(tsd, nameRe) { | ||
const unionToListConverter = new UnionToListConverter(); | ||
return tsd.replace(TUPLE_ARRAY_TYPE_RE, (m, declarator, name, unionTypeDef) => { | ||
if (!nameRe.test(name)) { | ||
return m; | ||
} | ||
const typeList = unionToListConverter.convert(unionTypeDef); | ||
if (!unionToListConverter.atRoot()) { | ||
throw new SyntaxError(`Unbalanced brackets in union type definition '${unionTypeDef}'`); | ||
} | ||
return `${declarator}[${typeList}];`; | ||
}); | ||
} | ||
|
||
module.exports = tsdConvertTupleArrays; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import UnionReplacer = require('union-replacer'); | ||
|
||
new UnionReplacer([]); | ||
new UnionReplacer(); // $ExpectError | ||
new UnionReplacer([/foo/, 'bar']); // $ExpectError | ||
new UnionReplacer([['foo', 'bar']]); // $ExpectError | ||
|
||
new UnionReplacer([[/foo/, 'bar']]); | ||
new UnionReplacer([[/foo/, 'bar', true]]); // $ExpectError | ||
new UnionReplacer([[/foo/, 'bar', false]]); // $ExpectError | ||
|
||
new UnionReplacer([[/foo/, (m: string, index: number): string => '']]); | ||
new UnionReplacer([[/foo/, (m: string, index: number): number => 1]]); // $ExpectError | ||
new UnionReplacer([[/foo/, (m: string): string => m, false]]); | ||
new UnionReplacer([[/foo/, (m: string): string => m, true]]); // $ExpectError | ||
|
||
new UnionReplacer([[/foo/, (ctx: UnionReplacer.MatchingContext): string => '', true]]); | ||
new UnionReplacer([[/foo/, (ctx: UnionReplacer.MatchingContext): number => 1, true]]); // $ExpectError | ||
new UnionReplacer([[/foo/, (ctx: UnionReplacer.MatchingContext): string => '', false]]); // $ExpectError | ||
|
||
const replacer: UnionReplacer = new UnionReplacer([ | ||
[/foo/, (m, index) => { | ||
m; // $ExpectType string | ||
return ''; | ||
}], | ||
[/bar/, (ctx: UnionReplacer.MatchingContext) => { | ||
ctx; // $ExpectType MatchingContext | ||
ctx.match; // $ExpectType RegExpExecArray | null | ||
return ''; | ||
}, true], | ||
]); | ||
|
||
replacer.replace('foobar'); // $ExpectType string | ||
|
||
class MyBuilder implements UnionReplacer.ReplacementBuilder<number> { | ||
addSubjectSlice(subject: string, start: number, end: number) {} | ||
addReplacedString(string: string) {} | ||
build() { return 1; } | ||
} | ||
replacer.replace('foobar', {}, new MyBuilder()); // $ExpectType number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"lib": [ | ||
"es6" | ||
], | ||
"noImplicitAny": true, | ||
"noImplicitThis": true, | ||
"strictNullChecks": true, | ||
"strictFunctionTypes": true, | ||
"noEmit": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"baseUrl": ".", | ||
"types": [], | ||
"paths": { "union-replacer": ["."] } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "dtslint/dtslint.json", | ||
"rules": { | ||
"strict-export-declare-modifiers": false, | ||
"no-unnecessary-qualifier": false, | ||
"max-line-length": false, | ||
"no-redundant-jsdoc": false | ||
} | ||
} |