Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Make SourceCodeFixer accept text instead of a SourceCode instance #9178

Merged
merged 1 commit into from Aug 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/linter.js
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/testers/rule-tester.js
Expand Up @@ -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.");
}
Expand Down
19 changes: 5 additions & 14 deletions lib/util/source-code-fixer.js
Expand Up @@ -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;

Expand Down