Skip to content

Commit

Permalink
Avoid deprecated context.getSourceCode()
Browse files Browse the repository at this point in the history
Closes #145.
  • Loading branch information
lydell committed Feb 3, 2024
1 parent c31798c commit d23d59a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
ExportAllDeclaration: addParent,

"Program:exit": () => {
const sourceCode = context.getSourceCode();
const sourceCode = shared.getSourceCode(context);
for (const parent of parents) {
for (const chunk of shared.extractChunks(parent, (node, lastNode) =>
isPartOfChunk(node, lastNode, sourceCode)
Expand All @@ -50,7 +50,7 @@ module.exports = {
};

function maybeReportChunkSorting(chunk, context) {
const sourceCode = context.getSourceCode();
const sourceCode = shared.getSourceCode(context);
const items = shared.getImportExportItems(
chunk,
sourceCode,
Expand All @@ -67,7 +67,7 @@ function maybeReportChunkSorting(chunk, context) {
function maybeReportExportSpecifierSorting(node, context) {
const sorted = shared.printWithSortedSpecifiers(
node,
context.getSourceCode(),
shared.getSourceCode(context),
getSpecifiers
);
const [start, end] = node.range;
Expand Down
2 changes: 1 addition & 1 deletion src/imports.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module.exports = {
};

function maybeReportChunkSorting(chunk, context, outerGroups) {
const sourceCode = context.getSourceCode();
const sourceCode = shared.getSourceCode(context);
const items = shared.getImportExportItems(
chunk,
sourceCode,
Expand Down
9 changes: 8 additions & 1 deletion src/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function extractChunks(parentNode, isPartOfChunk) {
}

function maybeReportSorting(context, sorted, start, end) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const original = sourceCode.getText().slice(start, end);
if (original !== sorted) {
context.report({
Expand Down Expand Up @@ -854,10 +854,17 @@ function flatMap(array, fn) {
return [].concat(...array.map(fn));
}

function getSourceCode(context) {
// `.getSourceCode()` is deprecated in favor of `.sourceCode`.
// We support both for now.
return context.sourceCode || context.getSourceCode();
}

module.exports = {
extractChunks,
flatMap,
getImportExportItems,
getSourceCode,
isPunctuator,
maybeReportSorting,
printSortedItems,
Expand Down

0 comments on commit d23d59a

Please sign in to comment.