Skip to content

Commit

Permalink
fix: #308 - Should not insert before the BOM.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Apr 14, 2018
1 parent 4b0dbb0 commit 07a2a51
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const Chars = {
BOM: "\uFEFF"
};
3 changes: 2 additions & 1 deletion src/manipulation/helpers/getInsertPosFromIndex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ts, SyntaxKind} from "../../typescript";
import {Node} from "../../compiler";
import {Chars} from "../../constants";
import {TypeGuards} from "../../utils";
import {getPosAtStartOfLineOrNonWhitespace} from "../textSeek";

Expand All @@ -9,7 +10,7 @@ import {getPosAtStartOfLineOrNonWhitespace} from "../textSeek";
export function getInsertPosFromIndex(index: number, parent: Node, children: Node[]) {
if (index === 0) {
if (TypeGuards.isSourceFile(parent))
return 0;
return parent.getFullText()[0] === Chars.BOM ? 1 : 0;
else if (TypeGuards.isCaseClause(parent) || TypeGuards.isDefaultClause(parent)) {
const colonToken = parent.getFirstChildByKindOrThrow(SyntaxKind.ColonToken);
return colonToken.getEnd();
Expand Down
3 changes: 2 additions & 1 deletion src/manipulation/manipulations/insertion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import CodeBlockWriter from "code-block-writer";
import { ts, SyntaxKind } from "../../typescript";
import { Chars } from "../../constants";
import {Node, SourceFile} from "../../compiler";
import {StringUtils, TypeGuards} from "../../utils";
import {InsertionTextManipulator} from "../textManipulators";
Expand Down Expand Up @@ -153,7 +154,7 @@ export function insertIntoBracesOrSourceFile<TStructure = {}>(opts: InsertIntoBr
opts.write(writer, {
previousMember: getChild(children[index - 1]),
nextMember: getChild(children[index]),
isStartOfFile: insertPos === 0
isStartOfFile: insertPos === 0 || insertPos === 1 && fullText[0] === Chars.BOM
});
return writer.toString();

Expand Down
7 changes: 6 additions & 1 deletion src/tests/compiler/file/sourceFileTests.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 { Chars } from "../../../constants";
import {ts, LanguageVariant, ScriptTarget, NewLineKind, CompilerOptions, ModuleResolutionKind} from "../../../typescript";
import {SourceFile, ImportDeclaration, ExportDeclaration, ExportAssignment, EmitResult, FormatCodeSettings, QuoteKind,
FileSystemRefreshResult} from "../../../compiler";
Expand Down Expand Up @@ -392,7 +393,7 @@ describe(nameof(SourceFile), () => {
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);
expect(sourceFile.getFullText()).to.equal(expectedCode);
}

it("should insert the different kinds of imports", () => {
Expand Down Expand Up @@ -421,6 +422,10 @@ describe(nameof(SourceFile), () => {
}).to.throw();
});

it("should insert an import after a utf-8 bom", () => {
doTest(Chars.BOM, 0, [{ moduleSpecifier: "./test" }], `${Chars.BOM}import "./test";\n`);
});

it("should insert at the beginning and use single quotes when specified", () => {
doTest(`export class Class {}\n`, 0, [{ moduleSpecifier: "./test" }], `import './test';\n\nexport class Class {}\n`, true);
});
Expand Down
5 changes: 5 additions & 0 deletions src/tests/compiler/statement/statementedNodeTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {expect} from "chai";
import { Chars } from "../../../constants";
import {ts, SyntaxKind} from "../../../typescript";
import {CaseClause, DefaultClause, StatementedNode, SourceFile, FunctionDeclaration, NamespaceDeclaration, Node} from "../../../compiler";
import {StatementedNodeStructure} from "../../../structures";
Expand Down Expand Up @@ -111,6 +112,10 @@ describe(nameof(StatementedNode), () => {
"newText;\nsecondText;\n");
});

it("should insert statements after a utf-8 bom", () => {
doSourceFileTest(Chars.BOM, 0, "newText;", 1, Chars.BOM + "newText;\n");
});

it("should allow inserting nothing", () => {
doSourceFileTest("", 0, "", 0, "");
});
Expand Down
6 changes: 3 additions & 3 deletions src/utils/FileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import globParent = require("glob-parent");
import isNegatedGlob = require("is-negated-glob");
import * as toAbsoluteGlob from "@dsherret/to-absolute-glob";
import {FileSystemHost, FileSystemWrapper} from "../fileSystem";
import { Chars } from "../constants";
import {StringUtils} from "./StringUtils";
import {ArrayUtils} from "./ArrayUtils";

Expand Down Expand Up @@ -240,10 +241,9 @@ export class FileUtils {
* @param text - Text.
*/
static getTextWithByteOrderMark(text: string) {
const bom = "\ufeff";
if (text[0] === bom)
if (text[0] === Chars.BOM)
return text;
return bom + text;
return Chars.BOM + text;
}

/**
Expand Down

0 comments on commit 07a2a51

Please sign in to comment.