-
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.
New testing helpers using
using
, replace old console mocking functi…
…ons. (#11177)
- Loading branch information
Showing
42 changed files
with
1,812 additions
and
1,573 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
Empty file.
Empty file.
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,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"strict": true, | ||
"target": "esnext" | ||
}, | ||
"include": ["file.ts", "react.tsx"] | ||
} |
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,14 @@ | ||
require("ts-node").register({ | ||
transpileOnly: true, | ||
compilerOptions: { | ||
// we need this to be nodenext in the tsconfig, because | ||
// @typescript-eslint/utils only seems to export ESM | ||
// in TypeScript's eyes, but it totally works | ||
module: "commonjs", | ||
moduleResolution: "node", | ||
}, | ||
}); | ||
|
||
module.exports = { | ||
"require-using-disposable": require("./require-using-disposable").rule, | ||
}; |
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,5 @@ | ||
{ | ||
"scripts": { | ||
"test": "node -r ts-node/register/transpile-only --no-warnings --test --watch *.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,31 @@ | ||
import { rule } from "./require-using-disposable"; | ||
import { ruleTester } from "./testSetup"; | ||
|
||
ruleTester.run("require-using-disposable", rule, { | ||
valid: [ | ||
` | ||
function foo(): Disposable {} | ||
using bar = foo() | ||
`, | ||
` | ||
function foo(): AsyncDisposable {} | ||
await using bar = foo() | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
function foo(): Disposable {} | ||
const bar = foo() | ||
`, | ||
errors: [{ messageId: "missingUsing" }], | ||
}, | ||
{ | ||
code: ` | ||
function foo(): AsyncDisposable {} | ||
const bar = foo() | ||
`, | ||
errors: [{ messageId: "missingAwaitUsing" }], | ||
}, | ||
], | ||
}); |
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,63 @@ | ||
import { ESLintUtils } from "@typescript-eslint/utils"; | ||
import ts from "typescript"; | ||
import * as utils from "ts-api-utils"; | ||
|
||
export const rule = ESLintUtils.RuleCreator.withoutDocs({ | ||
create(context) { | ||
return { | ||
VariableDeclaration(node) { | ||
for (const declarator of node.declarations) { | ||
if (!declarator.init) continue; | ||
const services = ESLintUtils.getParserServices(context); | ||
const type = services.getTypeAtLocation(declarator.init); | ||
for (const typePart of parts(type)) { | ||
if (!utils.isObjectType(typePart) || !typePart.symbol) { | ||
continue; | ||
} | ||
if ( | ||
// bad check, but will do for now | ||
// in the future, we should check for a `[Symbol.disposable]` property | ||
// but I have no idea how to do that right now | ||
typePart.symbol.escapedName === "Disposable" && | ||
node.kind != "using" | ||
) { | ||
context.report({ | ||
messageId: "missingUsing", | ||
node: declarator, | ||
}); | ||
} | ||
if ( | ||
// similarly bad check | ||
typePart.symbol.escapedName === "AsyncDisposable" && | ||
node.kind != "await using" | ||
) { | ||
context.report({ | ||
messageId: "missingAwaitUsing", | ||
node: declarator, | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
messages: { | ||
missingUsing: | ||
"Disposables should be allocated with `using <disposable>`.", | ||
missingAwaitUsing: | ||
"AsyncDisposables should be allocated with `await using <disposable>`.", | ||
}, | ||
type: "suggestion", | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
}); | ||
|
||
function parts(type: ts.Type): ts.Type[] { | ||
return type.isUnion() | ||
? utils.unionTypeParts(type).flatMap(parts) | ||
: type.isIntersection() | ||
? utils.intersectionTypeParts(type).flatMap(parts) | ||
: [type]; | ||
} |
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,15 @@ | ||
import { RuleTester } from "@typescript-eslint/rule-tester"; | ||
import nodeTest from "node:test"; | ||
|
||
RuleTester.it = nodeTest.it; | ||
RuleTester.itOnly = nodeTest.only; | ||
RuleTester.describe = nodeTest.describe; | ||
RuleTester.afterAll = nodeTest.after; | ||
|
||
export const ruleTester = new RuleTester({ | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
project: "./tsconfig.json", | ||
tsconfigRootDir: __dirname + "/fixtures", | ||
}, | ||
}); |
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,7 @@ | ||
{ | ||
"extends": "@tsconfig/node20/tsconfig.json", | ||
"compilerOptions": { | ||
"allowJs": true, | ||
"noEmit": true | ||
} | ||
} |
Oops, something went wrong.