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

chore: Remove pkg/driver //@ts-nocheck part 3 #19837

Merged
merged 25 commits into from
Feb 8, 2022

Conversation

sainthkh
Copy link
Contributor

User facing changelog

N/A. It's just removing // @ts-nocheck comments and defining types to avoid type errors.

Additional details

  • Why was this change necessary? => To make pkg/driver files more type-safe.
  • What is affected by this change? => N/A
  • Any implementation details to explain? => N/A

How has the user experience changed?

N/A

PR Tasks

  • [na] Have tests been added/updated?
  • [na] Has the original issue (or this PR, if no issue exists) been tagged with a release in ZenHub? (user-facing changes only)
  • [na] Has a PR for user-facing changes been opened in cypress-documentation?
  • [na] Have API changes been updated in the type definitions?
  • [na] Have new configuration options been added to the cypress.schema.json?

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jan 24, 2022

Thanks for taking the time to open a PR!

@sainthkh sainthkh marked this pull request as ready for review January 24, 2022 03:37
@sainthkh sainthkh requested a review from a team as a code owner January 24, 2022 03:37
@sainthkh sainthkh requested review from jennifer-shehane and removed request for a team January 24, 2022 03:37
@jennifer-shehane jennifer-shehane removed their request for review January 24, 2022 16:06
@@ -673,6 +679,8 @@ export default (Commands, Cypress, cy, state, config) => {
onFail: options._log,
args: { str },
})

return // Error is thrown above, it exists to satisfy typescript
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make this cleaner to just return what is on line 678?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type of throwErrByPath is void, and 678 always throws. So, line 683 is never called. If we remove this line, TypeScript shows all path doesn't return value error. That's why I left this comment.

microsoft/TypeScript#13219

In other languages like Java, there is throws keyword that handles this situation, but TypeScript isn't. The link above is the request to that feature. It seems that it's forgotten or given low priority.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this comment, if you update error_utils.throwsErrorByPath to return type never, this should be fine without the extra return type.

microsoft/TypeScript#13625 (comment)

@@ -685,9 +693,12 @@ export default (Commands, Cypress, cy, state, config) => {
}

$errUtils.throwErrByPath('go.invalid_argument', { onFail: options._log })

return // Error is thrown above, it exists to satisfy typescript
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@@ -15,7 +14,8 @@ const debug = debugFn('cypress:driver:command:type')
export default function (Commands, Cypress, cy, state, config) {
const { keyboard } = cy.devices

function type (subject, chars, options = {}) {
// TODO: change the type of `any` to `Partial<Cypress.TypeOptions>`
function type (subject, chars, options: any = {}) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is preventing you from making this change now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cypress often extends then user-given options with some internal values like in line 24-34.

options = _.defaults({}, userOptions, {
$el: subject,
log: true,
verify: true,
force: false,
delay: config('keystrokeDelay') || $Keyboard.getConfig().keystrokeDelay,
release: true,
parseSpecialCharSequences: true,
waitForAnimations: config('waitForAnimations'),
animationDistanceThreshold: config('animationDistanceThreshold'),
})

And TypeScript treats it as a type error. That's why it's commented.

By the way, I tried to rename user-given options and internal options as opts in #6459. But it was rejected and refactored in the current style. Maybe we need to find a way to name our internal option objects to solve this problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a small notes summarizing what you explained here? It makes sense to me but might not be obvious to others without doing a deeper drive into the code base on the work required here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that this comment exists on almost every command. So it's a bit hard to find the right place.

And I'll remove all of them after removing // @ts-nocheck comments. (I believe the part 4 will be the final.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I estimated when removing TODOs will be done. And it might take 1-2 months in current speed. So, I decided to add a note above.

I guess we need this somewhere in our code.

import _ from 'lodash'
import Promise from 'bluebird'

import $dom from '../../dom'
import $utils from '../../cypress/utils'
import $errUtils from '../../cypress/error_utils'
import $errUtils, { CypressError } from '../../cypress/error_utils'

const returnFalseIfThenable = (key, ...args) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const returnFalseIfThenable = (key, ...args) => {
const returnFalseIfThenable = (key, ...args): boolean => {

@@ -110,7 +108,7 @@ export default function (Commands, Cypress, cy, state, config) {
})
}

const getAndClear = (log, timeout, options = {}) => {
const getAndClear = (log?, timeout?, options = {}) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is log optional here? it's not optional for automateCookies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's called without them at line 164.

Comment on lines 16 to 19
let previousDomainVisited: boolean | null = null
let hasVisitedAboutBlank: boolean | null = null
let currentlyVisitingAboutBlank: boolean | null = null
let knownCommandCausedInstability: boolean | null = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the defaults should be updated to false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -17,9 +15,9 @@ const crossOriginScriptRe = /^script error/i

if (!Error.captureStackTrace) {
Error.captureStackTrace = (err, fn) => {
const stack = (new Error()).stack
const stack = (new Error()).stack;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const stack = (new Error()).stack;
const stack = (new Error()).stack

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

; is necessary here because JavaScript tries to translate .stack\n\n(err as Error) as a function call.

return _.startsWith(err.message, 'Invalid Chai property')
}

const isCypressErr = (err = {}) => {
const isCypressErr = (err: Error) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const isCypressErr = (err: Error) => {
const isCypressErr = (err: Error): boolean => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -197,20 +195,20 @@ const makeErrFromObj = (obj) => {
return err2
}

const throwErr = (err, options = {}) => {
const throwErr = (err, options: any = {}) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const throwErr = (err, options: any = {}) => {
const throwErr = (err, options: any = {}): never => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As never means a function never reaches its end, I had to create makeErrFromErr function.

@@ -227,7 +225,7 @@ const throwErr = (err, options = {}) => {
throw err
}

const throwErrByPath = (errPath, options = {}) => {
const throwErrByPath = (errPath, options: any = {}) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const throwErrByPath = (errPath, options: any = {}) => {
const throwErrByPath = (errPath, options: any = {}): never => {

@sainthkh sainthkh force-pushed the remove-ts-nocheck-part3 branch 2 times, most recently from 07d37f0 to f3fe6cd Compare January 31, 2022 02:54
ryanthemanuel
ryanthemanuel previously approved these changes Jan 31, 2022
import _ from 'lodash'

import { scrollTo } from './jquery.scrollto'
import $dom from '../dom'

// Add missing types.
interface ExtendedJQueryStatic extends JQueryStatic {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this caused typescript errors for individuals using JQuery methods other than find and expr?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm. this isn't the types provided via the cli! sorry aboutt that

emilyrohrbough
emilyrohrbough previously approved these changes Feb 1, 2022
@emilyrohrbough
Copy link
Member

@sainthkh can you take a look at the failing tests?

@sainthkh sainthkh force-pushed the remove-ts-nocheck-part3 branch 5 times, most recently from 8a25a22 to 6844eae Compare February 3, 2022 04:08
@sainthkh
Copy link
Contributor Author

sainthkh commented Feb 3, 2022

  • I fixed the type error by adding !. It's safe because defaultView isn't undefined.
  • I checked system-tests errors by adding empty commit. But it simply fails because of the timeouts. I cannot understand why. Sometimes, CircleCI fails when outside contributors use them. Maybe it's the case.

@sainthkh sainthkh force-pushed the remove-ts-nocheck-part3 branch from 6844eae to 4c9b618 Compare February 3, 2022 04:13
@sainthkh sainthkh force-pushed the remove-ts-nocheck-part3 branch from 4c9b618 to b47480f Compare February 3, 2022 04:23
@emilyrohrbough emilyrohrbough merged commit bf0d4db into cypress-io:develop Feb 8, 2022
tgriesser added a commit that referenced this pull request Feb 14, 2022
* develop:
  feat: gray out the path to system node in cypress run header (#20121)
  feat: redesign server errors (#20072)
  test: fix awesome-typescript-loader test and remove test-binary job (#20131)
  fix: Fix issues with stack traces and command log in Chrome 99 (#20049)
  fix: `cy.type(' ')` fires click event on button-like elements. (#20067)
  fix: `change`, `input` events are not fired when the same option is selected again. (#19623)
  build: publish vue3 on latest (#20099)
  chore: release @cypress/webpack-preprocessor-v5.11.1
  chore: release @cypress/webpack-dev-server-v1.8.1
  fix: detect newly added specs in dev-server compilation (#17950)
  chore: Remove pkg/driver //@ts-nocheck part 3 (#19837)
  chore: set up semantic-pull-request GitHub Action (#20091)
  chore: release @cypress/react-v5.12.2
  fix: remove nullish coalescing in js files to support node 12 (#20094)
  docs: update @cypress/webpack-preprocessor links (#19902)
  refactor: use aliases instead of meta (#19566)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants