@adonisjs/ioc-transformer
Advanced tools
Comparing version 2.2.1 to 2.3.0
@@ -9,3 +9,3 @@ "use strict"; | ||
* file that was distributed with this source code. | ||
*/ | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -12,0 +12,0 @@ exports.iocTransformer = void 0; |
"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. | ||
*/ | ||
* @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 | ||
@@ -21,12 +63,12 @@ * in compiled Javascript code. | ||
function visitor(node) { | ||
if (ts.isCallExpression(node) | ||
&& node.expression | ||
&& ts.isIdentifier(node.expression) | ||
&& node.expression.escapedText === 'require') { | ||
const moduleName = node.arguments[0].text; | ||
if (moduleName && moduleName.startsWith('@ioc:')) { | ||
return ts.factory.createCallExpression(ts.factory.createIdentifier('global[Symbol.for(\'ioc.use\')]'), undefined, [ts.factory.createStringLiteral(moduleName.substr(5))]); | ||
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 (moduleName && aliases.find((alias) => moduleName.startsWith(`${alias}/`))) { | ||
return ts.factory.createCallExpression(ts.factory.createIdentifier('global[Symbol.for(\'ioc.use\')]'), undefined, [ts.factory.createStringLiteral(moduleName)]); | ||
if (isAlias(aliases, moduleIdentifier)) { | ||
return ts.factory.createCallExpression(ts.factory.createIdentifier("global[Symbol.for('ioc.use')]"), undefined, node.arguments); | ||
} | ||
@@ -33,0 +75,0 @@ } |
# The MIT License | ||
Copyright 2019 Harminder virk, contributors | ||
Copyright 2021 Harminder Virk, contributors | ||
@@ -5,0 +5,0 @@ 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: |
{ | ||
"name": "@adonisjs/ioc-transformer", | ||
"version": "2.2.1", | ||
"version": "2.3.0", | ||
"description": "Typescript transformer to transform import statements to IoC container use calls", | ||
@@ -20,3 +20,7 @@ "main": "build/index.js", | ||
"release": "np", | ||
"version": "npm run build" | ||
"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" | ||
}, | ||
@@ -40,15 +44,18 @@ "repository": { | ||
"devDependencies": { | ||
"@adonisjs/mrm-preset": "^3.0.0", | ||
"@types/node": "^15.0.1", | ||
"commitizen": "^4.2.3", | ||
"cz-conventional-changelog": "^3.3.0", | ||
"@adonisjs/mrm-preset": "^4.1.0", | ||
"@adonisjs/require-ts": "^2.0.4", | ||
"@types/node": "^15.9.0", | ||
"del-cli": "^3.0.1", | ||
"doctoc": "^2.0.0", | ||
"eslint": "^7.25.0", | ||
"eslint": "^7.27.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-adonis": "^1.3.1", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"github-label-sync": "^2.0.0", | ||
"husky": "^6.0.0", | ||
"japa": "^3.1.1", | ||
"mrm": "^3.0.1", | ||
"mrm": "^3.0.2", | ||
"np": "^7.5.0", | ||
"typescript": "^4.2.4" | ||
"prettier": "^2.3.0", | ||
"typescript": "^4.3.2" | ||
}, | ||
@@ -76,6 +83,4 @@ "nyc": { | ||
"contents": ".", | ||
"anyBranch": false, | ||
"tests": false | ||
"anyBranch": false | ||
}, | ||
"dependencies": {}, | ||
"publishConfig": { | ||
@@ -82,0 +87,0 @@ "access": "public", |
@@ -6,3 +6,3 @@ <div align="center"><img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1564392111/adonis-banner_o9lunk.png" width="600px"></div> | ||
[![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] | ||
[![gh-workflow-image]][gh-workflow-url] [![npm-image]][npm-url] ![][typescript-image] [![license-image]][license-url] | ||
@@ -38,2 +38,5 @@ 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. | ||
[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 | ||
@@ -40,0 +43,0 @@ [typescript-url]: "typescript" |
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
9435
105
48
16