Comparing version 3.2.0 to 3.2.1
@@ -29,6 +29,7 @@ "use strict"; | ||
toStringWithImports(path) { | ||
const ourModulePath = (path || '').replace(/\.[tj]sx?/, ''); | ||
const imports = this.deepFindImports(); | ||
const defs = this.deepFindDefs(); | ||
assignAliasesIfNeeded(defs, imports, path || ''); | ||
const importPart = SymbolSpecs_1.emitImports(imports, path || ''); | ||
assignAliasesIfNeeded(defs, imports, ourModulePath); | ||
const importPart = SymbolSpecs_1.emitImports(imports, ourModulePath); | ||
const bodyPart = this.generateCode(); | ||
@@ -133,3 +134,3 @@ return maybePrettyWithConfig(importPart + '\n' + bodyPart); | ||
/** Finds any namespace collisions of a named import colliding with def and assigns the import an alias it. */ | ||
function assignAliasesIfNeeded(defs, imports, path) { | ||
function assignAliasesIfNeeded(defs, imports, ourModulePath) { | ||
const defNames = new Set(); | ||
@@ -141,3 +142,4 @@ defs.forEach((def) => defNames.add(def.symbol)); | ||
imports.forEach((i) => { | ||
if (i instanceof SymbolSpecs_1.ImportsName && !(i.source === path || i.definedIn === path)) { | ||
if (i instanceof SymbolSpecs_1.ImportsName && | ||
!(SymbolSpecs_1.sameModule(i.source, ourModulePath) || (i.definedIn && SymbolSpecs_1.sameModule(i.definedIn, ourModulePath)))) { | ||
if (defNames.has(i.value)) { | ||
@@ -144,0 +146,0 @@ // Look for an existing alias |
@@ -187,3 +187,5 @@ import { Node } from './Node'; | ||
/** Generates the `import ...` lines for the given `imports`. */ | ||
export declare function emitImports(imports: SymbolSpec[], filePath: string): string; | ||
export declare function emitImports(imports: SymbolSpec[], ourModulePath: string): string; | ||
export declare function maybeRelativePath(outputPath: string, importPath: string): string; | ||
/** Checks if `path1 === path2` despite minor path differences like `./foo` and `foo`. */ | ||
export declare function sameModule(path1: string, path2: string): boolean; |
@@ -258,3 +258,3 @@ "use strict"; | ||
/** Generates the `import ...` lines for the given `imports`. */ | ||
function emitImports(imports, filePath) { | ||
function emitImports(imports, ourModulePath) { | ||
if (imports.length == 0) { | ||
@@ -264,10 +264,10 @@ return ''; | ||
let result = ''; | ||
const ourModulePath = filePath.replace(/\.[tj]sx?/, ''); | ||
const augmentImports = lodash_1.default.groupBy(filterInstances(imports, Augmented), (a) => a.augmented); | ||
// Group the imports by source module they're imported from | ||
const importsByModule = lodash_1.default.groupBy(imports.filter((it) => it.source !== undefined && !(it instanceof ImportsName && it.definedIn === ourModulePath)), (it) => it.source); | ||
const importsByModule = lodash_1.default.groupBy(imports.filter((it) => it.source !== undefined && | ||
!(it instanceof ImportsName && it.definedIn && sameModule(it.definedIn, ourModulePath))), (it) => it.source); | ||
// Output each source module as one line | ||
Object.entries(importsByModule).forEach(([modulePath, imports]) => { | ||
// Skip imports from the current module | ||
if (ourModulePath === modulePath || path_1.default.resolve(ourModulePath) === path_1.default.resolve(modulePath)) { | ||
if (sameModule(ourModulePath, modulePath)) { | ||
return; | ||
@@ -328,1 +328,6 @@ } | ||
exports.maybeRelativePath = maybeRelativePath; | ||
/** Checks if `path1 === path2` despite minor path differences like `./foo` and `foo`. */ | ||
function sameModule(path1, path2) { | ||
return path1 === path2 || path_1.default.resolve(path1) === path_1.default.resolve(path2); | ||
} | ||
exports.sameModule = sameModule; |
{ | ||
"name": "ts-poet", | ||
"version": "3.2.0", | ||
"version": "3.2.1", | ||
"description": "code generation DSL for TypeScript", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
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
228659
4880