Comparing version 2.1.0 to 3.0.0-alpha.0
@@ -1,13 +0,11 @@ | ||
export { ClassSpec } from './ClassSpec'; | ||
export { CodeBlock } from './CodeBlock'; | ||
export { DecoratorSpec } from './DecoratorSpec'; | ||
export { EnumSpec } from './EnumSpec'; | ||
export { FileSpec } from './FileSpec'; | ||
export { FunctionSpec } from './FunctionSpec'; | ||
export { InterfaceSpec } from './InterfaceSpec'; | ||
export { Modifier } from './Modifier'; | ||
export { ParameterSpec } from './ParameterSpec'; | ||
export { PropertySpec } from './PropertySpec'; | ||
export { SymbolSpecs } from './SymbolSpecs'; | ||
export { TypeAliasSpec } from './TypeAliasSpec'; | ||
export * from './TypeNames'; | ||
import { SymbolSpec } from '@src/SymbolSpecs'; | ||
declare class Code { | ||
private literals; | ||
private placeholders; | ||
constructor(literals: TemplateStringsArray, placeholders: any[]); | ||
toStringWithImports(): string; | ||
toString(): string; | ||
} | ||
export declare function imp(spec: string): SymbolSpec; | ||
export declare function code(literals: TemplateStringsArray, ...placeholders: any[]): Code; | ||
export {}; |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const prettier_1 = __importDefault(require("prettier")); | ||
const SymbolSpecs_1 = require("@src/SymbolSpecs"); | ||
class Code { | ||
constructor(literals, placeholders) { | ||
this.literals = literals; | ||
this.placeholders = placeholders; | ||
} | ||
toStringWithImports() { | ||
const imports = []; | ||
let toScan = [...this.placeholders]; | ||
while (toScan.length > 0) { | ||
const placeholder = toScan.pop(); | ||
if (placeholder instanceof SymbolSpecs_1.SymbolSpec) { | ||
imports.push(placeholder); | ||
} | ||
else if (placeholder instanceof Code) { | ||
toScan = [...toScan, ...placeholder.placeholders]; | ||
} | ||
} | ||
const importPart = SymbolSpecs_1.emitImports(imports, ''); | ||
const bodyPart = this.toString(); | ||
return maybePretty(importPart + '\n' + bodyPart); | ||
} | ||
toString() { | ||
const { literals, placeholders } = this; | ||
let result = ''; | ||
// interleave the literals with the placeholders | ||
for (let i = 0; i < placeholders.length; i++) { | ||
const [literal, placeholder] = [literals[i], placeholders[i]]; | ||
result += literal; | ||
if (placeholder instanceof SymbolSpecs_1.SymbolSpec) { | ||
result += placeholder.value; | ||
} | ||
else { | ||
result += placeholder.toString(); | ||
} | ||
} | ||
// add the last literal | ||
result += literals[literals.length - 1]; | ||
return maybePretty(result); | ||
} | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ClassSpec_1 = require("./ClassSpec"); | ||
exports.ClassSpec = ClassSpec_1.ClassSpec; | ||
var CodeBlock_1 = require("./CodeBlock"); | ||
exports.CodeBlock = CodeBlock_1.CodeBlock; | ||
var DecoratorSpec_1 = require("./DecoratorSpec"); | ||
exports.DecoratorSpec = DecoratorSpec_1.DecoratorSpec; | ||
var EnumSpec_1 = require("./EnumSpec"); | ||
exports.EnumSpec = EnumSpec_1.EnumSpec; | ||
var FileSpec_1 = require("./FileSpec"); | ||
exports.FileSpec = FileSpec_1.FileSpec; | ||
var FunctionSpec_1 = require("./FunctionSpec"); | ||
exports.FunctionSpec = FunctionSpec_1.FunctionSpec; | ||
var InterfaceSpec_1 = require("./InterfaceSpec"); | ||
exports.InterfaceSpec = InterfaceSpec_1.InterfaceSpec; | ||
var Modifier_1 = require("./Modifier"); | ||
exports.Modifier = Modifier_1.Modifier; | ||
var ParameterSpec_1 = require("./ParameterSpec"); | ||
exports.ParameterSpec = ParameterSpec_1.ParameterSpec; | ||
var PropertySpec_1 = require("./PropertySpec"); | ||
exports.PropertySpec = PropertySpec_1.PropertySpec; | ||
var SymbolSpecs_1 = require("./SymbolSpecs"); | ||
exports.SymbolSpecs = SymbolSpecs_1.SymbolSpecs; | ||
var TypeAliasSpec_1 = require("./TypeAliasSpec"); | ||
exports.TypeAliasSpec = TypeAliasSpec_1.TypeAliasSpec; | ||
__export(require("./TypeNames")); | ||
function maybePretty(input) { | ||
try { | ||
return prettier_1.default.format(input.trim(), { parser: 'babel' }); | ||
} | ||
catch (e) { | ||
return input; // assume it's invalid syntax and ignore | ||
} | ||
} | ||
function imp(spec) { | ||
return SymbolSpecs_1.SymbolSpec.from(spec); | ||
} | ||
exports.imp = imp; | ||
function code(literals, ...placeholders) { | ||
return new Code(literals, placeholders); | ||
} | ||
exports.code = code; |
@@ -1,2 +0,1 @@ | ||
import { SymbolReferenceTracker } from './SymbolReferenceTracker'; | ||
export declare const moduleSeparator = "[*@+=]"; | ||
@@ -8,3 +7,3 @@ /** | ||
*/ | ||
export declare class SymbolSpec { | ||
export declare abstract class SymbolSpec { | ||
value: string; | ||
@@ -54,3 +53,3 @@ /** | ||
protected constructor(value: string); | ||
reference(trackedBy?: SymbolReferenceTracker): string; | ||
abstract source: string | undefined; | ||
} | ||
@@ -121,10 +120,9 @@ export declare class SymbolSpecs { | ||
constructor(value: string); | ||
reference(): string; | ||
source: undefined; | ||
} | ||
/** | ||
* Common base class for imported symbols | ||
*/ | ||
/** Common base class for imported symbols. */ | ||
export declare abstract class Imported extends SymbolSpec { | ||
value: string; | ||
source: string; | ||
/** The value is the imported symbol, i.e. `BarClass`, and source is the path it comes from. */ | ||
protected constructor(value: string, source: string); | ||
@@ -178,1 +176,2 @@ } | ||
} | ||
export declare function emitImports(imports: SymbolSpec[], path: string): string; |
@@ -88,8 +88,2 @@ "use strict"; | ||
} | ||
reference(trackedBy) { | ||
if (trackedBy) { | ||
trackedBy.referenced(this); | ||
} | ||
return this.value; | ||
} | ||
} | ||
@@ -175,12 +169,9 @@ exports.SymbolSpec = SymbolSpec; | ||
super(value); | ||
this.source = undefined; | ||
} | ||
reference() { | ||
return this.value; | ||
} | ||
} | ||
exports.Implicit = Implicit; | ||
/** | ||
* Common base class for imported symbols | ||
*/ | ||
/** Common base class for imported symbols. */ | ||
class Imported extends SymbolSpec { | ||
/** The value is the imported symbol, i.e. `BarClass`, and source is the path it comes from. */ | ||
constructor(value, source) { | ||
@@ -254,1 +245,63 @@ super(source); | ||
exports.SideEffect = SideEffect; | ||
function emitImports(imports, path) { | ||
if (imports.length == 0) { | ||
return ''; | ||
} | ||
let result = ''; | ||
const augmentImports = lodash_1.default.groupBy(filterInstances(imports, Augmented), a => a.augmented); | ||
const sideEffectImports = lodash_1.default.groupBy(filterInstances(imports, SideEffect), a => a.source); | ||
// FileModules.importPath(this.path, it.source)).toSortedMap() | ||
const m = lodash_1.default.groupBy(imports.filter(it => it.source !== undefined), it => it.source); | ||
Object.entries(m).forEach(([sourceImportPath, imports]) => { | ||
// Skip imports from the current module | ||
// if (path === sourceImportPath || Path.resolve(path) === Path.resolve(sourceImportPath)) { | ||
// return; | ||
// } | ||
const importPath = maybeRelativePath(path, sourceImportPath); | ||
// Output star imports individually | ||
filterInstances(imports, ImportsAll).forEach(i => { | ||
result += `import * as ${i.value} from '${importPath}';\n`; | ||
const augments = augmentImports[i.value]; | ||
if (augments) { | ||
augments.forEach(augment => (result += `import '${augment.source}';\n`)); | ||
} | ||
}); | ||
// Output named imports as a group | ||
const names = unique(filterInstances(imports, ImportsName).map(it => it.value)); | ||
const def = unique(filterInstances(imports, ImportsDefault).map(it => it.value)); | ||
if (names.length > 0 || def.length > 0) { | ||
const namesPart = names.length > 0 ? [`{ ${names.join(', ')} }`] : []; | ||
const defPart = def.length > 0 ? [def[0]] : []; | ||
const allNames = [...defPart, ...namesPart]; | ||
result += `import ${allNames.join(', ')} from '${importPath}';\n`; | ||
[...names, ...def].forEach(name => { | ||
const augments = augmentImports[name]; | ||
if (augments) { | ||
augments.forEach(augment => (result += `import '${augment.source}';\n`)); | ||
} | ||
}); | ||
} | ||
}); | ||
Object.keys(sideEffectImports).forEach(it => (result += `import '${it}';\n`)); | ||
return result; | ||
} | ||
exports.emitImports = emitImports; | ||
function filterInstances(list, t) { | ||
return list.filter(e => e instanceof t); | ||
} | ||
function unique(list) { | ||
return [...new Set(list)]; | ||
} | ||
function maybeRelativePath(outputPath, importPath) { | ||
if (!importPath.startsWith('./')) { | ||
return importPath; | ||
} | ||
// Ideally we'd use a path library to do this | ||
const dirs = outputPath.split('').filter(l => l === '/').length; | ||
if (dirs === 0) { | ||
return importPath; | ||
} | ||
const a = new Array(dirs); | ||
const prefix = a.fill('..', 0, dirs).join('/'); | ||
return prefix + importPath.substring(1); | ||
} |
@@ -13,3 +13,3 @@ module.exports = { | ||
testEnvironment: 'node', | ||
testMatch: ['<rootDir>/tests/**/*-tests.+(ts|tsx|js)'], | ||
testMatch: ['<rootDir>/**/*-tests.+(ts|tsx|js)'], | ||
transform: { | ||
@@ -16,0 +16,0 @@ '^.+\\.(ts|tsx)$': 'ts-jest', |
{ | ||
"name": "ts-poet", | ||
"version": "2.1.0", | ||
"version": "3.0.0-alpha.0", | ||
"description": "code generation DSL for TypeScript", | ||
@@ -26,11 +26,11 @@ "main": "build/index.js", | ||
"jest": "^24.9.0", | ||
"prettier": "^1.18.2", | ||
"ts-jest": "^24.0.2", | ||
"tslint-immutable": "^6.0.1", | ||
"typescript": "^3.5.3" | ||
"typescript": "^3.7.4" | ||
}, | ||
"dependencies": { | ||
"@types/prettier": "^1.19.0", | ||
"lodash": "^4.17.15", | ||
"ts-imm": "0.4.0" | ||
"prettier": "^1.18.2" | ||
} | ||
} |
@@ -43,5 +43,7 @@ | ||
```typescript | ||
import { TypeNames, ClassSpec, TypeName, Modifier, FunctionSpec } from "ts-poet"; | ||
const observableTypeName = TypeNames.importedType("@rxjs/Observable") | ||
val testClass = ClassSpec.create("Greeter") | ||
const testClass = ClassSpec.create("Greeter") | ||
.addProperty("name", TypeName.STRING, false, Modifier.PRIVATE) | ||
@@ -48,0 +50,0 @@ .constructor( |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
12
71
77252
3
23
855
2
1
+ Added@types/prettier@^1.19.0
+ Addedprettier@^1.18.2
+ Added@types/prettier@1.19.1(transitive)
+ Addedprettier@1.19.1(transitive)
- Removedts-imm@0.4.0
- Removedts-imm@0.4.0(transitive)