-
-
Notifications
You must be signed in to change notification settings - Fork 39
chore: add type tests for core package
#137
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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,9 @@ | ||
| { | ||
| "extends": "../../tsconfig.json", | ||
| "compilerOptions": { | ||
| "noEmit": true, | ||
| "rootDir": "../..", | ||
| "strict": true | ||
| }, | ||
| "files": ["../../dist/esm/types.d.ts", "types.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,241 @@ | ||
| /** | ||
| * @fileoverview Type tests for ESLint Core. | ||
| * @author Francesco Trotta | ||
| */ | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Imports | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| import type { | ||
| File, | ||
| FileProblem, | ||
| Language, | ||
| LanguageContext, | ||
| LanguageOptions, | ||
| OkParseResult, | ||
| ParseResult, | ||
| RuleContext, | ||
| RuleDefinition, | ||
| RulesConfig, | ||
| RulesMeta, | ||
| RuleTextEdit, | ||
| RuleTextEditor, | ||
| RuleVisitor, | ||
| SourceLocation, | ||
| SourceRange, | ||
| TextSourceCode, | ||
| TraversalStep, | ||
| } from "@eslint/core"; | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Helper types | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| interface TestNode { | ||
| type: string; | ||
| start: number; | ||
| lenght: number; | ||
| } | ||
|
|
||
| interface TestRootNode { | ||
| type: "root"; | ||
| start: number; | ||
| length: number; | ||
| } | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Tests for shared types | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| interface TestLanguageOptions extends LanguageOptions { | ||
| howMuch?: "yes" | "no" | boolean; | ||
| } | ||
|
|
||
| class TestSourceCode | ||
| implements | ||
| TextSourceCode<{ | ||
| LangOptions: TestLanguageOptions; | ||
| RootNode: TestRootNode; | ||
| SyntaxElementWithLoc: unknown; | ||
| ConfigNode: unknown; | ||
| }> | ||
| { | ||
| text: string; | ||
| ast: TestRootNode; | ||
| notMuch: "no" | false; | ||
| visitorKeys?: Record<string, string[]> | undefined; | ||
|
|
||
| constructor(text: string, ast: TestRootNode) { | ||
| this.text = text; | ||
| this.ast = ast; | ||
| this.notMuch = false; | ||
| } | ||
|
|
||
| /* eslint-disable class-methods-use-this -- not all methods need `this` */ | ||
|
|
||
| getLoc(syntaxElement: { start: number; length: number }): SourceLocation { | ||
| return { | ||
| start: { line: 1, column: syntaxElement.start + 1 }, | ||
| end: { | ||
| line: 1, | ||
| column: syntaxElement.start + 1 + syntaxElement.length, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| getRange(syntaxElement: { start: number; length: number }): SourceRange { | ||
| return [ | ||
| syntaxElement.start, | ||
| syntaxElement.start + syntaxElement.length, | ||
| ]; | ||
| } | ||
|
|
||
| *traverse(): Iterable<TraversalStep> { | ||
| // To be implemented. | ||
| } | ||
|
|
||
| applyLanguageOptions(languageOptions: TestLanguageOptions): void { | ||
| if (languageOptions.howMuch === "yes") { | ||
| this.notMuch = "no"; | ||
| } | ||
| } | ||
|
|
||
| applyInlineConfig(): { | ||
| configs: { loc: SourceLocation; config: { rules: RulesConfig } }[]; | ||
| problems: FileProblem[]; | ||
| } { | ||
| throw new Error("Method not implemented."); | ||
| } | ||
|
|
||
| /* eslint-enable class-methods-use-this -- not all methods need `this` */ | ||
| } | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Tests for language-related types | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| interface TestNormalizedLanguageOptions extends TestLanguageOptions { | ||
| howMuch: boolean; // option is required and must be a boolean | ||
| } | ||
|
|
||
| const testLanguage: Language = { | ||
| fileType: "text", | ||
| lineStart: 1, | ||
| columnStart: 1, | ||
| nodeTypeKey: "type", | ||
|
|
||
| validateLanguageOptions(languageOptions: TestLanguageOptions): void { | ||
| if ( | ||
| !["yes", "no", true, false, undefined].includes( | ||
| languageOptions.howMuch, | ||
| ) | ||
| ) { | ||
| throw Error("Invalid options."); | ||
| } | ||
| }, | ||
|
|
||
| normalizeLanguageOptions( | ||
| languageOptions: TestLanguageOptions, | ||
| ): TestNormalizedLanguageOptions { | ||
| const { howMuch } = languageOptions; | ||
| return { howMuch: howMuch === "yes" || howMuch === true }; | ||
| }, | ||
|
|
||
| parse( | ||
| file: File, | ||
| context: { languageOptions: TestNormalizedLanguageOptions }, | ||
| ): ParseResult<TestRootNode> { | ||
| context.languageOptions.howMuch satisfies boolean; | ||
| return { | ||
| ok: true, | ||
| ast: { | ||
| type: "root", | ||
| start: 0, | ||
| length: file.body.length, | ||
| }, | ||
| }; | ||
| }, | ||
|
|
||
| createSourceCode( | ||
| file: File, | ||
| input: OkParseResult<TestRootNode>, | ||
| context: LanguageContext<TestNormalizedLanguageOptions>, | ||
| ): TestSourceCode { | ||
| context.languageOptions.howMuch satisfies boolean; | ||
| return new TestSourceCode(String(file.body), input.ast); | ||
| }, | ||
| }; | ||
|
|
||
| testLanguage.defaultLanguageOptions satisfies LanguageOptions | undefined; | ||
|
|
||
| //----------------------------------------------------------------------------- | ||
| // Tests for rule-related types | ||
| //----------------------------------------------------------------------------- | ||
|
|
||
| interface TestRuleVisitor extends RuleVisitor { | ||
| Node?: (node: TestNode) => void; | ||
| } | ||
|
|
||
| type TestRuleContext = RuleContext<{ | ||
| LangOptions: TestLanguageOptions; | ||
| Code: TestSourceCode; | ||
| RuleOptions: [{ foo: string; bar: number }]; | ||
| Node: TestNode; | ||
| }>; | ||
|
|
||
| const testRule: RuleDefinition<{ | ||
| LangOptions: TestLanguageOptions; | ||
| Code: TestSourceCode; | ||
| RuleOptions: [{ foo: string; bar: number }]; | ||
| Visitor: TestRuleVisitor; | ||
| Node: TestNode; | ||
| MessageIds: "badFoo" | "wrongBar"; | ||
| ExtRuleDocs: never; | ||
| }> = { | ||
| meta: { | ||
| type: "problem", | ||
| fixable: "code", | ||
| messages: { | ||
| badFoo: "change this foo", | ||
| wrongBar: "fix this bar", | ||
| }, | ||
| }, | ||
|
|
||
| create(context: TestRuleContext): TestRuleVisitor { | ||
| return { | ||
| Foo(node: TestNode) { | ||
| // node.type === "Foo" | ||
| context.report({ | ||
| messageId: "badFoo", | ||
| loc: { | ||
| start: { line: node.start, column: 1 }, | ||
| end: { line: node.start + 1, column: Infinity }, | ||
| }, | ||
| fix(fixer: RuleTextEditor): RuleTextEdit { | ||
| return fixer.replaceText( | ||
| node, | ||
| context.languageOptions.howMuch === "yes" | ||
| ? "👍" | ||
| : "👎", | ||
| ); | ||
| }, | ||
| }); | ||
| }, | ||
| Bar(node: TestNode) { | ||
| // node.type === "Bar" | ||
| context.report({ | ||
| message: "This bar is foobar", | ||
| node, | ||
| suggest: [ | ||
| { | ||
| messageId: "Bar", | ||
| }, | ||
| ], | ||
| }); | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| testRule.meta satisfies RulesMeta | undefined; | ||
Oops, something went wrong.
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.