Repository: eslint/eslint
Pull request #9178 (2017-08-29 01:44:57)

Chore: Make SourceCodeFixer accept text instead of a SourceCode instance
<!--
    ESLint adheres to the [JS Foundation Code of Conduct](https://js.foundation/community/code-of-conduct).
-->

**What is the purpose of this pull request? (put an "X" next to item)**

[x] Other, please explain:

<!--
    If the item you've checked above has a template, please paste the template questions below and answer them. (If this pull request is addressing an issue, you can just paste a link to the issue here instead.)
-->

<!--
    Please ensure your pull request is ready:

    - Read the pull request guide (http://eslint.org/docs/developer-guide/contributing/pull-requests)
    - Include tests for this change
    - Update documentation for this change (if appropriate)
-->

<!--
    The following is required for all pull requests:
-->

**What changes did you make? (Give an overview)**

This updates `SourceCodeFixer` to operate on a string containing the source text, rather than a `SourceCode` instance. This removes the need for `Linter` to retain a reference to a given `SourceCode` instance after calling `verify`. (This change was also included as part of https://github.com/eslint/eslint/pull/9090, but it's probably better to separate it.)

**Is there anything you'd like reviewers to focus on?**

Nothing in particular


eslintbot: LGTM

Patch:
diff --git a/lib/linter.js b/lib/linter.js
index 02be483a2..856fc7e75 100755
--- a/lib/linter.js
+++ b/lib/linter.js
@@ -1217,7 +1217,7 @@ class Linter extends EventEmitter {
             messages = this.verify(text, config, options);
 
             debug(`Generating fixed text for ${debugTextDescription} (pass ${passNumber})`);
-            fixedResult = SourceCodeFixer.applyFixes(this.getSourceCode(), messages, shouldFix);
+            fixedResult = SourceCodeFixer.applyFixes(text, messages, shouldFix);
 
             // stop if there are any syntax errors.
             // 'fixedResult.output' is a empty string.
diff --git a/lib/util/source-code-fixer.js b/lib/util/source-code-fixer.js
index 1b6270a1e..ebb7e3e02 100644
--- a/lib/util/source-code-fixer.js
+++ b/lib/util/source-code-fixer.js
@@ -53,37 +53,28 @@ function SourceCodeFixer() {
 /**
  * Applies the fixes specified by the messages to the given text. Tries to be
  * smart about the fixes and won't apply fixes over the same area in the text.
- * @param {SourceCode} sourceCode The source code to apply the changes to.
+ * @param {string} sourceText The text to apply the changes to.
  * @param {Message[]} messages The array of messages reported by ESLint.
  * @param {boolean|Function} [shouldFix=true] Determines whether each message should be fixed
  * @returns {Object} An object containing the fixed text and any unfixed messages.
  */
-SourceCodeFixer.applyFixes = function(sourceCode, messages, shouldFix) {
+SourceCodeFixer.applyFixes = function(sourceText, messages, shouldFix) {
     debug("Applying fixes");
 
-    if (!sourceCode) {
-        debug("No source code to fix");
-        return {
-            fixed: false,
-            messages,
-            output: ""
-        };
-    }
-
     if (shouldFix === false) {
         debug("shouldFix parameter was false, not attempting fixes");
         return {
             fixed: false,
             messages,
-            output: sourceCode.text
+            output: sourceText
         };
     }
 
     // clone the array
     const remainingMessages = [],
         fixes = [],
-        bom = (sourceCode.hasBOM ? BOM : ""),
-        text = sourceCode.text;
+        bom = sourceText.startsWith(BOM) ? BOM : "",
+        text = bom ? sourceText.slice(1) : sourceText;
     let lastPos = Number.NEGATIVE_INFINITY,
         output = bom;
 
diff --git a/tools/eslint-fuzzer.js b/tools/eslint-fuzzer.js
index 3653673f3..ea84e994b 100644
--- a/tools/eslint-fuzzer.js
+++ b/tools/eslint-fuzzer.js
@@ -97,7 +97,7 @@ function fuzz(options) {
             }
 
             lastGoodText = currentText;
-            currentText = SourceCodeFixer.applyFixes(linter.getSourceCode(), messages).output;
+            currentText = SourceCodeFixer.applyFixes(currentText, messages).output;
         } while (lastGoodText !== currentText);
 
         return lastGoodText;


Test patch:
diff --git a/lib/testers/rule-tester.js b/lib/testers/rule-tester.js
index 9ffad975f..5b58e5452 100644
--- a/lib/testers/rule-tester.js
+++ b/lib/testers/rule-tester.js
@@ -521,7 +521,7 @@ class RuleTester {
                         "Expected no autofixes to be suggested"
                     );
                 } else {
-                    const fixResult = SourceCodeFixer.applyFixes(linter.getSourceCode(), messages);
+                    const fixResult = SourceCodeFixer.applyFixes(item.code, messages);
 
                     assert.equal(fixResult.output, item.output, "Output is incorrect.");
                 }
diff --git a/tests/lib/util/source-code-fixer.js b/tests/lib/util/source-code-fixer.js
index 1ce5adc25..758baf178 100644
--- a/tests/lib/util/source-code-fixer.js
+++ b/tests/lib/util/source-code-fixer.js
@@ -9,17 +9,14 @@
 //------------------------------------------------------------------------------
 
 const assert = require("chai").assert,
-    espree = require("espree"),
     sinon = require("sinon"),
-    SourceCode = require("../../../lib/util/source-code"),
     SourceCodeFixer = require("../../../lib/util/source-code-fixer");
 
 //------------------------------------------------------------------------------
 // Helpers
 //------------------------------------------------------------------------------
 
-const TEST_CODE = "var answer = 6 * 7;",
-    TEST_AST = espree.parse(TEST_CODE, { loc: true, range: true, tokens: true, comment: true });
+const TEST_CODE = "var answer = 6 * 7;";
 const INSERT_AT_END = {
         message: "End",
         fix: {
@@ -150,34 +147,16 @@ describe("SourceCodeFixer", () => {
     });
 
     describe("applyFixes() with no BOM", () => {
-
-        let sourceCode;
-
-        beforeEach(() => {
-            sourceCode = new SourceCode(TEST_CODE, TEST_AST);
-        });
-
-        it("Should have empty output if sourceCode is not provided", () => {
-            const result = SourceCodeFixer.applyFixes(null, [INSERT_AT_END]);
-
-            assert.equal(result.output.length, 0);
-        });
-
         describe("shouldFix parameter", () => {
-
-            beforeEach(() => {
-                sourceCode = new SourceCode(TEST_CODE, TEST_AST);
-            });
-
             it("Should not perform any fixes if 'shouldFix' is false", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_END], false);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_AT_END], false);
 
                 assert.isFalse(result.fixed);
-                assert.equal(result.output, sourceCode.text);
+                assert.equal(result.output, TEST_CODE);
             });
 
             it("Should perform fixes if 'shouldFix' is not provided", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_END]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_AT_END]);
 
                 assert.isTrue(result.fixed);
             });
@@ -185,34 +164,34 @@ describe("SourceCodeFixer", () => {
             it("should call a function provided as 'shouldFix' for each message", () => {
                 const shouldFixSpy = sinon.spy();
 
-                SourceCodeFixer.applyFixes(sourceCode, [INSERT_IN_MIDDLE, INSERT_AT_START, INSERT_AT_END], shouldFixSpy);
+                SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_IN_MIDDLE, INSERT_AT_START, INSERT_AT_END], shouldFixSpy);
                 assert.isTrue(shouldFixSpy.calledThrice);
             });
 
             it("should provide a message object as an argument to 'shouldFix'", () => {
                 const shouldFixSpy = sinon.spy();
 
-                SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_START], shouldFixSpy);
+                SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_AT_START], shouldFixSpy);
                 assert.equal(shouldFixSpy.firstCall.args[0], INSERT_AT_START);
             });
 
             it("should not perform fixes if 'shouldFix' function returns false", () => {
                 const shouldFixSpy = sinon.spy(() => false);
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_START], shouldFixSpy);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_AT_START], shouldFixSpy);
 
                 assert.isFalse(result.fixed);
             });
 
             it("should return original text as output if 'shouldFix' function prevents all fixes", () => {
                 const shouldFixSpy = sinon.spy(() => false);
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_START], shouldFixSpy);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_AT_START], shouldFixSpy);
 
                 assert.equal(result.output, TEST_CODE);
             });
 
             it("should only apply fixes for which the 'shouldFix' function returns true", () => {
                 const shouldFixSpy = sinon.spy(problem => problem.message === "foo");
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_START, REPLACE_ID], shouldFixSpy);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_AT_START, REPLACE_ID], shouldFixSpy);
 
                 assert.equal(result.output, "var foo = 6 * 7;");
             });
@@ -220,7 +199,7 @@ describe("SourceCodeFixer", () => {
             it("is called without access to internal eslint state", () => {
                 const shouldFixSpy = sinon.spy();
 
-                SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_START], shouldFixSpy);
+                SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_AT_START], shouldFixSpy);
 
                 assert.isUndefined(shouldFixSpy.thisValues[0]);
             });
@@ -229,28 +208,28 @@ describe("SourceCodeFixer", () => {
         describe("Text Insertion", () => {
 
             it("should insert text at the end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_END]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_AT_END]);
 
                 assert.equal(result.output, TEST_CODE + INSERT_AT_END.fix.text);
                 assert.equal(result.messages.length, 0);
             });
 
             it("should insert text at the beginning of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_START]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_AT_START]);
 
                 assert.equal(result.output, INSERT_AT_START.fix.text + TEST_CODE);
                 assert.equal(result.messages.length, 0);
             });
 
             it("should insert text in the middle of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_IN_MIDDLE]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_IN_MIDDLE]);
 
                 assert.equal(result.output, TEST_CODE.replace("6 *", `${INSERT_IN_MIDDLE.fix.text}6 *`));
                 assert.equal(result.messages.length, 0);
             });
 
             it("should insert text at the beginning, middle, and end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_IN_MIDDLE, INSERT_AT_START, INSERT_AT_END]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_IN_MIDDLE, INSERT_AT_START, INSERT_AT_END]);
 
                 assert.equal(result.output, INSERT_AT_START.fix.text + TEST_CODE.replace("6 *", `${INSERT_IN_MIDDLE.fix.text}6 *`) + INSERT_AT_END.fix.text);
                 assert.equal(result.messages.length, 0);
@@ -258,7 +237,7 @@ describe("SourceCodeFixer", () => {
 
 
             it("should ignore reversed ranges", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REVERSED_RANGE]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REVERSED_RANGE]);
 
                 assert.equal(result.output, TEST_CODE);
             });
@@ -269,7 +248,7 @@ describe("SourceCodeFixer", () => {
         describe("Text Replacement", () => {
 
             it("should replace text at the end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_VAR]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REPLACE_VAR]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, TEST_CODE.replace("var", "let"));
@@ -277,7 +256,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should replace text at the beginning of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_ID]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REPLACE_ID]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, TEST_CODE.replace("answer", "foo"));
@@ -285,7 +264,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should replace text in the middle of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_NUM]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REPLACE_NUM]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, TEST_CODE.replace("6", "5"));
@@ -293,7 +272,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should replace text at the beginning and end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_ID, REPLACE_VAR, REPLACE_NUM]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REPLACE_ID, REPLACE_VAR, REPLACE_NUM]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, "let foo = 5 * 7;");
@@ -305,7 +284,7 @@ describe("SourceCodeFixer", () => {
         describe("Text Removal", () => {
 
             it("should remove text at the start of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_START]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REMOVE_START]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, TEST_CODE.replace("var ", ""));
@@ -313,7 +292,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should remove text in the middle of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_MIDDLE]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REMOVE_MIDDLE]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, TEST_CODE.replace("answer", "a"));
@@ -321,7 +300,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should remove text towards the end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_END]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REMOVE_END]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, TEST_CODE.replace(" * 7", ""));
@@ -329,7 +308,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should remove text at the beginning, middle, and end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_END, REMOVE_START, REMOVE_MIDDLE]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REMOVE_END, REMOVE_START, REMOVE_MIDDLE]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, "a = 6;");
@@ -340,7 +319,7 @@ describe("SourceCodeFixer", () => {
         describe("Combination", () => {
 
             it("should replace text at the beginning, remove text in the middle, and insert text at the end", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_END, REMOVE_END, REPLACE_VAR]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_AT_END, REMOVE_END, REPLACE_VAR]);
 
                 assert.equal(result.output, "let answer = 6;// end");
                 assert.isTrue(result.fixed);
@@ -348,7 +327,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should only apply one fix when ranges overlap", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_MIDDLE, REPLACE_ID]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REMOVE_MIDDLE, REPLACE_ID]);
 
                 assert.equal(result.output, TEST_CODE.replace("answer", "foo"));
                 assert.equal(result.messages.length, 1);
@@ -357,7 +336,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should apply one fix when the end of one range is the same as the start of a previous range overlap", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_START, REPLACE_ID]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REMOVE_START, REPLACE_ID]);
 
                 assert.equal(result.output, TEST_CODE.replace("var ", ""));
                 assert.equal(result.messages.length, 1);
@@ -366,7 +345,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should only apply one fix when ranges overlap and one message has no fix", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_MIDDLE, REPLACE_ID, NO_FIX]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REMOVE_MIDDLE, REPLACE_ID, NO_FIX]);
 
                 assert.equal(result.output, TEST_CODE.replace("answer", "foo"));
                 assert.equal(result.messages.length, 2);
@@ -376,8 +355,8 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should apply the same fix when ranges overlap regardless of order", () => {
-                const result1 = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_MIDDLE, REPLACE_ID]);
-                const result2 = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_ID, REMOVE_MIDDLE]);
+                const result1 = SourceCodeFixer.applyFixes(TEST_CODE, [REMOVE_MIDDLE, REPLACE_ID]);
+                const result2 = SourceCodeFixer.applyFixes(TEST_CODE, [REPLACE_ID, REMOVE_MIDDLE]);
 
                 assert.equal(result1.output, result2.output);
             });
@@ -386,7 +365,7 @@ describe("SourceCodeFixer", () => {
         describe("No Fixes", () => {
 
             it("should only apply one fix when ranges overlap and one message has no fix", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [NO_FIX]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [NO_FIX]);
 
                 assert.equal(result.output, TEST_CODE);
                 assert.equal(result.messages.length, 1);
@@ -395,7 +374,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should sort the no fix messages correctly", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_ID, NO_FIX2, NO_FIX1]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REPLACE_ID, NO_FIX2, NO_FIX1]);
 
                 assert.equal(result.output, TEST_CODE.replace("answer", "foo"));
                 assert.equal(result.messages.length, 2);
@@ -409,7 +388,7 @@ describe("SourceCodeFixer", () => {
         describe("BOM manipulations", () => {
 
             it("should insert BOM with an insertion of '\uFEFF' at 0", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_BOM]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_BOM]);
 
                 assert.equal(result.output, `\uFEFF${TEST_CODE}`);
                 assert.isTrue(result.fixed);
@@ -417,7 +396,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should insert BOM with an insertion of '\uFEFFfoobar' at 0", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_BOM_WITH_TEXT]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [INSERT_BOM_WITH_TEXT]);
 
                 assert.equal(result.output, `\uFEFF// start\n${TEST_CODE}`);
                 assert.isTrue(result.fixed);
@@ -425,7 +404,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should remove BOM with a negative range", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_BOM]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REMOVE_BOM]);
 
                 assert.equal(result.output, TEST_CODE);
                 assert.isTrue(result.fixed);
@@ -433,7 +412,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should replace BOM with a negative range and 'foobar'", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_BOM_WITH_TEXT]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE, [REPLACE_BOM_WITH_TEXT]);
 
                 assert.equal(result.output, `// start\n${TEST_CODE}`);
                 assert.isTrue(result.fixed);
@@ -448,37 +427,33 @@ describe("SourceCodeFixer", () => {
     // Just `result.output` has BOM.
     describe("applyFixes() with BOM", () => {
 
-        let sourceCode;
-
-        beforeEach(() => {
-            sourceCode = new SourceCode(`\uFEFF${TEST_CODE}`, TEST_AST);
-        });
+        const TEST_CODE_WITH_BOM = `\uFEFF${TEST_CODE}`;
 
         describe("Text Insertion", () => {
 
             it("should insert text at the end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_END]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [INSERT_AT_END]);
 
                 assert.equal(result.output, `\uFEFF${TEST_CODE}${INSERT_AT_END.fix.text}`);
                 assert.equal(result.messages.length, 0);
             });
 
             it("should insert text at the beginning of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_START]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [INSERT_AT_START]);
 
                 assert.equal(result.output, `\uFEFF${INSERT_AT_START.fix.text}${TEST_CODE}`);
                 assert.equal(result.messages.length, 0);
             });
 
             it("should insert text in the middle of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_IN_MIDDLE]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [INSERT_IN_MIDDLE]);
 
                 assert.equal(result.output, `\uFEFF${TEST_CODE.replace("6 *", `${INSERT_IN_MIDDLE.fix.text}6 *`)}`);
                 assert.equal(result.messages.length, 0);
             });
 
             it("should insert text at the beginning, middle, and end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_IN_MIDDLE, INSERT_AT_START, INSERT_AT_END]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [INSERT_IN_MIDDLE, INSERT_AT_START, INSERT_AT_END]);
                 const insertInMiddle = TEST_CODE.replace("6 *", `${INSERT_IN_MIDDLE.fix.text}6 *`);
 
                 assert.equal(result.output, `\uFEFF${INSERT_AT_START.fix.text}${insertInMiddle}${INSERT_AT_END.fix.text}`);
@@ -486,7 +461,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should ignore reversed ranges", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REVERSED_RANGE]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REVERSED_RANGE]);
 
                 assert.equal(result.output, `\uFEFF${TEST_CODE}`);
             });
@@ -496,7 +471,7 @@ describe("SourceCodeFixer", () => {
         describe("Text Replacement", () => {
 
             it("should replace text at the end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_VAR]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REPLACE_VAR]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, `\uFEFF${TEST_CODE.replace("var", "let")}`);
@@ -504,7 +479,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should replace text at the beginning of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_ID]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REPLACE_ID]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, `\uFEFF${TEST_CODE.replace("answer", "foo")}`);
@@ -512,7 +487,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should replace text in the middle of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_NUM]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REPLACE_NUM]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, `\uFEFF${TEST_CODE.replace("6", "5")}`);
@@ -520,7 +495,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should replace text at the beginning and end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_ID, REPLACE_VAR, REPLACE_NUM]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REPLACE_ID, REPLACE_VAR, REPLACE_NUM]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, "\uFEFFlet foo = 5 * 7;");
@@ -532,7 +507,7 @@ describe("SourceCodeFixer", () => {
         describe("Text Removal", () => {
 
             it("should remove text at the start of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_START]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REMOVE_START]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, `\uFEFF${TEST_CODE.replace("var ", "")}`);
@@ -540,7 +515,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should remove text in the middle of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_MIDDLE]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REMOVE_MIDDLE]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, `\uFEFF${TEST_CODE.replace("answer", "a")}`);
@@ -548,7 +523,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should remove text towards the end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_END]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REMOVE_END]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, `\uFEFF${TEST_CODE.replace(" * 7", "")}`);
@@ -556,7 +531,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should remove text at the beginning, middle, and end of the code", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_END, REMOVE_START, REMOVE_MIDDLE]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REMOVE_END, REMOVE_START, REMOVE_MIDDLE]);
 
                 assert.equal(result.messages.length, 0);
                 assert.equal(result.output, "\uFEFFa = 6;");
@@ -567,7 +542,7 @@ describe("SourceCodeFixer", () => {
         describe("Combination", () => {
 
             it("should replace text at the beginning, remove text in the middle, and insert text at the end", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_AT_END, REMOVE_END, REPLACE_VAR]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [INSERT_AT_END, REMOVE_END, REPLACE_VAR]);
 
                 assert.equal(result.output, "\uFEFFlet answer = 6;// end");
                 assert.isTrue(result.fixed);
@@ -575,7 +550,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should only apply one fix when ranges overlap", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_MIDDLE, REPLACE_ID]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REMOVE_MIDDLE, REPLACE_ID]);
 
                 assert.equal(result.output, `\uFEFF${TEST_CODE.replace("answer", "foo")}`);
                 assert.equal(result.messages.length, 1);
@@ -584,7 +559,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should apply one fix when the end of one range is the same as the start of a previous range overlap", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_START, REPLACE_ID]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REMOVE_START, REPLACE_ID]);
 
                 assert.equal(result.output, `\uFEFF${TEST_CODE.replace("var ", "")}`);
                 assert.equal(result.messages.length, 1);
@@ -593,7 +568,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should only apply one fix when ranges overlap and one message has no fix", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_MIDDLE, REPLACE_ID, NO_FIX]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REMOVE_MIDDLE, REPLACE_ID, NO_FIX]);
 
                 assert.equal(result.output, `\uFEFF${TEST_CODE.replace("answer", "foo")}`);
                 assert.equal(result.messages.length, 2);
@@ -603,8 +578,8 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should apply the same fix when ranges overlap regardless of order", () => {
-                const result1 = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_MIDDLE, REPLACE_ID]);
-                const result2 = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_ID, REMOVE_MIDDLE]);
+                const result1 = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REMOVE_MIDDLE, REPLACE_ID]);
+                const result2 = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REPLACE_ID, REMOVE_MIDDLE]);
 
                 assert.equal(result1.output, result2.output);
             });
@@ -614,7 +589,7 @@ describe("SourceCodeFixer", () => {
         describe("No Fixes", () => {
 
             it("should only apply one fix when ranges overlap and one message has no fix", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [NO_FIX]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [NO_FIX]);
 
                 assert.equal(result.output, `\uFEFF${TEST_CODE}`);
                 assert.equal(result.messages.length, 1);
@@ -627,7 +602,7 @@ describe("SourceCodeFixer", () => {
         describe("BOM manipulations", () => {
 
             it("should insert BOM with an insertion of '\uFEFF' at 0", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_BOM]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [INSERT_BOM]);
 
                 assert.equal(result.output, `\uFEFF${TEST_CODE}`);
                 assert.isTrue(result.fixed);
@@ -635,7 +610,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should insert BOM with an insertion of '\uFEFFfoobar' at 0", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [INSERT_BOM_WITH_TEXT]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [INSERT_BOM_WITH_TEXT]);
 
                 assert.equal(result.output, `\uFEFF// start\n${TEST_CODE}`);
                 assert.isTrue(result.fixed);
@@ -643,7 +618,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should remove BOM with a negative range", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REMOVE_BOM]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REMOVE_BOM]);
 
                 assert.equal(result.output, TEST_CODE);
                 assert.isTrue(result.fixed);
@@ -651,7 +626,7 @@ describe("SourceCodeFixer", () => {
             });
 
             it("should replace BOM with a negative range and 'foobar'", () => {
-                const result = SourceCodeFixer.applyFixes(sourceCode, [REPLACE_BOM_WITH_TEXT]);
+                const result = SourceCodeFixer.applyFixes(TEST_CODE_WITH_BOM, [REPLACE_BOM_WITH_TEXT]);
 
                 assert.equal(result.output, `// start\n${TEST_CODE}`);
                 assert.isTrue(result.fixed);