Skip to content

Commit

Permalink
refactor: Renamed QuoteType to QuoteKind.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: QuoteType is now QuoteKind.

This was done to make it consistent with NewLineKind.
  • Loading branch information
dsherret committed Mar 26, 2018
1 parent 76ce4ad commit 964571a
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 37 deletions.
10 changes: 5 additions & 5 deletions docs/manipulation/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ title: Manipulation Settings
The manipulation settings can be set when creating the main AST object:

```ts
import Project, {QuoteType, NewLineKind, IndentationText} from "ts-simple-ast";
import Project, {QuoteKind, NewLineKind, IndentationText} from "ts-simple-ast";

const project = new Project({
// these are the defaults
Expand All @@ -17,7 +17,7 @@ const project = new Project({
// LineFeed or CarriageReturnLineFeed
newLineKind: NewLineKind.LineFeed,
// Single or Double
quoteType: QuoteType.Double
quoteKind: QuoteKind.Double
}
});
```
Expand All @@ -37,7 +37,7 @@ Get more details about the settings by looking at the `manipulationSettings` pro
```ts
project.manipulationSettings.getIndentationText();
project.manipulationSettings.getNewLineKind();
project.manipulationSettings.getQuoteType();
project.manipulationSettings.getQuoteKind();
```

### Updating
Expand All @@ -46,11 +46,11 @@ You can update these settings later if you wish by using the `set` method:

```ts
// set only one
project.manipulationSettings.set({ quoteType: QuoteType.Single });
project.manipulationSettings.set({ quoteKind: QuoteKind.Single });

// or multiple
project.manipulationSettings.set({
quoteType: QuoteType.Single,
quoteKind: QuoteKind.Single,
indentationText: IndentationText.TwoSpaces
});
```
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {FunctionDeclaration} from "../function";
import {FormatCodeSettings} from "../tools";
import {TypeAliasDeclaration, Type} from "../type";
import {InterfaceDeclaration} from "../interface";
import {QuoteType} from "../literal/QuoteType";
import {QuoteKind} from "../literal/QuoteKind";
import {NamespaceDeclaration} from "../namespace";
import {Statement, StatementedNode} from "../statement";
import {KindToNodeMappings} from "../kindToNodeMappings";
Expand Down Expand Up @@ -1256,7 +1256,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
newLine: this.global.manipulationSettings.getNewLineKindAsString(),
indentNumberOfSpaces: indentationText === IndentationText.Tab ? undefined : indentationText.length,
useTabs: indentationText === IndentationText.Tab,
useSingleQuote: this.global.manipulationSettings.getQuoteType() === QuoteType.Single
useSingleQuote: this.global.manipulationSettings.getQuoteKind() === QuoteKind.Single
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/compiler/enum/EnumMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export class EnumMember extends EnumMemberBase<ts.EnumMember> {
setValue(value: string | number) {
let text: string;
if (typeof value === "string") {
const quoteType = this.global.manipulationSettings.getQuoteType();
text = quoteType + value + quoteType;
const quoteKind = this.global.manipulationSettings.getQuoteKind();
text = quoteKind + value + quoteKind;
}
else {
text = value.toString();
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/file/ExportDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export class ExportDeclaration extends Statement<ts.ExportDeclaration> {

if (stringLiteral == null) {
const semiColonToken = this.getLastChildIfKind(SyntaxKind.SemicolonToken);
const quoteType = this.global.manipulationSettings.getQuoteType();
const quoteKind = this.global.manipulationSettings.getQuoteKind();
insertIntoParentTextRange({
insertPos: semiColonToken != null ? semiColonToken.getPos() : this.getEnd(),
parent: this,
newText: ` from ${quoteType}${text}${quoteType}`
newText: ` from ${quoteKind}${text}${quoteKind}`
});
}
else
Expand Down
7 changes: 7 additions & 0 deletions src/compiler/literal/QuoteKind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** Quote type for a string literal. */
export enum QuoteKind {
/** Single quote */
Single = "'",
/** Double quote */
Double = "\""
}
10 changes: 5 additions & 5 deletions src/compiler/literal/StringLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ts} from "../../typescript";
import {StringUtils} from "../../utils";
import {replaceNodeText} from "../../manipulation";
import {LiteralExpression} from "../expression";
import {QuoteType} from "./QuoteType";
import {QuoteKind} from "./QuoteKind";

export const StringLiteralBase = LiteralExpression;
export class StringLiteral extends StringLiteralBase<ts.StringLiteral> {
Expand All @@ -24,15 +24,15 @@ export class StringLiteral extends StringLiteralBase<ts.StringLiteral> {
sourceFile: this.sourceFile,
start: this.getStart() + 1,
replacingLength: this.getWidth() - 2,
newText: StringUtils.escapeChar(value, this.getQuoteType()).replace(/(\r?\n)/g, "\\$1")
newText: StringUtils.escapeChar(value, this.getQuoteKind()).replace(/(\r?\n)/g, "\\$1")
});
return this;
}

/**
* Gets the quote type.
* Gets the quote kind.
*/
getQuoteType() {
return this.getText()[0] === "'" ? QuoteType.Single : QuoteType.Double;
getQuoteKind() {
return this.getText()[0] === "'" ? QuoteKind.Single : QuoteKind.Double;
}
}
2 changes: 1 addition & 1 deletion src/compiler/literal/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from "./BooleanLiteral";
export * from "./NullLiteral";
export * from "./NumericLiteral";
export * from "./QuoteType";
export * from "./QuoteKind";
export * from "./RegularExpressionLiteral";
export * from "./StringLiteral";
export * from "./template";
12 changes: 6 additions & 6 deletions src/options/ManipulationSettingsContainer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as objectAssign from "object-assign";
import {ts, NewLineKind, EditorSettings} from "../typescript";
import {QuoteType} from "../compiler";
import {QuoteKind} from "../compiler";
import {newLineKindToString, fillDefaultEditorSettings} from "../utils";
import {SettingsContainer} from "./SettingsContainer";

Expand All @@ -25,7 +25,7 @@ export interface ManipulationSettings extends SupportedFormatCodeSettingsOnly {
/** New line kind */
newLineKind: NewLineKind;
/** Quote type used for string literals. */
quoteType: QuoteType;
quoteKind: QuoteKind;
}

/**
Expand Down Expand Up @@ -57,7 +57,7 @@ export class ManipulationSettingsContainer extends SettingsContainer<Manipulatio
super({
indentationText: IndentationText.FourSpaces,
newLineKind: NewLineKind.LineFeed,
quoteType: QuoteType.Double,
quoteKind: QuoteKind.Double,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true
});
}
Expand Down Expand Up @@ -89,10 +89,10 @@ export class ManipulationSettingsContainer extends SettingsContainer<Manipulatio
}

/**
* Gets the quote type used for string literals.
* Gets the quote kind used for string literals.
*/
getQuoteType() {
return this.settings.quoteType;
getQuoteKind() {
return this.settings.quoteKind;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/tests/compiler/file/sourceFileTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect} from "chai";
import * as errors from "../../../errors";
import {ts, LanguageVariant, ScriptTarget, NewLineKind} from "../../../typescript";
import {SourceFile, ImportDeclaration, ExportDeclaration, ExportAssignment, EmitResult, FormatCodeSettings, QuoteType,
import {SourceFile, ImportDeclaration, ExportDeclaration, ExportAssignment, EmitResult, FormatCodeSettings, QuoteKind,
FileSystemRefreshResult} from "../../../compiler";
import {IndentationText, ManipulationSettings} from "../../../options";
import {ImportDeclarationStructure, ExportDeclarationStructure, SourceFileSpecificStructure, ExportAssignmentStructure} from "../../../structures";
Expand Down Expand Up @@ -389,7 +389,7 @@ describe(nameof(SourceFile), () => {
function doTest(startCode: string, index: number, structures: ImportDeclarationStructure[], expectedCode: string, useSingleQuotes = false) {
const {sourceFile, project} = getInfoFromText(startCode);
if (useSingleQuotes)
project.manipulationSettings.set({ quoteType: QuoteType.Single });
project.manipulationSettings.set({ quoteKind: QuoteKind.Single });
const result = sourceFile.insertImportDeclarations(index, structures);
expect(result.length).to.equal(structures.length);
expect(sourceFile.getText()).to.equal(expectedCode);
Expand Down
12 changes: 6 additions & 6 deletions src/tests/compiler/literal/stringLiteralTests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from "chai";
import {ts, SyntaxKind} from "../../../typescript";
import {StringLiteral, QuoteType} from "../../../compiler";
import {StringLiteral, QuoteKind} from "../../../compiler";
import {getInfoFromTextWithDescendant} from "../testHelpers";

function getStringLiteral(text: string) {
Expand Down Expand Up @@ -73,18 +73,18 @@ describe(nameof(StringLiteral), () => {
});
});

describe(nameof<StringLiteral>(n => n.getQuoteType), () => {
function doTest(text: string, quoteType: QuoteType) {
describe(nameof<StringLiteral>(n => n.getQuoteKind), () => {
function doTest(text: string, quoteKind: QuoteKind) {
const literal = getStringLiteral(text);
expect(literal.getQuoteType()).to.equal(quoteType);
expect(literal.getQuoteKind()).to.equal(quoteKind);
}

it("should be a double when a double", () => {
doTest(`const t = "str";`, QuoteType.Double);
doTest(`const t = "str";`, QuoteKind.Double);
});

it("should be a single when a single", () => {
doTest("const t = 'str';", QuoteType.Single);
doTest("const t = 'str';", QuoteKind.Single);
});
});
});
12 changes: 6 additions & 6 deletions src/tests/manipulationSettingsTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect} from "chai";
import {ManipulationSettings, ManipulationSettingsContainer, IndentationText} from "../options";
import {ts, NewLineKind, EditorSettings, IndentStyle} from "../typescript";
import {QuoteType} from "../compiler";
import {QuoteKind} from "../compiler";
import {StringUtils} from "../utils";

describe(nameof(IndentationText), () => {
Expand Down Expand Up @@ -32,7 +32,7 @@ describe(nameof(ManipulationSettingsContainer), () => {
it("should have the correct defaults", () => {
const settings = new ManipulationSettingsContainer();
checkSettings(settings, {
quoteType: QuoteType.Double,
quoteKind: QuoteKind.Double,
newLineKind: NewLineKind.LineFeed,
indentationText: IndentationText.FourSpaces,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true
Expand All @@ -42,11 +42,11 @@ describe(nameof(ManipulationSettingsContainer), () => {
it("should set the settings when partially setting them", () => {
const settings = new ManipulationSettingsContainer();
settings.set({
quoteType: QuoteType.Single
quoteKind: QuoteKind.Single
});

checkSettings(settings, {
quoteType: QuoteType.Single,
quoteKind: QuoteKind.Single,
newLineKind: NewLineKind.LineFeed,
indentationText: IndentationText.FourSpaces,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true
Expand All @@ -56,14 +56,14 @@ describe(nameof(ManipulationSettingsContainer), () => {
it("should set the settings when setting all of them", () => {
const settings = new ManipulationSettingsContainer();
settings.set({
quoteType: QuoteType.Single,
quoteKind: QuoteKind.Single,
newLineKind: NewLineKind.CarriageReturnLineFeed,
indentationText: IndentationText.EightSpaces,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: false
});

checkSettings(settings, {
quoteType: QuoteType.Single,
quoteKind: QuoteKind.Single,
newLineKind: NewLineKind.CarriageReturnLineFeed,
indentationText: IndentationText.EightSpaces,
insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: false
Expand Down

0 comments on commit 964571a

Please sign in to comment.