-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(utils): improve error message on typed rule with invalid parser (#…
…8146) * feat(utils): throw error on typed rule with invalid parser * Switch to more informative error message * fix tests
- Loading branch information
1 parent
5a33c2b
commit 687f73c
Showing
4 changed files
with
152 additions
and
3 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
3 changes: 3 additions & 0 deletions
3
packages/utils/src/eslint-utils/parserPathSeemsToBeTSESLint.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,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 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 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); | ||
}); | ||
}); |