@ealmansi/jagger
Advanced tools
Comparing version 0.1.11 to 0.1.12
@@ -8,4 +8,4 @@ /** | ||
*/ | ||
static provide<T>(): T; | ||
static provide<Args extends unknown[], T>(_: new (...args: Args) => T): () => T; | ||
} | ||
//# sourceMappingURL=index.d.ts.map |
@@ -11,9 +11,11 @@ "use strict"; | ||
*/ | ||
static provide() { | ||
throw new Error([ | ||
"Jagger has not been configured correctly.", | ||
"Please, visit https://github.com/ealmansi/jagger.", | ||
].join(" ")); | ||
static provide(_) { | ||
return function provide() { | ||
throw new Error([ | ||
"Jagger has not been configured correctly.", | ||
"Please, visit https://github.com/ealmansi/jagger.", | ||
].join(" ")); | ||
}; | ||
} | ||
} | ||
exports.Jagger = Jagger; |
@@ -7,2 +7,3 @@ "use strict"; | ||
const typescript_1 = __importDefault(require("typescript")); | ||
const ts_pattern_1 = require("ts-pattern"); | ||
exports.default = (program) => { | ||
@@ -12,88 +13,91 @@ return (context) => { | ||
const visitor = (node) => { | ||
const typeChecker = program.getTypeChecker(); | ||
do { | ||
if (!typescript_1.default.isPropertyDeclaration(node)) { | ||
break; | ||
const replacementNode = (0, ts_pattern_1.match)(node) | ||
.with({ | ||
kind: typescript_1.default.SyntaxKind.PropertyDeclaration, | ||
name: { | ||
kind: typescript_1.default.SyntaxKind.Identifier, | ||
}, | ||
initializer: { | ||
kind: typescript_1.default.SyntaxKind.CallExpression, | ||
expression: { | ||
kind: typescript_1.default.SyntaxKind.PropertyAccessExpression, | ||
expression: { | ||
kind: typescript_1.default.SyntaxKind.Identifier, | ||
escapedText: "Jagger", | ||
}, | ||
name: { | ||
kind: typescript_1.default.SyntaxKind.Identifier, | ||
escapedText: "provide", | ||
}, | ||
}, | ||
arguments: [ | ||
{ | ||
kind: typescript_1.default.SyntaxKind.Identifier, | ||
}, | ||
], | ||
}, | ||
}, (node) => { | ||
const propertyDeclaration = node; | ||
const typeChecker = program.getTypeChecker(); | ||
const argument = node.initializer.arguments[0]; | ||
const argumentSymbol = typeChecker.getSymbolAtLocation(argument); | ||
const argumentClassDeclaration = (0, ts_pattern_1.match)(argumentSymbol) | ||
.with({ | ||
declarations: [ | ||
{ | ||
kind: typescript_1.default.SyntaxKind.ClassDeclaration, | ||
}, | ||
], | ||
}, (argumentSymbol) => argumentSymbol.declarations[0]) | ||
.with({ | ||
declarations: [ | ||
{ | ||
kind: typescript_1.default.SyntaxKind.ImportSpecifier, | ||
}, | ||
], | ||
}, (argumentSymbol) => { | ||
const typeChecker = program.getTypeChecker(); | ||
const declaration = argumentSymbol | ||
.declarations[0]; | ||
const moduleSpecifierSymbol = typeChecker.getSymbolAtLocation(declaration.parent.parent.parent.moduleSpecifier); | ||
const exportSymbol = moduleSpecifierSymbol?.exports?.get(declaration.name.escapedText); | ||
return (0, ts_pattern_1.match)(exportSymbol) | ||
.with({ | ||
valueDeclaration: { | ||
kind: typescript_1.default.SyntaxKind.ClassDeclaration, | ||
}, | ||
}, (exportSymbol) => exportSymbol.valueDeclaration) | ||
.otherwise(() => undefined); | ||
}) | ||
.otherwise(() => undefined); | ||
if (argumentClassDeclaration === undefined) { | ||
return undefined; | ||
} | ||
if (!typescript_1.default.isIdentifier(node.name)) { | ||
break; | ||
const argumentClassDeclarationConstructorDeclaration = argumentClassDeclaration.members.find(typescript_1.default.isConstructorDeclaration); | ||
if (argumentClassDeclarationConstructorDeclaration === undefined) { | ||
return undefined; | ||
} | ||
const nodeName = node.name; | ||
if (!(node.initializer !== undefined && | ||
typescript_1.default.isExpressionWithTypeArguments(node.initializer))) { | ||
break; | ||
} | ||
const nodeInitializer = node.initializer; | ||
if (!typescript_1.default.isPropertyAccessExpression(nodeInitializer.expression)) { | ||
break; | ||
} | ||
const nodeInitializerExpression = nodeInitializer.expression; | ||
if (!typescript_1.default.isIdentifier(nodeInitializerExpression.expression)) { | ||
break; | ||
} | ||
const nodeInitializerExpressionExpression = nodeInitializerExpression.expression; | ||
if (nodeInitializerExpressionExpression.escapedText !== "Jagger") { | ||
break; | ||
} | ||
if (!typescript_1.default.isIdentifier(nodeInitializerExpression.name)) { | ||
break; | ||
} | ||
const nodeInitializerExpressionName = nodeInitializerExpression.name; | ||
if (nodeInitializerExpressionName.escapedText !== "provide") { | ||
break; | ||
} | ||
if (nodeInitializer.typeArguments === undefined) { | ||
break; | ||
} | ||
const nodeInitializerTypeArguments = nodeInitializer.typeArguments; | ||
if (!(nodeInitializerTypeArguments.length === 1 && | ||
nodeInitializerTypeArguments[0] !== undefined && | ||
typescript_1.default.isTypeReferenceNode(nodeInitializerTypeArguments[0]))) { | ||
break; | ||
} | ||
const nodeInitializerTypeArgument = nodeInitializerTypeArguments[0]; | ||
if (!typescript_1.default.isIdentifier(nodeInitializerTypeArgument.typeName)) { | ||
break; | ||
} | ||
const nodeInitializerTypeArgumentTypeName = nodeInitializerTypeArgument.typeName; | ||
const nodeInitializerTypeArgumentTypeNameSymbol = typeChecker.getSymbolAtLocation(nodeInitializerTypeArgumentTypeName); | ||
if (!(nodeInitializerTypeArgumentTypeNameSymbol !== undefined && | ||
nodeInitializerTypeArgumentTypeNameSymbol.declarations !== | ||
undefined)) { | ||
break; | ||
} | ||
const nodeInitializerTypeArgumentTypeNameSymbolDeclarations = nodeInitializerTypeArgumentTypeNameSymbol.declarations; | ||
if (!(nodeInitializerTypeArgumentTypeNameSymbolDeclarations.length === | ||
1 && | ||
nodeInitializerTypeArgumentTypeNameSymbolDeclarations[0] !== | ||
undefined && | ||
typescript_1.default.isClassDeclaration(nodeInitializerTypeArgumentTypeNameSymbolDeclarations[0]))) { | ||
break; | ||
} | ||
const nodeInitializerTypeArgumentTypeNameSymbolClassDeclaration = nodeInitializerTypeArgumentTypeNameSymbolDeclarations[0]; | ||
const nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclaration = nodeInitializerTypeArgumentTypeNameSymbolClassDeclaration.members.find(typescript_1.default.isConstructorDeclaration); | ||
if (nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclaration === | ||
undefined) { | ||
break; | ||
} | ||
const nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclarationParameters = nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclaration.parameters; | ||
const nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclarationParametersTypes = []; | ||
for (const { type, } of nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclarationParameters) { | ||
const argumentClassDeclarationConstructorDeclarationParameters = argumentClassDeclarationConstructorDeclaration.parameters; | ||
const argumentClassDeclarationConstructorDeclarationParametersTypes = []; | ||
for (const { type, } of argumentClassDeclarationConstructorDeclarationParameters) { | ||
if (!(type !== undefined && typescript_1.default.isTypeReferenceNode(type))) { | ||
break; | ||
return undefined; | ||
} | ||
nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclarationParametersTypes.push(type); | ||
argumentClassDeclarationConstructorDeclarationParametersTypes.push(type); | ||
} | ||
const nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclarationParametersTypesTypeNames = []; | ||
for (const { typeName, } of nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclarationParametersTypes) { | ||
const argumentClassDeclarationConstructorDeclarationParametersTypesTypeNames = []; | ||
for (const { typeName, } of argumentClassDeclarationConstructorDeclarationParametersTypes) { | ||
if (!typescript_1.default.isIdentifier(typeName)) { | ||
break; | ||
return undefined; | ||
} | ||
nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclarationParametersTypesTypeNames.push(typeName); | ||
argumentClassDeclarationConstructorDeclarationParametersTypesTypeNames.push(typeName); | ||
} | ||
return context.factory.createPropertyDeclaration(node.modifiers, nodeName, node.questionToken ?? node.exclamationToken ?? undefined, node.type, context.factory.createFunctionExpression([], undefined, nodeName, [], [], undefined, context.factory.createBlock([ | ||
context.factory.createReturnStatement(context.factory.createNewExpression(nodeInitializerTypeArgumentTypeName, [], nodeInitializerTypeArgumentTypeNameSymbolClassDeclarationConstructorDeclarationParametersTypesTypeNames.map(({ escapedText }) => context.factory.createCallExpression(context.factory.createPropertyAccessExpression(context.factory.createThis(), context.factory.createIdentifier("provide" + escapedText)), undefined, [])))), | ||
return context.factory.createPropertyDeclaration(propertyDeclaration.modifiers, propertyDeclaration.name, propertyDeclaration.questionToken ?? | ||
propertyDeclaration.exclamationToken ?? | ||
undefined, propertyDeclaration.type, context.factory.createFunctionExpression([], undefined, propertyDeclaration.name, [], [], undefined, context.factory.createBlock([ | ||
context.factory.createReturnStatement(context.factory.createNewExpression(argument, [], argumentClassDeclarationConstructorDeclarationParametersTypesTypeNames.map(({ escapedText }) => context.factory.createCallExpression(context.factory.createPropertyAccessExpression(context.factory.createThis(), context.factory.createIdentifier("provide" + escapedText)), undefined, [])))), | ||
]))); | ||
} while (false); | ||
return typescript_1.default.visitEachChild(node, visitor, context); | ||
}) | ||
.otherwise(() => undefined); | ||
return replacementNode ?? typescript_1.default.visitEachChild(node, visitor, context); | ||
}; | ||
@@ -100,0 +104,0 @@ return typescript_1.default.visitNode(sourceFile, visitor); |
{ | ||
"name": "@ealmansi/jagger", | ||
"version": "0.1.11", | ||
"version": "0.1.12", | ||
"description": "WIP", | ||
@@ -33,2 +33,3 @@ "files": [ | ||
"rimraf": "^5.0.5", | ||
"ts-pattern": "^5.0.5", | ||
"typescript": "^5.2.2" | ||
@@ -40,4 +41,5 @@ }, | ||
"format": "prettier --write ./ts", | ||
"build": "tsc" | ||
"build": "tsc", | ||
"dev": "tsc --watch" | ||
} | ||
} |
@@ -65,4 +65,4 @@ # @ealmansi/jagger | ||
class AppModule { | ||
protected provideLogger = Jagger.provide<Logger>; | ||
public provideApp = Jagger.provide<App>; | ||
protected provideLogger = Jagger.provide(Logger); | ||
public provideApp = Jagger.provide(App); | ||
} | ||
@@ -69,0 +69,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
138
10558
4