@living-architecture/riviere-extract-ts 0.1.0 → 0.1.1
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/predicates/evaluate-predicate.d.ts +4 -0
- package/dist/predicates/evaluate-predicate.d.ts.map +1 -0
- package/dist/predicates/evaluate-predicate.js +129 -0
- package/dist/predicates/index.d.ts +2 -0
- package/dist/predicates/index.d.ts.map +1 -0
- package/dist/predicates/index.js +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAAE,KAAK,cAAc,EACvC,MAAM,aAAa,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EAAE,KAAK,cAAc,EACvC,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evaluate-predicate.d.ts","sourceRoot":"","sources":["../../src/predicates/evaluate-predicate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAKV,IAAI,EACL,MAAM,UAAU,CAAA;AAEjB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6CAA6C,CAAA;AA4B5E,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,SAAS,GAAG,OAAO,CA+B3E"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { Node as TsMorphNode } from 'ts-morph';
|
|
2
|
+
function isDecoratableNode(node) {
|
|
3
|
+
return TsMorphNode.isClassDeclaration(node) || TsMorphNode.isMethodDeclaration(node);
|
|
4
|
+
}
|
|
5
|
+
function isJSDocableNode(node) {
|
|
6
|
+
return (TsMorphNode.isMethodDeclaration(node) ||
|
|
7
|
+
TsMorphNode.isFunctionDeclaration(node) ||
|
|
8
|
+
TsMorphNode.isClassDeclaration(node));
|
|
9
|
+
}
|
|
10
|
+
function isNameableNode(node) {
|
|
11
|
+
return (TsMorphNode.isClassDeclaration(node) ||
|
|
12
|
+
TsMorphNode.isMethodDeclaration(node) ||
|
|
13
|
+
TsMorphNode.isFunctionDeclaration(node));
|
|
14
|
+
}
|
|
15
|
+
export function evaluatePredicate(node, predicate) {
|
|
16
|
+
if ('hasDecorator' in predicate) {
|
|
17
|
+
return evaluateHasDecorator(node, predicate.hasDecorator.name, predicate.hasDecorator.from);
|
|
18
|
+
}
|
|
19
|
+
if ('hasJSDoc' in predicate) {
|
|
20
|
+
return evaluateHasJSDoc(node, predicate.hasJSDoc.tag);
|
|
21
|
+
}
|
|
22
|
+
if ('extendsClass' in predicate) {
|
|
23
|
+
return evaluateExtendsClass(node, predicate.extendsClass.name);
|
|
24
|
+
}
|
|
25
|
+
if ('implementsInterface' in predicate) {
|
|
26
|
+
return evaluateImplementsInterface(node, predicate.implementsInterface.name);
|
|
27
|
+
}
|
|
28
|
+
if ('nameEndsWith' in predicate) {
|
|
29
|
+
return evaluateNameEndsWith(node, predicate.nameEndsWith.suffix);
|
|
30
|
+
}
|
|
31
|
+
if ('nameMatches' in predicate) {
|
|
32
|
+
return evaluateNameMatches(node, predicate.nameMatches.pattern);
|
|
33
|
+
}
|
|
34
|
+
if ('inClassWith' in predicate) {
|
|
35
|
+
return evaluateInClassWith(node, predicate.inClassWith);
|
|
36
|
+
}
|
|
37
|
+
if ('and' in predicate) {
|
|
38
|
+
return predicate.and.every((p) => evaluatePredicate(node, p));
|
|
39
|
+
}
|
|
40
|
+
/* istanbul ignore else -- @preserve: false branch leads to unreachable code; Predicate is exhaustive union */
|
|
41
|
+
if ('or' in predicate) {
|
|
42
|
+
return predicate.or.some((p) => evaluatePredicate(node, p));
|
|
43
|
+
}
|
|
44
|
+
/* istanbul ignore next -- @preserve: unreachable with valid Predicate type */
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
function evaluateHasDecorator(node, decoratorName, fromPackage) {
|
|
48
|
+
if (!isDecoratableNode(node)) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
const decorators = node.getDecorators();
|
|
52
|
+
const names = Array.isArray(decoratorName) ? decoratorName : [decoratorName];
|
|
53
|
+
return decorators.some((d) => {
|
|
54
|
+
if (!names.includes(d.getName())) {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
if (fromPackage !== undefined) {
|
|
58
|
+
return getDecoratorImportSource(d) === fromPackage;
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function getDecoratorImportSource(decorator) {
|
|
64
|
+
const name = decorator.getName();
|
|
65
|
+
const sourceFile = decorator.getSourceFile();
|
|
66
|
+
const importDecl = sourceFile.getImportDeclaration((decl) => {
|
|
67
|
+
const namedImports = decl.getNamedImports();
|
|
68
|
+
return namedImports.some((n) => n.getName() === name);
|
|
69
|
+
});
|
|
70
|
+
return importDecl?.getModuleSpecifierValue();
|
|
71
|
+
}
|
|
72
|
+
function evaluateHasJSDoc(node, tagName) {
|
|
73
|
+
if (!isJSDocableNode(node)) {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
const jsDocs = node.getJsDocs();
|
|
77
|
+
return jsDocs.some((jsDoc) => {
|
|
78
|
+
const tags = jsDoc.getTags();
|
|
79
|
+
return tags.some((tag) => tag.getTagName() === tagName);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function evaluateExtendsClass(node, className) {
|
|
83
|
+
if (!TsMorphNode.isClassDeclaration(node)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
const extendsExpr = node.getExtends();
|
|
87
|
+
if (extendsExpr === undefined) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return extendsExpr.getText() === className;
|
|
91
|
+
}
|
|
92
|
+
function evaluateImplementsInterface(node, interfaceName) {
|
|
93
|
+
if (!TsMorphNode.isClassDeclaration(node)) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
const implementsExprs = node.getImplements();
|
|
97
|
+
return implementsExprs.some((impl) => impl.getText() === interfaceName);
|
|
98
|
+
}
|
|
99
|
+
function evaluateNameEndsWith(node, suffix) {
|
|
100
|
+
if (!isNameableNode(node)) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
const name = node.getName();
|
|
104
|
+
/* istanbul ignore next -- @preserve: Anonymous declarations have undefined names; predicate correctly returns false */
|
|
105
|
+
if (name === undefined)
|
|
106
|
+
return false;
|
|
107
|
+
return name.endsWith(suffix);
|
|
108
|
+
}
|
|
109
|
+
function evaluateNameMatches(node, pattern) {
|
|
110
|
+
if (!isNameableNode(node)) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
const name = node.getName();
|
|
114
|
+
/* istanbul ignore next -- @preserve: Anonymous declarations have undefined names; predicate correctly returns false */
|
|
115
|
+
if (name === undefined)
|
|
116
|
+
return false;
|
|
117
|
+
const regex = new RegExp(pattern);
|
|
118
|
+
return regex.test(name);
|
|
119
|
+
}
|
|
120
|
+
function evaluateInClassWith(node, predicate) {
|
|
121
|
+
if (!TsMorphNode.isMethodDeclaration(node)) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
const parent = node.getParent();
|
|
125
|
+
/* istanbul ignore next -- @preserve: Methods in object literals have non-class parents; predicate correctly returns false */
|
|
126
|
+
if (!TsMorphNode.isClassDeclaration(parent))
|
|
127
|
+
return false;
|
|
128
|
+
return evaluatePredicate(parent, predicate);
|
|
129
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/predicates/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { evaluatePredicate } from './evaluate-predicate';
|