forked from import-js/eslint-plugin-import
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[utils] [new] add context compatibility helpers
This change adds helper functions to `eslint-module-utils` in order to add eslint v9 support to `eslint-plugin-import` in a backwards compatible way. Contributes to import-js#2996
- Loading branch information
1 parent
55add49
commit dd58fd6
Showing
5 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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,38 @@ | ||
import { Scope, SourceCode, Rule } from 'eslint'; | ||
import * as ESTree from 'estree'; | ||
|
||
type LegacyContext = { | ||
getFilename: () => string, | ||
getPhysicalFilename: () => string, | ||
getSourceCode: () => SourceCode, | ||
getScope: never, | ||
getAncestors: never, | ||
getDeclaredVariables: never, | ||
}; | ||
|
||
type NewContext = { | ||
filename: string, | ||
sourceCode: SourceCode, | ||
getPhysicalFilename?: () => string, | ||
getScope: () => Scope.Scope, | ||
getAncestors: () => ESTree.Node[], | ||
getDeclaredVariables: (node: ESTree.Node) => Scope.Variable[], | ||
}; | ||
|
||
export type Context = LegacyContext | NewContext | Rule.RuleContext; | ||
|
||
declare function getAncestors(context: Context, node: ESTree.Node): ESTree.Node[]; | ||
declare function getDeclaredVariables(context: Context, node: ESTree.Node): Scope.Variable[]; | ||
declare function getFilename(context: Context): string; | ||
declare function getPhysicalFilename(context: Context): string; | ||
declare function getScope(context: Context, node: ESTree.Node): Scope.Scope; | ||
declare function getSourceCode(context: Context): SourceCode; | ||
|
||
export { | ||
getAncestors, | ||
getDeclaredVariables, | ||
getFilename, | ||
getPhysicalFilename, | ||
getScope, | ||
getSourceCode, | ||
}; |
This file contains 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,72 @@ | ||
'use strict'; | ||
|
||
exports.__esModule = true; | ||
|
||
/** @type {import('./contextCompat').getAncestors} */ | ||
function getAncestors(context, node) { | ||
const sourceCode = getSourceCode(context); | ||
|
||
if (sourceCode && sourceCode.getAncestors) { | ||
return sourceCode.getAncestors(node); | ||
} | ||
|
||
return context.getAncestors(); | ||
} | ||
|
||
/** @type {import('./contextCompat').getDeclaredVariables} */ | ||
function getDeclaredVariables(context, node) { | ||
const sourceCode = getSourceCode(context); | ||
|
||
if (sourceCode && sourceCode.getDeclaredVariables) { | ||
return sourceCode.getDeclaredVariables(node); | ||
} | ||
|
||
return context.getDeclaredVariables(node); | ||
} | ||
|
||
/** @type {import('./contextCompat').getFilename} */ | ||
function getFilename(context) { | ||
if ('filename' in context) { | ||
return context.filename; | ||
} | ||
|
||
return context.getFilename(); | ||
} | ||
|
||
/** @type {import('./contextCompat').getPhysicalFilename} */ | ||
function getPhysicalFilename(context) { | ||
if (context.getPhysicalFilename) { | ||
return context.getPhysicalFilename(); | ||
} | ||
|
||
return getFilename(context); | ||
} | ||
|
||
/** @type {import('./contextCompat').getScope} */ | ||
function getScope(context, node) { | ||
const sourceCode = getSourceCode(context); | ||
|
||
if (sourceCode && sourceCode.getScope) { | ||
return sourceCode.getScope(node); | ||
} | ||
|
||
return context.getScope(); | ||
} | ||
|
||
/** @type {import('./contextCompat').getSourceCode} */ | ||
function getSourceCode(context) { | ||
if ('sourceCode' in context) { | ||
return context.sourceCode; | ||
} | ||
|
||
return context.getSourceCode(); | ||
} | ||
|
||
module.exports = { | ||
getAncestors, | ||
getDeclaredVariables, | ||
getFilename, | ||
getPhysicalFilename, | ||
getScope, | ||
getSourceCode, | ||
}; |
This file contains 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
This file contains 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