Comparing version 4.5.0 to 4.6.0
@@ -72,4 +72,5 @@ import { Node } from './Node'; | ||
* @param from The module the symbol is exported from | ||
* @param typeImport whether this is an `import type` import | ||
*/ | ||
static importsName(exportedName: string, from: string): Import; | ||
static importsName(exportedName: string, from: string, typeImport: boolean): Import; | ||
/** | ||
@@ -138,2 +139,3 @@ * Creates a symbol that is brought in by a whole module import | ||
sourceSymbol?: string | undefined; | ||
typeImport?: boolean | undefined; | ||
/** | ||
@@ -143,4 +145,5 @@ * @param symbol | ||
* @param sourceSymbol is the optional original symbol, i.e if we're renaming the symbol it is `Engine` | ||
* @param typeImport whether this is an `import type` import | ||
*/ | ||
constructor(symbol: string, source: string, sourceSymbol?: string | undefined); | ||
constructor(symbol: string, source: string, sourceSymbol?: string | undefined, typeImport?: boolean | undefined); | ||
toImportPiece(): string; | ||
@@ -147,0 +150,0 @@ } |
@@ -10,2 +10,3 @@ "use strict"; | ||
const Node_1 = require("./Node"); | ||
const typeImportMarker = '(?:t:)?'; | ||
const fileNamePattern = '(?:[a-zA-Z0-9._-]+)'; | ||
@@ -15,3 +16,3 @@ const modulePattern = `@?(?:(?:${fileNamePattern}(?:/${fileNamePattern})*))`; | ||
exports.importType = '[*@+=]'; | ||
const importPattern = `^(${identPattern})?(${exports.importType})(${modulePattern})(?:#(${identPattern}))?`; | ||
const importPattern = `^(${typeImportMarker}${identPattern})?(${exports.importType})(${modulePattern})(?:#(${identPattern}))?`; | ||
/** | ||
@@ -62,10 +63,15 @@ * Specifies a symbol and its related origin, either via import or implicit/local declaration. | ||
const modulePath = matched[3]; | ||
const type = matched[2] || '@'; | ||
const kind = matched[2] || '@'; | ||
const symbolName = matched[1] || lodash_1.default.last(modulePath.split('/')) || ''; | ||
const targetName = matched[4]; | ||
switch (type) { | ||
switch (kind) { | ||
case '*': | ||
return Import.importsAll(symbolName, modulePath); | ||
case '@': | ||
return Import.importsName(symbolName, modulePath); | ||
if (symbolName.startsWith('t:')) { | ||
return Import.importsName(symbolName.substring(2), modulePath, true); | ||
} | ||
else { | ||
return Import.importsName(symbolName, modulePath, false); | ||
} | ||
case '=': | ||
@@ -78,3 +84,3 @@ return Import.importsDefault(symbolName, modulePath); | ||
default: | ||
throw new Error('Invalid type character'); | ||
throw new Error('Invalid import kind character'); | ||
} | ||
@@ -113,5 +119,6 @@ } | ||
* @param from The module the symbol is exported from | ||
* @param typeImport whether this is an `import type` import | ||
*/ | ||
static importsName(exportedName, from) { | ||
return new ImportsName(exportedName, from); | ||
static importsName(exportedName, from, typeImport) { | ||
return new ImportsName(exportedName, from, undefined, typeImport); | ||
} | ||
@@ -199,6 +206,8 @@ /** | ||
* @param sourceSymbol is the optional original symbol, i.e if we're renaming the symbol it is `Engine` | ||
* @param typeImport whether this is an `import type` import | ||
*/ | ||
constructor(symbol, source, sourceSymbol) { | ||
constructor(symbol, source, sourceSymbol, typeImport) { | ||
super(symbol, source); | ||
this.sourceSymbol = sourceSymbol; | ||
this.typeImport = typeImport; | ||
} | ||
@@ -267,2 +276,3 @@ toImportPiece() { | ||
const importsByModule = lodash_1.default.groupBy(imports.filter((it) => it.source !== undefined && | ||
// Ignore imports that are in our own file | ||
!(it instanceof ImportsName && it.definedIn && sameModule(it.definedIn, ourModulePath))), (it) => it.source); | ||
@@ -284,5 +294,7 @@ // Output each source module as one line | ||
}); | ||
// Partition named imported into `import type` vs. regular imports | ||
const allNames = filterInstances(imports, ImportsName); | ||
const names = unique(allNames.filter((i) => !i.typeImport).map((it) => it.toImportPiece())); | ||
const def = unique(filterInstances(imports, ImportsDefault).map((it) => it.symbol)); | ||
// Output named imports as a group | ||
const names = unique(filterInstances(imports, ImportsName).map((it) => it.toImportPiece())); | ||
const def = unique(filterInstances(imports, ImportsDefault).map((it) => it.symbol)); | ||
if (names.length > 0 || def.length > 0) { | ||
@@ -299,2 +311,6 @@ const namesPart = names.length > 0 ? [`{ ${names.join(', ')} }`] : []; | ||
} | ||
const typeImports = unique(allNames.filter((i) => i.typeImport).map((it) => it.toImportPiece())); | ||
if (typeImports.length > 0) { | ||
result += `import type { ${typeImports.join(', ')} } from '${importPath}';\n`; | ||
} | ||
}); | ||
@@ -301,0 +317,0 @@ const sideEffectImports = lodash_1.default.groupBy(filterInstances(imports, SideEffect), (a) => a.source); |
@@ -5,3 +5,3 @@ module.exports = { | ||
'ts-jest': { | ||
tsConfig: 'tsconfig.json', | ||
tsconfig: 'tsconfig.json', | ||
}, | ||
@@ -8,0 +8,0 @@ }, |
{ | ||
"name": "ts-poet", | ||
"version": "4.5.0", | ||
"version": "4.6.0", | ||
"description": "code generation DSL for TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -69,2 +69,3 @@ ![npm](https://img.shields.io/npm/v/ts-poet) | ||
* `imp("Observable@rxjs")` --> `import { Observable } from "rxjs"` | ||
* `imp("t:Observable@rxjs")` --> `import type { Observable } from "rxjs"` | ||
* `imp("Observable@./Api")` --> `import { Observable } from "./Api"` | ||
@@ -71,0 +72,0 @@ * `imp("Observable*./Api")` --> `import * as Observable from "./Api"` |
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
103889
1911
145