This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+9
View File
@@ -0,0 +1,9 @@
# The MIT License
Copyright 2021 Harminder Virk, contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+47
View File
@@ -0,0 +1,47 @@
<div align="center"><img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1564392111/adonis-banner_o9lunk.png" width="600px"></div>
# Ioc Transformer
> Typescript transformer to transform import statements to IoC container use calls
[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url]
The [Ioc container](https://github.com/adonisjs/fold) of AdonisJs exposes the `use` method to resolve dependencies from the container. However, using `use` and `import` statements together feels a bit cluttered. This module enables using `import` statements for IoC container bindings and transforms them to the `use` call by hooking into the Typescript compiler lifecycle.
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
## Table of contents
- [Usage](#usage)
<!-- END doctoc generated TOC please keep comment here to allow auto update -->
## Usage
Install the package from npm registry as follows:
```sh
npm i @adonisjs/ioc-transformer
```
Pass it to the Typescript compiler as `after` hook. Following is an example of using it with `ts-node`.
```ts
const { iocTransformer } = require('@adonisjs/ioc-transformer')
require('ts-node').register({
transformers: {
after: [iocTransformer(require('typescript/lib/typescript'), require('./.adonisrc.json'))],
}
})
```
[gh-workflow-image]: https://img.shields.io/github/workflow/status/adonisjs/ioc-transformer/test?style=for-the-badge
[gh-workflow-url]: https://github.com/adonisjs/ioc-transformer/actions/workflows/test.yml "Github action"
[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript
[typescript-url]: "typescript"
[npm-image]: https://img.shields.io/npm/v/@adonisjs/ioc-transformer.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@adonisjs/ioc-transformer "npm"
[license-image]: https://img.shields.io/npm/l/@adonisjs/ioc-transformer?color=blueviolet&style=for-the-badge
[license-url]: LICENSE.md "license"
+1
View File
@@ -0,0 +1 @@
export { iocTransformer } from './src/transformer';
+13
View File
@@ -0,0 +1,13 @@
"use strict";
/*
* @adonisjs/ioc-transfomer
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.iocTransformer = void 0;
var transformer_1 = require("./src/transformer");
Object.defineProperty(exports, "iocTransformer", { enumerable: true, get: function () { return transformer_1.iocTransformer; } });
@@ -0,0 +1,10 @@
import tsStatic from 'typescript';
/**
* Converts Ioc container import statements to `use` statements
* in compiled Javascript code.
*/
export declare function iocTransformer(ts: typeof tsStatic, rcFile: {
aliases: {
[key: string]: string;
};
}): (ctx: tsStatic.TransformationContext) => (sourceFile: tsStatic.SourceFile) => tsStatic.SourceFile;
+81
View File
@@ -0,0 +1,81 @@
"use strict";
/*
* @adonisjs/ioc-transformer
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.iocTransformer = void 0;
/**
* Find if the node is a require call
*/
function isRequireCall(node, ts) {
return (ts.isCallExpression(node) &&
node.expression &&
ts.isIdentifier(node.expression) &&
node.expression.escapedText === 'require');
}
/**
* Returns the module identifier. Performs checks for dynamic imports as well
*/
function getModuleIdentifier(node, ts) {
const firstArg = node.arguments[0];
let moduleIdentifier = null;
let expressionType = 'unknown';
if (ts.isTemplateExpression(firstArg)) {
moduleIdentifier = firstArg.head.text;
expressionType = 'templateLiteral';
}
else if (ts.isStringLiteral(firstArg)) {
moduleIdentifier = firstArg.text;
expressionType = 'static';
}
else if (ts.isBinaryExpression(firstArg) && ts.isStringLiteral(firstArg.left)) {
moduleIdentifier = firstArg.left.text;
expressionType = 'binary';
}
return { moduleIdentifier, expressionType };
}
/**
* Find if the module indentifier is a container binding
*/
function isContainerBinding(moduleIdentifier) {
return !!moduleIdentifier && moduleIdentifier.startsWith('@ioc:');
}
/**
* Find if the module indentifier is an alias
*/
function isAlias(aliases, moduleIdentifier) {
return !!(moduleIdentifier && aliases.find((alias) => moduleIdentifier.startsWith(`${alias}/`)));
}
/**
* Converts Ioc container import statements to `use` statements
* in compiled Javascript code.
*/
function iocTransformer(ts, rcFile) {
const aliases = Object.keys(rcFile.aliases || {});
return (ctx) => {
return (sourceFile) => {
function visitor(node) {
if (isRequireCall(node, ts)) {
const { moduleIdentifier, expressionType } = getModuleIdentifier(node, ts);
if (isContainerBinding(moduleIdentifier)) {
if (expressionType !== 'static') {
throw new Error('Imports prefixed with "@ioc:" cannot use runtime values');
}
return ts.factory.createCallExpression(ts.factory.createIdentifier("global[Symbol.for('ioc.use')]"), undefined, [ts.factory.createStringLiteral(moduleIdentifier.substr(5))]);
}
if (isAlias(aliases, moduleIdentifier)) {
return ts.factory.createCallExpression(ts.factory.createIdentifier("global[Symbol.for('ioc.use')]"), undefined, node.arguments);
}
}
return ts.visitEachChild(node, visitor, ctx);
}
return ts.visitEachChild(sourceFile, visitor, ctx);
};
};
}
exports.iocTransformer = iocTransformer;
+87
View File
@@ -0,0 +1,87 @@
{
"name": "@adonisjs/ioc-transformer",
"version": "2.3.4",
"description": "Typescript transformer to transform import statements to IoC container use calls",
"main": "build/index.js",
"files": [
"build/src",
"build/index.d.ts",
"build/index.js"
],
"scripts": {
"mrm": "mrm --preset=@adonisjs/mrm-preset",
"pretest": "npm run lint",
"lint": "eslint . --ext=.ts",
"clean": "del build",
"compile": "npm run lint && npm run clean && tsc",
"build": "npm run compile",
"commit": "git-cz",
"release": "np",
"version": "npm run build",
"test": "node japaFile.js",
"prepublishOnly": "npm run build",
"format": "prettier --write .",
"sync-labels": "github-label-sync --labels ./node_modules/@adonisjs/mrm-preset/gh-labels.json adonisjs/ioc-transformer"
},
"repository": {
"type": "git",
"url": "git+https://github.com/adonisjs/ioc-transformer.git"
},
"keywords": [
"ioc",
"adonisjs",
"typescript",
"ts-plugin"
],
"author": "virk,adonisjs",
"license": "MIT",
"bugs": {
"url": "https://github.com/adonisjs/ioc-transformer/issues"
},
"homepage": "https://github.com/adonisjs/ioc-transformer#readme",
"devDependencies": {
"@adonisjs/mrm-preset": "^5.0.3",
"@adonisjs/require-ts": "^2.0.11",
"@types/node": "^17.0.35",
"del-cli": "^4.0.1",
"doctoc": "^2.2.0",
"eslint": "^8.16.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-adonis": "^2.1.0",
"eslint-plugin-prettier": "^4.0.0",
"github-label-sync": "^2.2.0",
"husky": "^8.0.1",
"japa": "^4.0.0",
"mrm": "^4.0.0",
"np": "^7.6.1",
"prettier": "^2.6.2",
"typescript": "^4.6.4"
},
"nyc": {
"exclude": [
"test"
],
"extension": [
".ts"
]
},
"husky": {
"hooks": {
"pre-commit": "doctoc README.md --title='## Table of contents' && git add README.md",
"commit-msg": "node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js"
}
},
"config": {
"commitizen": {
"path": "cz-conventional-changelog"
}
},
"np": {
"contents": ".",
"anyBranch": false
},
"publishConfig": {
"access": "public",
"tag": "latest"
}
}