-
Notifications
You must be signed in to change notification settings - Fork 217
chore: improve types by reducing the any type usage and type casting #2771
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
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| export const isBrowser = | ||
| typeof window !== 'undefined' || | ||
| typeof process === 'undefined' || | ||
| (process?.platform as any) === 'browser'; // main and worker thread | ||
| (process?.platform as string) === 'browser'; // main and worker thread | ||
| export const env = isBrowser ? {} : process.env || {}; |
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 |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import { | |
| import { isNamedType, SpecExtension, type NormalizedNodeType } from './types/index.js'; | ||
| import type { OasRef } from './typings/openapi.js'; | ||
| import { getOwn } from './utils/get-own.js'; | ||
| import { isPlainObject } from './utils/is-plain-object.js'; | ||
| import { makeRefId } from './utils/make-ref-id.js'; | ||
| import { nextTick } from './utils/next-tick.js'; | ||
| import { readFileFromUrl } from './utils/read-file-from-url.js'; | ||
|
|
@@ -193,17 +194,17 @@ export type ResolvedRefMap = Map<string, ResolvedRef>; | |
|
|
||
| type RefFrame = { | ||
| prev: RefFrame | null; | ||
| node: any; | ||
| node: unknown; | ||
| }; | ||
|
|
||
| function pushRef(head: RefFrame, node: any): RefFrame { | ||
| function pushRef(head: RefFrame, node: unknown): RefFrame { | ||
| return { | ||
| prev: head, | ||
| node, | ||
| }; | ||
| } | ||
|
|
||
| function hasRef(head: RefFrame | null, node: any): boolean { | ||
| function hasRef(head: RefFrame | null, node: unknown): boolean { | ||
| while (head) { | ||
| if (head.node === node) { | ||
| return true; | ||
|
|
@@ -236,18 +237,18 @@ export async function resolveDocument(opts: { | |
| return resolvedRefMap; | ||
|
|
||
| function resolveRefsInParallel( | ||
| rootNode: any, | ||
| rootNode: unknown, | ||
| rootNodeDocument: Document, | ||
| rootNodePointer: string, | ||
| type: any | ||
| type: NormalizedNodeType | ||
| ) { | ||
| const rootNodeDocAbsoluteRef = rootNodeDocument.source.absoluteRef; | ||
| const anchorRefsMap: Map<string, any> = new Map(); | ||
| const anchorRefsMap: Map<string, unknown> = new Map(); | ||
|
|
||
| walk(rootNode, type, rootNodeDocAbsoluteRef + rootNodePointer); | ||
|
|
||
| function walk(node: any, type: NormalizedNodeType, nodeAbsoluteRef: string) { | ||
| if (typeof node !== 'object' || node === null) { | ||
| function walk(node: unknown, type: NormalizedNodeType, nodeAbsoluteRef: string) { | ||
| if (!isPlainObject(node) && !Array.isArray(node)) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -258,11 +259,6 @@ export async function resolveDocument(opts: { | |
|
|
||
| seenNodes.add(nodeId); | ||
|
|
||
| const [_, anchor] = Object.entries(node).find(([key]) => key === '$anchor') || []; | ||
| if (anchor) { | ||
| anchorRefsMap.set(`#${anchor}`, node); | ||
| } | ||
|
|
||
| if (Array.isArray(node)) { | ||
| const itemsType = type.items; | ||
| // we continue resolving unknown types, but stop early on known scalars | ||
|
|
@@ -291,6 +287,11 @@ export async function resolveDocument(opts: { | |
| return; | ||
| } | ||
|
|
||
| const [_, anchor] = Object.entries(node).find(([key]) => key === '$anchor') || []; | ||
| if (anchor) { | ||
| anchorRefsMap.set(`#${anchor}`, node); | ||
| } | ||
|
|
||
| for (const propName of Object.keys(node)) { | ||
| let propValue = node[propName]; | ||
| let propType = getOwn(type.properties, propName); | ||
|
|
@@ -342,7 +343,7 @@ export async function resolveDocument(opts: { | |
| if (isExternalValue(node)) { | ||
| const promise = followRef( | ||
| rootNodeDocument, | ||
| { $ref: node.externalValue }, | ||
| { $ref: node.externalValue as string }, | ||
| { | ||
| prev: null, | ||
| node, | ||
|
|
@@ -352,7 +353,7 @@ export async function resolveDocument(opts: { | |
| resolveRefsInParallel( | ||
| resolvedRef.node, | ||
| resolvedRef.document, | ||
| resolvedRef.nodePointer!, | ||
| resolvedRef.nodePointer, | ||
| type | ||
| ); | ||
| } | ||
|
|
@@ -420,33 +421,42 @@ export async function resolveDocument(opts: { | |
| nodePointer: '#/', | ||
| }; | ||
|
|
||
| let target = targetDoc.parsed as any; | ||
| let target = targetDoc.parsed; | ||
|
|
||
| const segments = pointer; | ||
| for (const segment of segments) { | ||
| if (typeof target !== 'object') { | ||
| target = undefined; | ||
| break; | ||
| } else if (target[segment] !== undefined) { | ||
| if (isPlainObject(target) && target[segment] !== undefined) { | ||
| target = target[segment]; | ||
| resolvedRef.nodePointer = joinPointer( | ||
| resolvedRef.nodePointer!, | ||
| escapePointerFragment(segment) | ||
| ); | ||
| } else if (Array.isArray(target) && target[+segment] !== undefined) { | ||
| target = target[+segment]; | ||
| resolvedRef.nodePointer = joinPointer( | ||
| resolvedRef.nodePointer!, | ||
| escapePointerFragment(segment) | ||
| ); | ||
| } else if (isRef(target)) { | ||
| resolvedRef = await followRef(targetDoc, target, pushRef(refStack, target)); | ||
| targetDoc = resolvedRef.document || targetDoc; | ||
|
|
||
| if (typeof resolvedRef.node !== 'object') { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to the |
||
| if (isPlainObject(resolvedRef.node)) { | ||
| target = resolvedRef.node[segment]; | ||
| resolvedRef.nodePointer = joinPointer( | ||
| resolvedRef.nodePointer!, | ||
| escapePointerFragment(segment) | ||
| ); | ||
| } else if (Array.isArray(resolvedRef.node)) { | ||
| target = resolvedRef.node[+segment]; | ||
| resolvedRef.nodePointer = joinPointer( | ||
| resolvedRef.nodePointer!, | ||
| escapePointerFragment(segment) | ||
| ); | ||
| } else { | ||
| target = undefined; | ||
| break; | ||
| } | ||
|
|
||
| target = resolvedRef.node[segment]; | ||
| resolvedRef.nodePointer = joinPointer( | ||
| resolvedRef.nodePointer!, | ||
| escapePointerFragment(segment) | ||
| ); | ||
| } else { | ||
| target = undefined; | ||
| break; | ||
|
|
||
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
3 changes: 2 additions & 1 deletion
3
packages/core/src/rules/common/no-invalid-parameter-examples.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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced the guard with explicit Array/Object detection.