Comparing version 3.0.0-alpha.6 to 3.0.0-alpha.7
@@ -8,6 +8,21 @@ import { SymbolSpec } from './SymbolSpecs'; | ||
constructor(literals: TemplateStringsArray, placeholders: any[]); | ||
toStringWithImports(path?: string): string; | ||
/** Returns the formatted code, without any imports. */ | ||
/** | ||
* Returns the code with any necessary import statements prefixed. | ||
* | ||
* `path` is the intended file name of this code; it is used to know whether we | ||
* can skip import statements that would important from our own file. | ||
* | ||
* This method will also use any local `.prettierrc` settings, hence needs | ||
* to return a `Promise<String>`. | ||
*/ | ||
toStringWithImports(path?: string): Promise<string>; | ||
/** | ||
* Returns the formatted code, without any imports. | ||
* | ||
* Note that we don't use `.prettierrc` b/c that requires async I/O to resolve. | ||
*/ | ||
toString(): string; | ||
private deepFindImports; | ||
private generateCode; | ||
} | ||
export declare function imp(spec: string): SymbolSpec; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; | ||
result["default"] = mod; | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const prettier_1 = __importDefault(require("prettier")); | ||
const prettier_1 = __importStar(require("prettier")); | ||
const SymbolSpecs_1 = require("./SymbolSpecs"); | ||
@@ -18,3 +22,26 @@ /** A template literal to format code and auto-organize imports. */ | ||
} | ||
/** | ||
* Returns the code with any necessary import statements prefixed. | ||
* | ||
* `path` is the intended file name of this code; it is used to know whether we | ||
* can skip import statements that would important from our own file. | ||
* | ||
* This method will also use any local `.prettierrc` settings, hence needs | ||
* to return a `Promise<String>`. | ||
*/ | ||
toStringWithImports(path) { | ||
const imports = this.deepFindImports(); | ||
const importPart = SymbolSpecs_1.emitImports(imports, path || ''); | ||
const bodyPart = this.generateCode(); | ||
return maybePrettyWithConfig(importPart + '\n' + bodyPart); | ||
} | ||
/** | ||
* Returns the formatted code, without any imports. | ||
* | ||
* Note that we don't use `.prettierrc` b/c that requires async I/O to resolve. | ||
*/ | ||
toString() { | ||
return maybePretty(this.generateCode()); | ||
} | ||
deepFindImports() { | ||
const imports = []; | ||
@@ -34,8 +61,5 @@ let toScan = [...this.placeholders]; | ||
} | ||
const importPart = SymbolSpecs_1.emitImports(imports, path || ''); | ||
const bodyPart = this.toString(); | ||
return maybePretty(importPart + '\n' + bodyPart); | ||
return imports; | ||
} | ||
/** Returns the formatted code, without any imports. */ | ||
toString() { | ||
generateCode() { | ||
const { literals, placeholders } = this; | ||
@@ -69,6 +93,16 @@ let result = ''; | ||
result += literals[literals.length - 1]; | ||
return maybePretty(result); | ||
return result; | ||
} | ||
} | ||
exports.Code = Code; | ||
const configPromise = prettier_1.resolveConfig('./'); | ||
async function maybePrettyWithConfig(input) { | ||
try { | ||
const config = await configPromise; | ||
return prettier_1.default.format(input.trim(), { parser: 'typescript', ...config }); | ||
} | ||
catch (e) { | ||
return input; // assume it's invalid syntax and ignore | ||
} | ||
} | ||
function maybePretty(input) { | ||
@@ -75,0 +109,0 @@ try { |
{ | ||
"name": "ts-poet", | ||
"version": "3.0.0-alpha.6", | ||
"version": "3.0.0-alpha.7", | ||
"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
79985
926