Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: always return parserServices, add getParserServices from utils
BREAKING: previously plugins expected that `parserServices` would be `undefined` if full type information wasn't available.
- Loading branch information
1 parent
66f9741
commit 01de581
Showing
15 changed files
with
139 additions
and
138 deletions.
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
This file was deleted.
Oops, something went wrong.
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
40 changes: 40 additions & 0 deletions
40
packages/experimental-utils/src/eslint-utils/getParserServices.ts
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,40 @@ | ||
import { ParserServices, TSESLint } from '../'; | ||
|
||
const ERROR_MESSAGE = | ||
'You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.'; | ||
|
||
/** | ||
* Try to retrieve typescript parser service from context | ||
*/ | ||
export function getParserServices< | ||
TMessageIds extends string, | ||
TOptions extends readonly any[] | ||
>( | ||
context: TSESLint.RuleContext<TMessageIds, TOptions>, | ||
allowWithoutFullTypeInformation: boolean = false, | ||
): ParserServices { | ||
// backwards compatability check | ||
// old versions of the parser would not return any parserServices unless parserOptions.project was set | ||
if ( | ||
!context.parserServices || | ||
!context.parserServices.program || | ||
!context.parserServices.esTreeNodeToTSNodeMap || | ||
!context.parserServices.tsNodeToESTreeNodeMap | ||
) { | ||
throw new Error(ERROR_MESSAGE); | ||
} | ||
|
||
const hasFullTypeInformation = | ||
typeof context.parserServices.hasFullTypeInformation === 'boolean' | ||
? context.parserServices.hasFullTypeInformation | ||
: // backwards compatible | ||
true; | ||
|
||
// if a rule requries full type information, then hard fail if it doesn't exist | ||
// this forces the user to supply parserOptions.project | ||
if (!hasFullTypeInformation && !allowWithoutFullTypeInformation) { | ||
throw new Error(ERROR_MESSAGE); | ||
} | ||
|
||
return context.parserServices; | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './applyDefault'; | ||
export * from './batchedSingleLineTests'; | ||
export * from './getParserServices'; | ||
export * from './RuleCreator'; | ||
export * from './deepMerge'; |
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
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
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
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
Oops, something went wrong.