-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
fix(utils): improve error message on typed rule with invalid parser #8146
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
Merged
JoshuaKGoldberg
merged 4 commits into
typescript-eslint:main
from
JoshuaKGoldberg:utils-assert-our-parser
Jan 7, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e22e20f
feat(utils): throw error on typed rule with invalid parser
JoshuaKGoldberg a75caa1
Switch to more informative error message
JoshuaKGoldberg 7b30a3a
fix tests
JoshuaKGoldberg 649917f
Merge branch 'main' into utils-assert-our-parser
JoshuaKGoldberg 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
3 changes: 3 additions & 0 deletions
3
packages/utils/src/eslint-utils/parserPathSeemsToBeTSESLint.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,3 @@ | ||
export function parserPathSeemsToBeTSESLint(parserPath: string): boolean { | ||
return /(?:typescript-eslint|\.\.)[\w/\\]*parser/.test(parserPath); | ||
} |
107 changes: 107 additions & 0 deletions
107
packages/utils/tests/eslint-utils/getParserServices.test.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,107 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any, deprecation/deprecation -- wild and wacky testing */ | ||
import type * as ts from 'typescript'; | ||
|
||
import type { ParserServices, TSESLint, TSESTree } from '../../src'; | ||
import { ESLintUtils } from '../../src'; | ||
|
||
type UnknownRuleContext = Readonly<TSESLint.RuleContext<string, unknown[]>>; | ||
|
||
const defaults = { | ||
parserPath: '@typescript-eslint/parser/dist/index.js', | ||
parserServices: { | ||
esTreeNodeToTSNodeMap: new Map<TSESTree.Node, ts.Node>(), | ||
program: {}, | ||
tsNodeToESTreeNodeMap: new Map<ts.Node, TSESTree.Node>(), | ||
} as unknown as ParserServices, | ||
}; | ||
|
||
const createMockRuleContext = ( | ||
overrides: Partial<UnknownRuleContext> = {}, | ||
): UnknownRuleContext => | ||
({ | ||
...defaults, | ||
...overrides, | ||
}) as unknown as UnknownRuleContext; | ||
|
||
describe('getParserServices', () => { | ||
it('throws a standard error when parserOptions.esTreeNodeToTSNodeMap is missing and the parser is known', () => { | ||
const context = createMockRuleContext({ | ||
parserServices: { | ||
...defaults.parserServices, | ||
esTreeNodeToTSNodeMap: undefined as any, | ||
}, | ||
}); | ||
|
||
expect(() => ESLintUtils.getParserServices(context)).toThrow( | ||
new Error( | ||
'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.', | ||
), | ||
); | ||
}); | ||
|
||
it('throws an augment error when parserOptions.esTreeNodeToTSNodeMap is missing and the parser is unknown', () => { | ||
const context = createMockRuleContext({ | ||
parserPath: '@babel/parser.js', | ||
parserServices: { | ||
...defaults.parserServices, | ||
esTreeNodeToTSNodeMap: undefined as any, | ||
}, | ||
}); | ||
|
||
expect(() => ESLintUtils.getParserServices(context)).toThrow( | ||
new Error( | ||
'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.\n' + | ||
'Note: detected a parser other than @typescript-eslint/parser. Make sure the parser is configured to forward "parserOptions.project" to @typescript-eslint/parser.', | ||
), | ||
); | ||
}); | ||
|
||
it('throws an error when parserOptions.tsNodeToESTreeNodeMap is missing', () => { | ||
const context = createMockRuleContext({ | ||
parserServices: { | ||
...defaults.parserServices, | ||
tsNodeToESTreeNodeMap: undefined as any, | ||
}, | ||
}); | ||
|
||
expect(() => ESLintUtils.getParserServices(context)).toThrow( | ||
new Error( | ||
'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.', | ||
), | ||
); | ||
}); | ||
|
||
it('throws an error when parserServices.program is missing and allowWithoutFullTypeInformation is false', () => { | ||
const context = createMockRuleContext({ | ||
parserServices: { | ||
...defaults.parserServices, | ||
program: undefined as any, | ||
}, | ||
}); | ||
|
||
expect(() => ESLintUtils.getParserServices(context)).toThrow( | ||
new Error( | ||
'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.', | ||
), | ||
); | ||
}); | ||
|
||
it('returns when parserServices.program is missing and allowWithoutFullTypeInformation is true', () => { | ||
const context = createMockRuleContext({ | ||
parserServices: { | ||
...defaults.parserServices, | ||
program: undefined as any, | ||
}, | ||
}); | ||
|
||
expect(ESLintUtils.getParserServices(context, true)).toBe( | ||
context.parserServices, | ||
); | ||
}); | ||
|
||
it('returns when parserServices is filled out', () => { | ||
const context = createMockRuleContext(); | ||
|
||
expect(ESLintUtils.getParserServices(context)).toBe(context.parserServices); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
packages/utils/tests/eslint-utils/parserPathSeemsToBeTSESLint.test.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,24 @@ | ||
import { parserPathSeemsToBeTSESLint } from '../../src/eslint-utils/parserPathSeemsToBeTSESLint'; | ||
|
||
describe('parserPathSeemsToBeTSESLint', () => { | ||
test.each([ | ||
['local.js', false], | ||
['../other.js', false], | ||
['@babel/eslint-parser/lib/index.cjs', false], | ||
['@babel\\eslint-parser\\lib\\index.cjs', false], | ||
['node_modules/@babel/eslint-parser/lib/index.cjs', false], | ||
['../parser/dist/index.js', true], | ||
['../@typescript-eslint/parser/dist/index.js', true], | ||
['@typescript-eslint/parser/dist/index.js', true], | ||
['@typescript-eslint\\parser\\dist\\index.js', true], | ||
['node_modules/@typescript-eslint/parser/dist/index.js', true], | ||
['/path/to/typescript-eslint/parser/dist/index.js', true], | ||
['/path/to/typescript-eslint/parser/index.js', true], | ||
['/path/to/typescript-eslint/packages/parser/dist/index.js', true], | ||
['/path/to/typescript-eslint/packages/parser/index.js', true], | ||
['/path/to/@typescript-eslint/packages/parser/dist/index.js', true], | ||
['/path/to/@typescript-eslint/packages/parser/index.js', true], | ||
])('%s', (parserPath, expected) => { | ||
expect(parserPathSeemsToBeTSESLint(parserPath)).toBe(expected); | ||
}); | ||
}); |
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.
Uh oh!
There was an error while loading. Please reload this page.