Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Commit

Permalink
test: update all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimarimon committed Aug 14, 2023
1 parent 3ec7bf4 commit 0f7506e
Show file tree
Hide file tree
Showing 68 changed files with 393 additions and 357 deletions.
21 changes: 1 addition & 20 deletions packages/core/src/analyser-context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ReflectedNode, ReflectedTypeNode } from './reflected-node.js';
import type { NamedNodeName, SymbolWithLocation } from './utils/is.js';
import type { AnalyserDiagnostic } from './analyser-diagnostic.js';
import type { AnalyserOptions } from './analyser-options.js';
import type { AnalyserSystem } from './analyser-system.js';
import type { ReflectedNode } from './reflected-node.js';
import ts from 'typescript';


Expand All @@ -23,8 +23,6 @@ export class AnalyserContext {

private readonly _reflectedNodesBySymbol = new Map<ts.Symbol, ReflectedNode>();

private readonly _reflectedNodesByType = new Map<ts.Type, ReflectedTypeNode>();

constructor(
system: AnalyserSystem,
program: ts.Program,
Expand Down Expand Up @@ -96,23 +94,6 @@ export class AnalyserContext {
return reflection as T;
}

/**
* Creates a new reflected type only if it doesn't exist already in the internal cache.
*
* @param type - The `ts.Type` associated with the reflected type
* @param reflectedTypeFactory - The function to use to build the new reflection if it doesn't exist
*/
registerReflectedType<T extends ReflectedTypeNode>(type: ts.Type, reflectedTypeFactory: () => T): T {
let reflection = this._reflectedNodesByType.get(type);

if (!reflection) {
reflection = reflectedTypeFactory();
this._reflectedNodesByType.set(type, reflection);
}

return reflection as T;
}

/**
* Returns the associated `ts.Symbol` for the given node
*
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/factories/create-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export function createType(nodeOrType: ts.TypeNode | ts.Type, context: AnalyserC

const factory = typeReflectors[node.kind];
if (factory) {
return context.registerReflectedType(type, () => factory(node as ts.TypeNode, type, context));
return factory(node, type, context);
}

return new UnknownTypeNode(node, type, context);
Expand Down
6 changes: 2 additions & 4 deletions packages/core/src/nodes/class-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,7 @@ export class ClassNode implements DeclarationNode<ClassDeclaration, ts.ClassDecl
}

if (isProperty || ts.isGetAccessor(decl) || ts.isSetAccessor(decl)) {
const callback = () => new PropertyNode(decl, member, this._context);
const reflectedNode = this._context.registerReflectedNode(decl, callback);
const reflectedNode = new PropertyNode(decl, member, this._context);

if (reflectedNode.getModifier() === ModifierType.public) {
result.push(reflectedNode);
Expand All @@ -332,8 +331,7 @@ export class ClassNode implements DeclarationNode<ClassDeclaration, ts.ClassDecl
(isProperty && (isArrowFunction(decl.initializer) || isFunctionExpression(decl.initializer)));

if (isPropertyMethod) {
const callback = () => new FunctionNode(decl, member, this._context);
const reflectedNode = this._context.registerReflectedNode(decl, callback);
const reflectedNode = new FunctionNode(decl, member, this._context);

if (reflectedNode.getModifier() === ModifierType.public) {
result.push(reflectedNode);
Expand Down
19 changes: 18 additions & 1 deletion packages/core/src/nodes/export-declaration-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ export class ExportDeclarationNode implements ReflectedRootNode<Export, ExportDe
return (this._node as Exclude<ExportDeclarationNodeType, ts.VariableStatement>).name?.getText() ?? '';
}

/**
* Returns the name of the symbol prefixed by any parent namespace is inside:
*
* <NamespaceName1>.<Namespace2>.<SymbolName>
*/
getFullyQualifiedName(): string {
const node = this._declaration ?? this._node;
const symbol = this._context.getSymbol(node);

if (symbol) {
const fullyQualifiedName = this._context.getTypeChecker().getFullyQualifiedName(symbol);
return fullyQualifiedName.split('.').slice(1).join('.');
}

return this.getName();
}

getOriginalName(): string {
return this.getName();
}
Expand All @@ -64,7 +81,7 @@ export class ExportDeclarationNode implements ReflectedRootNode<Export, ExportDe

serialize(): Export {
return {
name: this.getName(),
name: this.getFullyQualifiedName(),
kind: this.getKind(),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class ExpressionWithTypeArgumentsNode implements ReflectedNode<Expression
constructor(node: ts.ExpressionWithTypeArguments, context: AnalyserContext) {
this._node = node;
this._context = context;
this._loc = context.getLocation(node);
this._loc = context.getLocation(node.expression);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/nodes/function-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ export class FunctionNode implements DeclarationNode<FunctionDeclaration | Metho
if (
ts.isFunctionDeclaration(this._node) ||
ts.isMethodDeclaration(this._node) ||
ts.isMethodSignature(this._node)
ts.isMethodSignature(this._node) ||
ts.isPropertyDeclaration(this._node) ||
ts.isPropertySignature(this._node)
) {
return this._node.name?.getText() || '';
}

if (ts.isPropertyDeclaration(this._node) || ts.isPropertySignature(this._node)) {
return this._node.name?.getText() || '';
}

if (ts.isVariableStatement(this._node)) {
const declaration = this._node.declarationList.declarations.find(decl => {
return isArrowFunction(decl.initializer) || isFunctionExpression(decl.initializer);
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/nodes/interface-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ export class InterfaceNode implements DeclarationNode<InterfaceDeclaration, ts.I
(ts.isPropertySignature(decl) || ts.isGetAccessor(decl) || ts.isSetAccessor(decl)) &&
!isPropertyMethod
) {
const callback = () => new PropertyNode(decl, member, this._context);
const reflectedNode = this._context.registerReflectedNode(decl, callback);

const reflectedNode = new PropertyNode(decl, member, this._context);
result.push(reflectedNode);
}
}
Expand All @@ -126,9 +124,7 @@ export class InterfaceNode implements DeclarationNode<InterfaceDeclaration, ts.I
const isPropertyMethod = ts.isPropertySignature(decl) && decl.type && ts.isFunctionTypeNode(decl.type);

if (ts.isMethodSignature(decl) || isPropertyMethod) {
const callback = () => new FunctionNode(decl, member, this._context);
const reflectedNode = this._context.registerReflectedNode(decl, callback);

const reflectedNode = new FunctionNode(decl, member, this._context);
result.push(reflectedNode);
}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/class/abstract/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"kind": "Property",
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 2,
"default": 4
Expand All @@ -25,7 +25,7 @@
"kind": "Property",
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 4,
"abstract": true
Expand All @@ -40,7 +40,7 @@
"return": {
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
}
},
"line": 6,
Expand All @@ -49,15 +49,15 @@
"name": "x",
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 6
},
{
"name": "y",
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 6
}
Expand Down
16 changes: 8 additions & 8 deletions tests/class/basic/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@
"name": "x",
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 17
},
{
"name": "y",
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 17
},
{
"name": "message",
"type": {
"text": "string",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 17
}
Expand All @@ -61,7 +61,7 @@
"kind": "Property",
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 7,
"optional": true
Expand All @@ -71,7 +71,7 @@
"kind": "Property",
"type": {
"text": "string",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 11,
"readOnly": true
Expand Down Expand Up @@ -100,7 +100,7 @@
"return": {
"type": {
"text": "void",
"kind": "Primitive"
"kind": "Intrinsic"
}
},
"line": 23,
Expand All @@ -109,7 +109,7 @@
"name": "message",
"type": {
"text": "string",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 23
}
Expand All @@ -126,7 +126,7 @@
"return": {
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
}
},
"line": 27
Expand Down
4 changes: 2 additions & 2 deletions tests/class/class-expressions/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"kind": "Property",
"type": {
"text": "string",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 5,
"default": "'foo'"
Expand All @@ -43,7 +43,7 @@
"kind": "Property",
"type": {
"text": "string",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 12,
"default": "'bar'"
Expand Down
10 changes: 5 additions & 5 deletions tests/class/default-inheritance/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"kind": "Property",
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 2,
"default": 4
Expand All @@ -28,11 +28,11 @@
"heritage": [
{
"name": "Class1",
"kind": "Class",
"source": {
"line": 1,
"path": "tests/class/default-inheritance/index.ts"
},
"kind": "Class"
}
}
],
"properties": [
Expand All @@ -41,7 +41,7 @@
"kind": "Property",
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 6,
"default": 3
Expand All @@ -51,7 +51,7 @@
"kind": "Property",
"type": {
"text": "number",
"kind": "Primitive"
"kind": "Intrinsic"
},
"line": 2,
"default": 4,
Expand Down
Loading

0 comments on commit 0f7506e

Please sign in to comment.