Skip to content

Commit

Permalink
feat: #148, #320 - Add WriterFunctions.object.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Oct 13, 2018
1 parent 8f11b1a commit dab4cfb
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/ts-simple-ast.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,20 @@ export declare class Project {
* @param globPatterns - Glob patterns for filtering out the source files.
*/
getSourceFiles(globPatterns: ReadonlyArray<string>): SourceFile[];
/**
* Gets the specified ambient module symbol or returns undefined if not found.
* @param moduleName - The ambient module name with or without quotes.
*/
getAmbientModule(moduleName: string): Symbol | undefined;
/**
* Gets the specified ambient module symbol or throws if not found.
* @param moduleName - The ambient module name with or without quotes.
*/
getAmbientModuleOrThrow(moduleName: string): Symbol;
/**
* Gets the ambient module symbols (ex. modules in the @types folder or node_modules).
*/
getAmbientModules(): Symbol[];
/**
* Saves all the unsaved source files to the file system and deletes all deleted files.
*/
Expand Down Expand Up @@ -1786,6 +1800,18 @@ export declare class TypeGuards {
static isYieldExpression(node: Node): node is YieldExpression;
}

/** Functions for writing. */
export declare class WriterFunctions {
private constructor();
/**
* Gets a writer function for writing the provided object as an object literal expression.
* @param obj - Object to write.
*/
static object(obj: {
[key: string]: string | number | WriterFunction | undefined;
}): WriterFunction;
}

export declare type PropertyName = Identifier | StringLiteral | NumericLiteral | ComputedPropertyName;

export declare type AccessorDeclaration = GetAccessorDeclaration | SetAccessorDeclaration;
Expand Down Expand Up @@ -9192,6 +9218,10 @@ export declare class TypeChecker {
* Gets the compiler's TypeChecker.
*/
readonly compilerObject: ts.TypeChecker;
/**
* Gets the ambient module symbols (ex. modules in the @types folder or node_modules).
*/
getAmbientModules(): Symbol[];
/**
* Gets the apparent type of a type.
* @param type - Type to get the apparent type of.
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export { printNode, PrintNodeOptions } from "./utils/compiler/printNode";
export { SourceFileReferencingNodes } from "./utils/references/SourceFileReferenceContainer";
export { CompilerOptionsFromTsConfigOptions, CompilerOptionsFromTsConfigResult, getCompilerOptionsFromTsConfig } from "./utils/tsconfig/getCompilerOptionsFromTsConfig";
export { TypeGuards } from "./utils/TypeGuards";
export { WriterFunctions } from "./utils/WriterFunctions";
export * from "./typescript/public";
42 changes: 42 additions & 0 deletions src/tests/utils/writerFunctionsTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from "chai";
import { CodeBlockWriter } from "../../codeBlockWriter";
import { WriterFunction } from "../../types";
import { WriterFunctions } from "../../utils";

describe(nameof(WriterFunctions), () => {
function getWriter() {
return new CodeBlockWriter();
}

function doWriterTest(action: (writer: CodeBlockWriter) => void, expected: string) {
const writer = getWriter();
action(writer);
expect(writer.toString()).to.equal(expected);
}

describe(nameof(WriterFunctions.object), () => {
function doTest(obj: { [key: string]: string | number | WriterFunction | undefined; }, expected: string) {
doWriterTest(writer => WriterFunctions.object(obj)(writer), expected);
}

it("should write an object with keys", () => {
doTest({
key1: "'testing'",
key2: 5,
key3: undefined,
key4: writer => writer.write("6"),
key5: "undefined"
}, `{
key1: 'testing',
key2: 5,
key3,
key4: 6,
key5: undefined
}`);
});

it("should write an object without keys on the same line", () => {
doTest({}, `{}`);
});
});
});
45 changes: 45 additions & 0 deletions src/utils/WriterFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { CodeBlockWriter } from "../codeBlockWriter";
import { WriterFunction } from "../types";

/** Functions for writing. */
export class WriterFunctions {
private constructor() {
}

/**
* Gets a writer function for writing the provided object as an object literal expression.
* @param obj - Object to write.
*/
static object(obj: { [key: string]: string | number | WriterFunction | undefined; }): WriterFunction {
return (writer: CodeBlockWriter) => {
const keyNames = Object.keys(obj);
writer.write("{");
if (keyNames.length > 0) {
writer.indentBlock(() => {
writeObject();
});
}
writer.write("}");

function writeObject() {
for (let i = 0; i < keyNames.length; i++) {
if (i > 0)
writer.write(",").newLine();

const keyName = keyNames[i];
const value = obj[keyName];
writer.write(keyName);
if (value != null) {
writer.write(": ");
if (value instanceof Function)
value(writer);
else
writer.write(value.toString());
}
}

writer.newLine();
}
};
}
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from "./setValueIfUndefined";
export * from "./StringUtils";
export * from "./tsconfig";
export * from "./TypeGuards";
export * from "./WriterFunctions";

0 comments on commit dab4cfb

Please sign in to comment.