-
Notifications
You must be signed in to change notification settings - Fork 12.9k
WIP: add refactoring: string concatenation to template literals #28923
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
Closed
Closed
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
6cc8029
add skeleton
bigaru 50e130a
add test cases
bigaru 9492623
add test cases
bigaru 1046eb6
add visibility tests
bigaru 8510e25
add diagnostic messages
bigaru f15fcb7
add working conversion to template literal
bigaru 3b99801
add test cases
bigaru 671eb54
complete toTemplate
bigaru f1d28c9
complete toString
bigaru 9bce643
catch empty head of template literal
bigaru 97cee68
add toString visibility from expression and from middle part
bigaru 5e6e1fb
fix test case
bigaru 91cfca3
combine preceding expressions to one
bigaru ec69194
do not offer refactoring for tagged templates
bigaru 4d522c3
optimize preceding expression
bigaru 7441c35
treat corner cases
bigaru 7d8fccd
remove parentheses also when expression at ending
bigaru e567e53
add possibility to invoke from parentheses
bigaru e0fdf74
only show toString if expression is not binary
bigaru bea12a1
extract creation of templateHead
bigaru 2a38eef
optimize nodesToTemplate
bigaru 59008eb
extract getEdits for string concatenation
bigaru 997f3e3
optimize getEdits string concatenation
bigaru 81804c2
change from tuple to object literal
bigaru 3f5761f
optimize templateLiteral check
bigaru 8c9903a
extract getEdits for template literal
bigaru dbd31cc
add test cases
bigaru 85dbb27
add skeleton for handling octal escape
bigaru 2d7a48f
complete handling for octal escape
bigaru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
201 changes: 201 additions & 0 deletions
201
src/services/refactors/convertStringOrTemplateLiteral.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
/* @internal */ | ||
namespace ts.refactor.convertStringOrTemplateLiteral { | ||
const refactorName = "Convert string concatenation or template literal"; | ||
const toTemplateLiteralActionName = "Convert to template literal"; | ||
const toStringConcatenationActionName = "Convert to string concatenation"; | ||
|
||
const refactorDescription = getLocaleSpecificMessage(Diagnostics.Convert_string_concatenation_or_template_literal); | ||
const toTemplateLiteralDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_template_literal); | ||
const toStringConcatenationDescription = getLocaleSpecificMessage(Diagnostics.Convert_to_string_concatenation); | ||
|
||
registerRefactor(refactorName, { getEditsForAction, getAvailableActions }); | ||
|
||
function getAvailableActions(context: RefactorContext): ReadonlyArray<ApplicableRefactorInfo> { | ||
const { file, startPosition } = context; | ||
const node = getNodeOrParentOfParentheses(file, startPosition); | ||
const maybeBinary = getParentBinaryExpression(node); | ||
const actions: RefactorActionInfo[] = []; | ||
|
||
if ((isBinaryExpression(maybeBinary) || isStringLiteral(maybeBinary)) && isStringConcatenationValid(maybeBinary)) { | ||
actions.push({ name: toTemplateLiteralActionName, description: toTemplateLiteralDescription }); | ||
} | ||
|
||
const templateLiteral = findAncestor(node, n => isTemplateLiteral(n)); | ||
|
||
if (templateLiteral && !isTaggedTemplateExpression(templateLiteral.parent)) { | ||
actions.push({ name: toStringConcatenationActionName, description: toStringConcatenationDescription }); | ||
} | ||
|
||
return [{ name: refactorName, description: refactorDescription, actions }]; | ||
} | ||
|
||
function getNodeOrParentOfParentheses(file: SourceFile, startPosition: number) { | ||
const node = getTokenAtPosition(file, startPosition); | ||
if (isParenthesizedExpression(node.parent) && isBinaryExpression(node.parent.parent)) return node.parent.parent; | ||
return node; | ||
} | ||
|
||
function getEditsForAction(context: RefactorContext, actionName: string): RefactorEditInfo | undefined { | ||
const { file, startPosition } = context; | ||
const node = getNodeOrParentOfParentheses(file, startPosition); | ||
|
||
switch (actionName) { | ||
case toTemplateLiteralActionName: | ||
return { edits: getEditsForToTemplateLiteral(context, node) }; | ||
|
||
case toStringConcatenationActionName: | ||
return { edits: getEditsForToStringConcatenation(context, node) }; | ||
|
||
default: | ||
return Debug.fail("invalid action"); | ||
} | ||
} | ||
|
||
function getEditsForToTemplateLiteral(context: RefactorContext, node: Node) { | ||
const maybeBinary = getParentBinaryExpression(node); | ||
const arrayOfNodes = transformTreeToArray(maybeBinary); | ||
const templateLiteral = nodesToTemplate(arrayOfNodes); | ||
return textChanges.ChangeTracker.with(context, t => t.replaceNode(context.file, maybeBinary, templateLiteral)); | ||
} | ||
|
||
function getEditsForToStringConcatenation(context: RefactorContext, node: Node) { | ||
const templateLiteral = findAncestor(node, n => isTemplateLiteral(n))! as TemplateLiteral; | ||
|
||
if (isTemplateExpression(templateLiteral)) { | ||
const { head, templateSpans } = templateLiteral; | ||
const arrayOfNodes = templateSpans.map(templateSpanToExpressions) | ||
.reduce((accumulator, nextArray) => accumulator.concat(nextArray)); | ||
|
||
if (head.text.length !== 0) arrayOfNodes.unshift(createStringLiteral(head.text)); | ||
|
||
const binaryExpression = arrayToTree(arrayOfNodes); | ||
return textChanges.ChangeTracker.with(context, t => t.replaceNode(context.file, templateLiteral, binaryExpression)); | ||
} | ||
else { | ||
const stringLiteral = createStringLiteral(templateLiteral.text); | ||
return textChanges.ChangeTracker.with(context, t => t.replaceNode(context.file, node, stringLiteral)); | ||
} | ||
} | ||
|
||
function templateSpanToExpressions(templateSpan: TemplateSpan): Expression[] { | ||
const { expression, literal } = templateSpan; | ||
const text = literal.text; | ||
return text.length === 0 ? [expression] : [expression, createStringLiteral(text)]; | ||
} | ||
|
||
function getParentBinaryExpression(expr: Node) { | ||
while (isBinaryExpression(expr.parent)) { | ||
expr = expr.parent; | ||
} | ||
return expr; | ||
} | ||
|
||
function arrayToTree(nodes: ReadonlyArray<Expression>, accumulator?: BinaryExpression): BinaryExpression { | ||
if (nodes.length === 0) return accumulator!; | ||
|
||
if (!accumulator) { | ||
const left = nodes[0]; | ||
const right = nodes[1]; | ||
|
||
const binary = createBinary(left, SyntaxKind.PlusToken, right); | ||
return arrayToTree(nodes.slice(2), binary); | ||
} | ||
|
||
const right = nodes[0]; | ||
const binary = createBinary(accumulator, SyntaxKind.PlusToken, right); | ||
return arrayToTree(nodes.slice(1), binary); | ||
} | ||
|
||
function isStringConcatenationValid(node: Node): boolean { | ||
const { containsString, areOperatorsValid } = treeToArray(node); | ||
return containsString && areOperatorsValid; | ||
} | ||
|
||
function transformTreeToArray(node: Node): ReadonlyArray<Expression> { | ||
return treeToArray(node).nodes; | ||
} | ||
|
||
function treeToArray(node: Node): { nodes: ReadonlyArray<Expression>, containsString: boolean, areOperatorsValid: boolean} { | ||
if (isBinaryExpression(node)) { | ||
const { nodes: leftNodes, containsString: leftHasString, areOperatorsValid: leftOperatorValid } = treeToArray(node.left); | ||
const { nodes: rightNodes, containsString: rightHasString, areOperatorsValid: rightOperatorValid } = treeToArray(node.right); | ||
|
||
if (!leftHasString && !rightHasString) { | ||
return { nodes: [node], containsString: false, areOperatorsValid: true }; | ||
} | ||
|
||
const nodeOperatorValid = node.operatorToken.kind === SyntaxKind.PlusToken; | ||
const isPlus = leftOperatorValid && nodeOperatorValid && rightOperatorValid; | ||
|
||
return { nodes: leftNodes.concat(rightNodes), containsString: true, areOperatorsValid: isPlus }; | ||
} | ||
|
||
return { nodes: [node as Expression], containsString: isStringLiteral(node), areOperatorsValid: true }; | ||
} | ||
|
||
function createHead(nodes: ReadonlyArray<Expression>): [number, TemplateHead] { | ||
let begin = 0; | ||
let text = ""; | ||
|
||
while (begin < nodes.length && isStringLiteral(nodes[begin])) { | ||
const next = nodes[begin] as StringLiteral; | ||
text = text + decodeRawString(next.getText()); | ||
begin++; | ||
} | ||
|
||
text = escapeText(text); | ||
return [begin, createTemplateHead(text)]; | ||
} | ||
|
||
function nodesToTemplate(nodes: ReadonlyArray<Expression>) { | ||
const templateSpans: TemplateSpan[] = []; | ||
const [begin, head] = createHead(nodes); | ||
|
||
if (begin === nodes.length) { | ||
return createNoSubstitutionTemplateLiteral(head.text); | ||
} | ||
|
||
for (let i = begin; i < nodes.length; i++) { | ||
let current = nodes[i]; | ||
let text = ""; | ||
|
||
while (i + 1 < nodes.length && isStringLiteral(nodes[i + 1])) { | ||
const next = nodes[i + 1] as StringLiteral; | ||
text = text + decodeRawString(next.getText()); | ||
i++; | ||
} | ||
|
||
text = escapeText(text); | ||
const templatePart = i === nodes.length - 1 ? createTemplateTail(text) : createTemplateMiddle(text); | ||
|
||
if (isParenthesizedExpression(current)) current = current.expression; | ||
templateSpans.push(createTemplateSpan(current, templatePart)); | ||
} | ||
|
||
return createTemplateExpression(head, templateSpans); | ||
} | ||
|
||
const hexToUnicode = (_match: string, grp: string) => String.fromCharCode(parseInt(grp, 16)); | ||
const octalToUnicode = (_match: string, grp: string) => String.fromCharCode(parseInt(grp, 8)); | ||
|
||
function decodeRawString(content: string) { | ||
const outerQuotes = /"((.|\s)*)"/; | ||
const unicodeEscape = /\\u([\d\w]+)/gi; | ||
const unicodeEscapeWithBraces = /\\u\{([\d\w]+\})/gi; | ||
const hexEscape = /\\x([\d\w]+)/gi; | ||
const octalEscape = /\\([0-7]+)/g; | ||
|
||
return content.replace(outerQuotes, (_match, grp) => grp) | ||
.replace(unicodeEscape, hexToUnicode) | ||
.replace(unicodeEscapeWithBraces, hexToUnicode) | ||
.replace(hexEscape, hexToUnicode) | ||
.replace(octalEscape, octalToUnicode); | ||
|
||
} | ||
|
||
function escapeText(content: string) { | ||
return content.replace("`", "\`") // back-tick | ||
.replace("\${", `$\\{`); // placeholder alike beginning | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringAsFnArgument.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// console.log(`/*x*/f/*y*/oobar is ${ 32 } years old`) | ||
|
||
goTo.select("x", "y"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert string concatenation or template literal", | ||
actionName: "Convert to string concatenation", | ||
actionDescription: "Convert to string concatenation", | ||
newContent: | ||
`console.log("foobar is " + 32 + " years old")`, | ||
}); |
29 changes: 29 additions & 0 deletions
29
tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringAvailability.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const age = 22 | ||
//// const name = "Eddy" | ||
//// const /*z*/f/*y*/oo = /*x*/`/*w*/M/*v*/r/*u*/ /*t*/$/*s*/{ /*r*/n/*q*/ame } is ${ /*p*/a/*o*/ge + 34 } years old` | ||
|
||
goTo.select("z", "y"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); | ||
|
||
goTo.select("x", "w"); | ||
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); | ||
|
||
goTo.select("v", "u"); | ||
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); | ||
|
||
goTo.select("t", "s"); | ||
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); | ||
|
||
goTo.select("r", "q"); | ||
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); | ||
|
||
goTo.select("p", "o"); | ||
verify.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); |
17 changes: 17 additions & 0 deletions
17
tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringAvailabilityTagged.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// function tag(literals: TemplateStringsArray, ...placeholders: string[]) { return "tagged" } | ||
//// const alpha = tag/*z*/`/*y*/foobar` | ||
//// const beta = tag/*x*/`/*w*/foobar ${/*v*/4/*u*/2}` | ||
|
||
goTo.select("z", "y"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); | ||
|
||
goTo.select("x", "w"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); | ||
|
||
goTo.select("v", "u"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to string concatenation"); | ||
verify.not.refactorAvailable("Convert string concatenation or template literal", "Convert to template literal"); |
12 changes: 12 additions & 0 deletions
12
tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringBackTick.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = `/*x*/w/*y*/ith back\`tick` | ||
|
||
goTo.select("x", "y"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert string concatenation or template literal", | ||
actionName: "Convert to string concatenation", | ||
actionDescription: "Convert to string concatenation", | ||
newContent: | ||
"const foo = \"with back`tick\"", | ||
}); |
12 changes: 12 additions & 0 deletions
12
tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringBinaryExpr.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = `/*x*/f/*y*/oobar is ${ 42 + 6 } years old` | ||
|
||
goTo.select("x", "y"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert string concatenation or template literal", | ||
actionName: "Convert to string concatenation", | ||
actionDescription: "Convert to string concatenation", | ||
newContent: | ||
`const foo = "foobar is " + (42 + 6) + " years old"`, | ||
}); |
12 changes: 12 additions & 0 deletions
12
tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringDollar.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = `/*x*/w/*y*/ith \${dollar}` | ||
|
||
goTo.select("x", "y"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert string concatenation or template literal", | ||
actionName: "Convert to string concatenation", | ||
actionDescription: "Convert to string concatenation", | ||
newContent: | ||
"const foo = \"with \${dollar}\"", | ||
}); |
12 changes: 12 additions & 0 deletions
12
tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringExprInRow.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const foo = `/*x*/f/*y*/oobar is ${ 42 }${ 6 } years old` | ||
|
||
goTo.select("x", "y"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert string concatenation or template literal", | ||
actionName: "Convert to string concatenation", | ||
actionDescription: "Convert to string concatenation", | ||
newContent: | ||
`const foo = "foobar is " + 42 + 6 + " years old"`, | ||
}); |
16 changes: 16 additions & 0 deletions
16
tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringMultiExpr.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const age = 22 | ||
//// const name = "Eddy" | ||
//// const foo = `/*x*/$/*y*/{ name } is ${ age } years old` | ||
|
||
goTo.select("x", "y"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert string concatenation or template literal", | ||
actionName: "Convert to string concatenation", | ||
actionDescription: "Convert to string concatenation", | ||
newContent: | ||
`const age = 22 | ||
const name = "Eddy" | ||
const foo = name + " is " + age + " years old"`, | ||
}); |
14 changes: 14 additions & 0 deletions
14
tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringNestedInner.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const age = 42 | ||
//// const foo = `foobar is a ${ age < 18 ? 'child' : /*x*/`/*y*/grown-up ${ age > 40 ? 'who needs probaply assistance' : ''}` }` | ||
|
||
goTo.select("x", "y"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert string concatenation or template literal", | ||
actionName: "Convert to string concatenation", | ||
actionDescription: "Convert to string concatenation", | ||
newContent: | ||
`const age = 42 | ||
const foo = \`foobar is a \${ age < 18 ? 'child' : "grown-up " + (age > 40 ? 'who needs probaply assistance' : '') }\``, | ||
}); |
14 changes: 14 additions & 0 deletions
14
tests/cases/fourslash/refactorConvertStringOrTemplateLiteral_ToStringNestedInnerNonSub.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// const age = 42 | ||
//// const foo = `foobar is a ${ `/*x*/3/*y*/4` }` | ||
|
||
goTo.select("x", "y"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert string concatenation or template literal", | ||
actionName: "Convert to string concatenation", | ||
actionDescription: "Convert to string concatenation", | ||
newContent: | ||
`const age = 42 | ||
const foo = \`foobar is a \${ "34" }\``, | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
node.getText()
is the correct solution to get the raw string content. To avoid unnecessary work, consider passing the SourceFile as argument to it.