Skip to content
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

fix(fast_check): use as never instead of as any #424

Merged
merged 2 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/fast_check/swc_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ pub fn is_void_type(return_type: &TsType) -> bool {
is_keyword_type(return_type, TsKeywordTypeKind::TsVoidKeyword)
}

pub fn is_never_type(return_type: &TsType) -> bool {
is_keyword_type(return_type, TsKeywordTypeKind::TsNeverKeyword)
}

fn is_keyword_type(return_type: &TsType, kind: TsKeywordTypeKind) -> bool {
match return_type {
TsType::TsKeywordType(TsKeywordType { kind: k, .. }) => k == &kind,
Expand Down
31 changes: 9 additions & 22 deletions src/fast_check/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use super::swc_helpers::any_type_ann;
use super::swc_helpers::emit;
use super::swc_helpers::get_return_stmts_with_arg_from_function_body;
use super::swc_helpers::ident;
use super::swc_helpers::is_never_type;
use super::swc_helpers::is_void_type;
use super::swc_helpers::maybe_lit_to_ts_type;
use super::swc_helpers::ts_keyword_type;
Expand Down Expand Up @@ -626,9 +625,9 @@ impl<'a> FastCheckTransformer<'a> {
}
for arg in c.args.iter_mut() {
arg.expr = if arg.spread.is_some() {
Box::new(paren_expr(obj_as_any_expr()))
Box::new(paren_expr(obj_as_never_expr()))
} else {
obj_as_any_expr()
obj_as_never_expr()
};
}
true
Expand Down Expand Up @@ -1124,7 +1123,7 @@ impl<'a> FastCheckTransformer<'a> {
)?;
}
} else {
assign.right = array_as_any_expr();
assign.right = array_as_never_expr();
}
p.elems.clear();
}
Expand All @@ -1140,7 +1139,7 @@ impl<'a> FastCheckTransformer<'a> {
)?;
}
} else {
assign.right = obj_as_any_expr();
assign.right = obj_as_never_expr();
}
p.props.clear();
}
Expand Down Expand Up @@ -1220,7 +1219,7 @@ impl<'a> FastCheckTransformer<'a> {
span: DUMMY_SP,
type_ann: Box::new(t),
}));
n.init = Some(obj_as_any_expr());
n.init = Some(obj_as_never_expr());
}
None => {
let is_init_leavable = match n.init.as_mut() {
Expand All @@ -1240,7 +1239,7 @@ impl<'a> FastCheckTransformer<'a> {
}
}
} else {
n.init = Some(obj_as_any_expr());
n.init = Some(obj_as_never_expr());
}
}
Pat::Array(_)
Expand Down Expand Up @@ -1638,10 +1637,8 @@ fn void_or_promise_void(is_async: bool) -> Box<TsType> {
fn replacement_return_value(ty: &TsType) -> Option<Box<Expr>> {
if is_void_type(ty) {
None
} else if is_never_type(ty) {
Some(obj_as_never_expr())
} else {
Some(obj_as_any_expr())
Some(obj_as_never_expr())
}
}

Expand Down Expand Up @@ -1824,23 +1821,13 @@ fn is_expr_ident_or_member_idents(expr: &Expr) -> bool {
}
}

fn array_as_any_expr() -> Box<Expr> {
fn array_as_never_expr() -> Box<Expr> {
expr_as_keyword_expr(
Expr::Array(ArrayLit {
span: DUMMY_SP,
elems: Default::default(),
}),
TsKeywordTypeKind::TsAnyKeyword,
)
}

fn obj_as_any_expr() -> Box<Expr> {
expr_as_keyword_expr(
Expr::Object(ObjectLit {
span: DUMMY_SP,
props: Default::default(),
}),
TsKeywordTypeKind::TsAnyKeyword,
TsKeywordTypeKind::TsNeverKeyword,
)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/specs/graph/jsr/DynamicImport.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
}
}
}
const test: typeof import('./a.ts') = {} as any;
const test: typeof import('./a.ts') = {} as never;
export { test };
--- DTS ---
declare const test: typeof import('./a.ts');
Expand Down
6 changes: 3 additions & 3 deletions tests/specs/graph/jsr/FastCheck.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
export class Test {
/** Testing */ noReturnTypeVoid(param?: number, param2?: string): void {}
noReturnTypeAsync(): Promise<void> {
return {} as any;
return {} as never;
}
returnTypeGenerator(): Generator<unknown, any, any> {
return {} as any;
return {} as never;
}
hasReturnType(): A1 & RenamedA3 | A4 | A6 {
return {} as any;
return {} as never;
}
declare private inner: any;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/specs/graph/jsr/FastCheckCache_Basic.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,13 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
export class Test {
/** Testing */ noReturnTypeVoid(param?: number, param2?: string): void {}
noReturnTypeAsync(): Promise<void> {
return {} as any;
return {} as never;
}
returnTypeGenerator(): Generator<unknown, any, any> {
return {} as any;
return {} as never;
}
hasReturnType(): A1 & RenamedA3 | A4 | A6 {
return {} as any;
return {} as never;
}
declare private inner: any;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
Fast check https://jsr.io/@scope/b/1.0.0/mod.ts:
{}
export function add(a: number, b: number): number {
return {} as any;
return {} as never;
}

== fast check cache ==
Expand Down
76 changes: 76 additions & 0 deletions tests/specs/graph/jsr/FastCheck_AliasedNeverType.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# https://jsr.io/@scope/a/meta.json
{"versions": { "1.0.0": {} } }

# https://jsr.io/@scope/a/1.0.0_meta.json
{ "exports": { ".": "./mod.ts" } }

# https://jsr.io/@scope/a/1.0.0/mod.ts
export type NeverType = never;
/**
* This should have `return {} as never` instead of `as any`, which will error when type checking.
*/
export function myFunction(): NeverType {
Deno.exit(1);
}

# mod.ts
import 'jsr:@scope/a'

# output
{
"roots": [
"file:///mod.ts"
],
"modules": [
{
"kind": "esm",
"dependencies": [
{
"specifier": "jsr:@scope/a",
"code": {
"specifier": "jsr:@scope/a",
"span": {
"start": {
"line": 0,
"character": 7
},
"end": {
"line": 0,
"character": 21
}
}
}
}
],
"size": 22,
"mediaType": "TypeScript",
"specifier": "file:///mod.ts"
},
{
"kind": "esm",
"size": 198,
"mediaType": "TypeScript",
"specifier": "https://jsr.io/@scope/a/1.0.0/mod.ts"
}
],
"redirects": {
"jsr:@scope/a": "https://jsr.io/@scope/a/1.0.0/mod.ts"
},
"packages": {
"@scope/a": "@scope/a@1.0.0"
}
}

Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
{}
export type NeverType = never;
/**
* This should have `return {} as never` instead of `as any`, which will error when type checking.
*/ export function myFunction(): NeverType {
return {} as never;
}
--- DTS ---
export type NeverType = never;
/**
* This should have `return {} as never` instead of `as any`, which will error when type checking.
*/ export declare function myFunction(): NeverType;
2 changes: 1 addition & 1 deletion tests/specs/graph/jsr/FastCheck_ClassGettersAndSetters.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
{}
export class MyClass {
get value(): string {
return {} as any;
return {} as never;
}
set value(_value: string) {}
}
Expand Down
10 changes: 5 additions & 5 deletions tests/specs/graph/jsr/FastCheck_ClassProperties.txt
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,18 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
declare right: RedBlackNode<T> | null;
declare red: boolean;
constructor(parent: RedBlackNode<T> | null, value: T){
super({} as any, {} as any);
super({} as never, {} as never);
}
static override from<T>(node: RedBlackNode<T>): RedBlackNode<T> {
return {} as any;
return {} as never;
}
}
const isSecure: Symbol = {} as any;
const public2: Symbol = {} as any;
const isSecure: Symbol = {} as never;
const public2: Symbol = {} as never;
export class CookieMapBase {
declare [isSecure]: boolean;
[public2](): number {
return {} as any;
return {} as never;
}
}
export class Bar {
Expand Down
6 changes: 3 additions & 3 deletions tests/specs/graph/jsr/FastCheck_ClassPropertiesLeavable.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
}
import type { Foo, Bar } from "./b.ts";
export class Foo {
fizz = (a: string): string =>({} as any);
foo = (): Foo =>({} as any);
bar = (b: Bar): number =>({} as any);
fizz = (a: string): string =>({} as never);
foo = (): Foo =>({} as never);
bar = (b: Bar): number =>({} as never);
bax = function(b: Bax): void {};
}
--- DTS ---
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/graph/jsr/FastCheck_ClassSuper.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
}
export class Child extends Base {
constructor(){
super({} as any, {} as any);
super({} as never, {} as never);
}
}
class SpreadBase {
constructor(...params: string[]){}
}
export class SpreadChild extends SpreadBase {
constructor(){
super(...({} as any));
super(...({} as never));
}
}
--- DTS ---
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/graph/jsr/FastCheck_Comments.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ import 'jsr:@scope/a'
Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
{}
// @ts-ignore broken line
export const value1: number = {} as any;
export const value1: number = {} as never;
--- DTS ---
// @ts-ignore broken line
export declare const value1: number;
2 changes: 1 addition & 1 deletion tests/specs/graph/jsr/FastCheck_DefaultExportExprVar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
export interface $Type {
prop: boolean;
}
export const $: $Type = {} as any;
export const $: $Type = {} as never;
export default $;
--- DTS ---
export interface $Type {
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/graph/jsr/FastCheck_Enums.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
Value1 = "a",
Value2 = "b"
}
const value: number = {} as any;
const value: number = {} as never;
export enum EnumWithNonConstInits {
Value1 = new Public1().test,
Value2 = new Public2().asdf * value + NonExportedEnum.Value
Expand All @@ -120,7 +120,7 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
enum NonExportedEnum {
Value
}
const NUM: any = {} as any;
const NUM: any = {} as never;
export enum Foo1 {
A = 1,
B = "2",
Expand Down
24 changes: 12 additions & 12 deletions tests/specs/graph/jsr/FastCheck_Functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,51 +128,51 @@ Fast check https://jsr.io/@scope/a/1.0.0/mod.ts:
{}
export function test1(): void {}
export function test2(): number {
return {} as any;
return {} as never;
}
/** Some Doc **/ export function test3(param: number): number {
return {} as any;
return {} as never;
}
export function test4(param?: number): number {
return {} as any;
return {} as never;
}
export function test5<T extends PublicOther>(param?: number): T {
return {} as any;
return {} as never;
}
export function test6<T = PublicOther>(param?: number): T {
return {} as any;
return {} as never;
}
export function test7(param?: number): number;
export function test7(param?: number, param2?: PublicOther2): number;
export function test7(param0?: any, param1?: any): any {
return {} as any;
return {} as never;
}
function test8(param: number): number;
function test8(param: string): string;
function test8(param0?: any): any {
return {} as any;
return {} as never;
}
export { test8 };
export default function test9(param: number): number;
export default function test9(param: string): string;
export default function test9(param0?: any): any {
return {} as any;
return {} as never;
}
export function test10(...params: string[]): string[] {
return {} as any;
return {} as never;
}
export function test11(options = {
copy: true
}): void {}
export interface GlobOptions {
}
export function test12({}: GlobOptions = {} as any): void {}
export function test13([]: number[] = [] as any): void {}
export function test12({}: GlobOptions = {} as never): void {}
export function test13([]: number[] = [] as never): void {}
export function test14(): never {
return {} as never;
}
export function test15(): Generator<number> {
return {} as any;
return {} as never;
}
class PublicOther {
}
Expand Down
Loading