@signpostmarv/ts-assert 0.4.1 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/generated/assertions.d.ts +1 -1
- package/generated/assertions.js +2 -2
- package/generated/assertions.js.map +1 -0
- package/generated/assertions.ts +2882 -0
- package/lib/assertables.d.ts +29 -0
- package/lib/assertables.js +51 -0
- package/lib/assertables.js.map +1 -0
- package/lib/assertables.ts +135 -0
- package/lib/main.d.ts +260 -260
- package/lib/main.js +11 -1
- package/lib/main.js.map +1 -0
- package/lib/main.ts +74 -0
- package/package.json +17 -13
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { FunctionDeclaration, Identifier, TypeReferenceNode } from 'typescript';
|
|
2
|
+
type named_function = (FunctionDeclaration & {
|
|
3
|
+
name: Identifier;
|
|
4
|
+
});
|
|
5
|
+
type assertable_function<NodeParam extends string = string> = (named_function & {
|
|
6
|
+
name: (Identifier & {
|
|
7
|
+
text: `is${string}`;
|
|
8
|
+
});
|
|
9
|
+
parameters: [
|
|
10
|
+
{
|
|
11
|
+
name: (Identifier & {
|
|
12
|
+
text: NodeParam;
|
|
13
|
+
});
|
|
14
|
+
type: (TypeReferenceNode & {
|
|
15
|
+
typeName: Identifier;
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
];
|
|
19
|
+
type: ({
|
|
20
|
+
parameterName: (Identifier & {
|
|
21
|
+
text: NodeParam;
|
|
22
|
+
});
|
|
23
|
+
type: (TypeReferenceNode & {
|
|
24
|
+
typeName: Identifier;
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
export declare const assertables: assertable_function[];
|
|
29
|
+
export default assertables;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { readFile, } from 'node:fs/promises';
|
|
2
|
+
import ts from 'typescript';
|
|
3
|
+
import assert from 'node:assert/strict';
|
|
4
|
+
// oxlint-disable-next-line @stylistic/max-len
|
|
5
|
+
// oxlint-disable-next-line typescript/no-unsafe-assignment, typescript/no-unsafe-call
|
|
6
|
+
const typescript_defs = await readFile(`${import.meta.dirname}/../node_modules/typescript/lib/typescript.d.ts`);
|
|
7
|
+
const ast = ts.createSourceFile('typescript.d.ts',
|
|
8
|
+
// oxlint-disable-next-line @stylistic/max-len
|
|
9
|
+
// oxlint-disable-next-line typescript/no-unsafe-argument, typescript/no-unsafe-call, typescript/no-unsafe-member-access
|
|
10
|
+
typescript_defs.toString(), ts.ScriptTarget.Latest, false, ts.ScriptKind.TS).statements.filter((e) => ts.isModuleDeclaration(e));
|
|
11
|
+
// oxlint-disable-next-line @stylistic/max-len
|
|
12
|
+
// oxlint-disable-next-line typescript/no-unsafe-call, typescript/no-unsafe-member-access
|
|
13
|
+
assert.equal(ast.length, 1, 'Could not obtain typescript module AST');
|
|
14
|
+
const module_body = ast[0].body;
|
|
15
|
+
// oxlint-disable-next-line @stylistic/max-len
|
|
16
|
+
// oxlint-disable-next-line typescript/no-unsafe-call, typescript/no-unsafe-member-access
|
|
17
|
+
assert.equal(!!module_body && !!ts.isModuleBlock(module_body), true, 'Could not obtain module body!');
|
|
18
|
+
const { statements } = module_body;
|
|
19
|
+
const functions = statements.filter((e) => {
|
|
20
|
+
return ts.isFunctionDeclaration(e) && !!e.name;
|
|
21
|
+
});
|
|
22
|
+
const skip_these = [
|
|
23
|
+
'isAssertClause',
|
|
24
|
+
'isAssertEntry',
|
|
25
|
+
'isImportTypeAssertionContainer',
|
|
26
|
+
'isUnparsedTextLike',
|
|
27
|
+
'isUnparsedNode',
|
|
28
|
+
'isUnparsedPrepend',
|
|
29
|
+
'isUnparsedSource',
|
|
30
|
+
'isUnparsedTextLike',
|
|
31
|
+
];
|
|
32
|
+
export const assertables = functions.filter((e) => {
|
|
33
|
+
return (e.name.text.startsWith('is')
|
|
34
|
+
&& !skip_these.includes(e.name.text)
|
|
35
|
+
&& 1 === e.parameters.length
|
|
36
|
+
&& !!e.parameters[0].type
|
|
37
|
+
&& ts.isTypeReferenceNode(e.parameters[0].type)
|
|
38
|
+
&& ts.isIdentifier(e.parameters[0].type.typeName)
|
|
39
|
+
&& ts.isIdentifier(e.parameters[0].name)
|
|
40
|
+
&& !!e.type
|
|
41
|
+
&& ts.isTypePredicateNode(e.type)
|
|
42
|
+
&& !!e.type.type
|
|
43
|
+
&& ts.isTypeReferenceNode(e.type.type)
|
|
44
|
+
&& ts.isIdentifier(e.type.type.typeName)
|
|
45
|
+
&& ts.isIdentifier(e.type.parameterName)
|
|
46
|
+
&& e.parameters[0].name.text === e.type.parameterName.text);
|
|
47
|
+
}).sort((a, b) => {
|
|
48
|
+
return a.name.text.localeCompare(b.name.text);
|
|
49
|
+
});
|
|
50
|
+
export default assertables;
|
|
51
|
+
//# sourceMappingURL=assertables.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assertables.js","sourceRoot":"","sources":["assertables.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,QAAQ,GACR,MAAM,kBAAkB,CAAC;AAQ1B,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,MAAM,MAAM,oBAAoB,CAAC;AAExC,8CAA8C;AAC9C,sFAAsF;AACtF,MAAM,eAAe,GAAG,MAAM,QAAQ,CAAC,GACtC,MAAM,CAAC,IAAI,CAAC,OACb,iDAAiD,CAAC,CAAC;AAEnD,MAAM,GAAG,GAAG,EAAE,CAAC,gBAAgB,CAC9B,iBAAiB;AAEjB,8CAA8C;AAC9C,wHAAwH;AACxH,eAAe,CAAC,QAAQ,EAAE,EAC1B,EAAE,CAAC,YAAY,CAAC,MAAM,EACtB,KAAK,EACL,EAAE,CAAC,UAAU,CAAC,EAAE,CAChB,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAA0B,EAAE,CAAC,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC;AAE9E,8CAA8C;AAC9C,yFAAyF;AACzF,MAAM,CAAC,KAAK,CAA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C,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;WACrC,CAAC,CAAC,CAAC,CAAC,IAAI;WACR,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC,IAAI,CAAC;WAC9B,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI;WACb,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;WACnC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC;WACrC,EAAE,CAAC,YAAY,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC;WACrC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,CAC1D,CAAC;AACH,CAAC,CACD,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;IACf,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC/C,CAAC,CAAC,CAAC;AAEH,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {
|
|
2
|
+
readFile,
|
|
3
|
+
} from 'node:fs/promises';
|
|
4
|
+
import type {
|
|
5
|
+
FunctionDeclaration,
|
|
6
|
+
Identifier,
|
|
7
|
+
ModuleBlock,
|
|
8
|
+
ModuleDeclaration,
|
|
9
|
+
TypeReferenceNode,
|
|
10
|
+
} from 'typescript';
|
|
11
|
+
import ts from 'typescript';
|
|
12
|
+
import assert from 'node:assert/strict';
|
|
13
|
+
|
|
14
|
+
// oxlint-disable-next-line @stylistic/max-len
|
|
15
|
+
// oxlint-disable-next-line typescript/no-unsafe-assignment, typescript/no-unsafe-call
|
|
16
|
+
const typescript_defs = await readFile(`${
|
|
17
|
+
import.meta.dirname
|
|
18
|
+
}/../node_modules/typescript/lib/typescript.d.ts`);
|
|
19
|
+
|
|
20
|
+
const ast = ts.createSourceFile(
|
|
21
|
+
'typescript.d.ts',
|
|
22
|
+
|
|
23
|
+
// oxlint-disable-next-line @stylistic/max-len
|
|
24
|
+
// oxlint-disable-next-line typescript/no-unsafe-argument, typescript/no-unsafe-call, typescript/no-unsafe-member-access
|
|
25
|
+
typescript_defs.toString(),
|
|
26
|
+
ts.ScriptTarget.Latest,
|
|
27
|
+
false,
|
|
28
|
+
ts.ScriptKind.TS,
|
|
29
|
+
).statements.filter((e): e is ModuleDeclaration => ts.isModuleDeclaration(e));
|
|
30
|
+
|
|
31
|
+
// oxlint-disable-next-line @stylistic/max-len
|
|
32
|
+
// oxlint-disable-next-line typescript/no-unsafe-call, typescript/no-unsafe-member-access
|
|
33
|
+
assert.equal(ast.length, 1, 'Could not obtain typescript module AST');
|
|
34
|
+
|
|
35
|
+
const module_body = ast[0].body;
|
|
36
|
+
|
|
37
|
+
// oxlint-disable-next-line @stylistic/max-len
|
|
38
|
+
// oxlint-disable-next-line typescript/no-unsafe-call, typescript/no-unsafe-member-access
|
|
39
|
+
assert.equal(
|
|
40
|
+
!!module_body && !!ts.isModuleBlock(module_body),
|
|
41
|
+
true,
|
|
42
|
+
'Could not obtain module body!',
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const {statements} = module_body as ModuleBlock;
|
|
46
|
+
type named_function = (
|
|
47
|
+
& FunctionDeclaration
|
|
48
|
+
& {name: Identifier}
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const functions: named_function[] = statements.filter(
|
|
52
|
+
(e): e is named_function => {
|
|
53
|
+
return ts.isFunctionDeclaration(e) && !!e.name;
|
|
54
|
+
},
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
type assertable_function<
|
|
58
|
+
NodeParam extends string = string,
|
|
59
|
+
> = (
|
|
60
|
+
& named_function
|
|
61
|
+
& {
|
|
62
|
+
name: (
|
|
63
|
+
& Identifier
|
|
64
|
+
& {text: `is${string}`}
|
|
65
|
+
),
|
|
66
|
+
parameters: [
|
|
67
|
+
{
|
|
68
|
+
name: (
|
|
69
|
+
& Identifier
|
|
70
|
+
& {
|
|
71
|
+
text: NodeParam,
|
|
72
|
+
}
|
|
73
|
+
),
|
|
74
|
+
type: (
|
|
75
|
+
& TypeReferenceNode
|
|
76
|
+
& {
|
|
77
|
+
typeName: Identifier,
|
|
78
|
+
}
|
|
79
|
+
),
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
type: (
|
|
83
|
+
& {
|
|
84
|
+
parameterName: (
|
|
85
|
+
& Identifier
|
|
86
|
+
& {
|
|
87
|
+
text: NodeParam,
|
|
88
|
+
}
|
|
89
|
+
),
|
|
90
|
+
type: (
|
|
91
|
+
& TypeReferenceNode
|
|
92
|
+
& {
|
|
93
|
+
typeName: Identifier,
|
|
94
|
+
}
|
|
95
|
+
),
|
|
96
|
+
}
|
|
97
|
+
),
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
const skip_these = [
|
|
102
|
+
'isAssertClause',
|
|
103
|
+
'isAssertEntry',
|
|
104
|
+
'isImportTypeAssertionContainer',
|
|
105
|
+
'isUnparsedTextLike',
|
|
106
|
+
'isUnparsedNode',
|
|
107
|
+
'isUnparsedPrepend',
|
|
108
|
+
'isUnparsedSource',
|
|
109
|
+
'isUnparsedTextLike',
|
|
110
|
+
];
|
|
111
|
+
|
|
112
|
+
export const assertables: assertable_function[] = functions.filter(
|
|
113
|
+
(e): e is assertable_function => {
|
|
114
|
+
return (
|
|
115
|
+
e.name.text.startsWith('is')
|
|
116
|
+
&& !skip_these.includes(e.name.text)
|
|
117
|
+
&& 1 === e.parameters.length
|
|
118
|
+
&& !!e.parameters[0].type
|
|
119
|
+
&& ts.isTypeReferenceNode(e.parameters[0].type)
|
|
120
|
+
&& ts.isIdentifier(e.parameters[0].type.typeName)
|
|
121
|
+
&& ts.isIdentifier(e.parameters[0].name)
|
|
122
|
+
&& !!e.type
|
|
123
|
+
&& ts.isTypePredicateNode(e.type)
|
|
124
|
+
&& !!e.type.type
|
|
125
|
+
&& ts.isTypeReferenceNode(e.type.type)
|
|
126
|
+
&& ts.isIdentifier(e.type.type.typeName)
|
|
127
|
+
&& ts.isIdentifier(e.type.parameterName)
|
|
128
|
+
&& e.parameters[0].name.text === e.type.parameterName.text
|
|
129
|
+
);
|
|
130
|
+
},
|
|
131
|
+
).sort((a, b) => {
|
|
132
|
+
return a.name.text.localeCompare(b.name.text);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
export default assertables;
|