Skip to content

Commit

Permalink
fix: Setting string literal value by string should escape newlines an…
Browse files Browse the repository at this point in the history
…d quote chars.

Also, fixed this downstream in code-block-writer in the quote(str) method.
  • Loading branch information
dsherret committed Mar 27, 2018
1 parent 20afef6 commit d68b6b9
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
"tslint": "^5.8.0"
},
"dependencies": {
"@dsherret/to-absolute-glob": "^2.0.2",
"code-block-writer": "^6.7.0",
"globby": "^6.1.0",
"multimatch": "^2.1.0",
"object-assign": "^4.1.1",
"@dsherret/to-absolute-glob": "2.0.2",
"code-block-writer": "6.7.1",
"globby": "6.1.0",
"multimatch": "2.1.0",
"object-assign": "4.1.1",
"typescript": "2.7.1"
},
"standard-version": {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/enum/EnumMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class EnumMember extends EnumMemberBase<ts.EnumMember> {
let text: string;
if (typeof value === "string") {
const quoteKind = this.global.manipulationSettings.getQuoteKind();
text = quoteKind + StringUtils.escapeChar(value, quoteKind) + quoteKind;
text = quoteKind + StringUtils.escapeForWithinString(value, quoteKind) + quoteKind;
}
else {
text = value.toString();
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/literal/StringLiteral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class StringLiteral extends StringLiteralBase<ts.StringLiteral> {
sourceFile: this.sourceFile,
start: this.getStart() + 1,
replacingLength: this.getWidth() - 2,
newText: StringUtils.escapeChar(value, this.getQuoteKind()).replace(/(\r?\n)/g, "\\$1")
newText: StringUtils.escapeForWithinString(value, this.getQuoteKind())
});
return this;
}
Expand Down
4 changes: 4 additions & 0 deletions src/tests/compiler/enum/enumMemberTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ describe(nameof(EnumMember), () => {
doTest("enum MyEnum { member }", "st\"r", `enum MyEnum { member = "st\\"r" }`);
});

it("should escape a string with a newline in it", () => {
doTest("enum MyEnum { member }", "st\nr", `enum MyEnum { member = "st\\\nr" }`);
});

it("should set the value for to a number", () => {
doTest("enum MyEnum { member }", 5, `enum MyEnum { member = 5 }`);
});
Expand Down
11 changes: 11 additions & 0 deletions src/tests/utils/stringUtilsTests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {expect} from "chai";
import * as errors from "../../errors";
import {QuoteKind} from "../../compiler";
import {StringUtils, Es5StringUtils} from "../../utils";

describe(nameof(StringUtils), () => {
Expand Down Expand Up @@ -43,6 +44,16 @@ describe(nameof(StringUtils), () => {
});
});

describe(nameof(StringUtils.escapeForWithinString), () => {
function doTest(input: string, expected: string) {
expect(StringUtils.escapeForWithinString(input, QuoteKind.Double)).to.equal(expected);
}

it("should escape the quotes and newline", () => {
doTest(`"testing\n this out"`, `\\"testing\\\n this out\\"`);
});
});

describe(nameof(StringUtils.escapeChar), () => {
function doTest(input: string, char: string, expected: string) {
expect(StringUtils.escapeChar(input, char)).to.equal(expected);
Expand Down
7 changes: 6 additions & 1 deletion src/utils/StringUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as errors from "../errors";
import {QuoteKind} from "../compiler";
import * as errors from "../errors";

export class StringUtils {
private constructor() {
Expand Down Expand Up @@ -40,6 +41,10 @@ export class StringUtils {
return count + 1; // convert count to line number
}

static escapeForWithinString(str: string, quoteKind: QuoteKind) {
return StringUtils.escapeChar(str, quoteKind).replace(/(\r?\n)/g, "\\$1");
}

/**
* Escapes all the occurences of the char in the string.
*/
Expand Down
32 changes: 16 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# yarn lockfile v1


"@dsherret/to-absolute-glob@^2.0.2":
"@dsherret/to-absolute-glob@2.0.2":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@dsherret/to-absolute-glob/-/to-absolute-glob-2.0.2.tgz#1f6475dc8bd974cea07a2daf3864b317b1dd332c"
dependencies:
Expand Down Expand Up @@ -388,9 +388,9 @@ clone@^1.0.0, clone@^1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/clone/-/clone-1.0.3.tgz#298d7e2231660f40c003c2ed3140decf3f53085f"

code-block-writer@^6.7.0:
version "6.7.0"
resolved "https://registry.yarnpkg.com/code-block-writer/-/code-block-writer-6.7.0.tgz#64841a32455f7d2a783d2989f987830374761880"
code-block-writer@6.7.1:
version "6.7.1"
resolved "https://registry.yarnpkg.com/code-block-writer/-/code-block-writer-6.7.1.tgz#174bbc3b8c7345c7d6e3e47a75159f5e502bda0d"

collection-visit@^1.0.0:
version "1.0.0"
Expand Down Expand Up @@ -963,22 +963,22 @@ global-prefix@^1.0.1:
is-windows "^1.0.1"
which "^1.2.14"

globby@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-5.0.0.tgz#ebd84667ca0dbb330b99bcfc68eac2bc54370e0d"
globby@6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-6.1.0.tgz#f5a6d70e8395e21c858fb0489d64df02424d506c"
dependencies:
array-union "^1.0.1"
arrify "^1.0.0"
glob "^7.0.3"
object-assign "^4.0.1"
pify "^2.0.0"
pinkie-promise "^2.0.0"

globby@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-6.1.0.tgz#f5a6d70e8395e21c858fb0489d64df02424d506c"
globby@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-5.0.0.tgz#ebd84667ca0dbb330b99bcfc68eac2bc54370e0d"
dependencies:
array-union "^1.0.1"
arrify "^1.0.0"
glob "^7.0.3"
object-assign "^4.0.1"
pify "^2.0.0"
Expand Down Expand Up @@ -1898,7 +1898,7 @@ ms@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"

multimatch@^2.0.0, multimatch@^2.1.0:
multimatch@2.1.0, multimatch@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/multimatch/-/multimatch-2.1.0.tgz#9c7906a22fb4c02919e2f5f75161b4cdbd4b2a2b"
dependencies:
Expand Down Expand Up @@ -1950,14 +1950,14 @@ oauth-sign@~0.8.1:
version "0.8.2"
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.8.2.tgz#46a6ab7f0aead8deae9ec0565780b7d4efeb9d43"

object-assign@4.1.1, object-assign@^4.0.0, object-assign@^4.0.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"

object-assign@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-3.0.0.tgz#9bedd5ca0897949bca47e7ff408062d549f587f2"

object-assign@^4.0.0, object-assign@^4.0.1, object-assign@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"

object-copy@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/object-copy/-/object-copy-0.1.0.tgz#7e7d858b781bd7c991a41ba975ed3812754e998c"
Expand Down

0 comments on commit d68b6b9

Please sign in to comment.