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

Approach to dealing with non-strict-boolean-expressions #199

Closed
nokome opened this issue Aug 1, 2019 · 2 comments · Fixed by #392
Closed

Approach to dealing with non-strict-boolean-expressions #199

nokome opened this issue Aug 1, 2019 · 2 comments · Fixed by #392

Comments

@nokome
Copy link
Member

nokome commented Aug 1, 2019

In .eslintrc we turn off strict-boolean-expressions:

{
  "extends": "@stencila/eslint-config",
  "rules": {
    "@typescript-eslint/strict-boolean-expressions": 0
  }
}

If you renable it, you get 447 errors like:

/home/nokome/stencila/source/encoda/src/util/xml.ts
   24:6   error  Unexpected non-boolean in conditional  @typescript-eslint/strict-boolean-expressions

We've had a discussion in stencila/dev-config#4 regarding whether check is too strict or not. We've also come across a bug which would have been avoided if this check was enabled: #197

Many (most?) of these Unexpected non-boolean in conditional errors are in places like this when decoding:

let { title, authors, orig_nbformat, ...rest } = notebook.metadata
if (!title) title = 'Untitled'
if (!authors) authors = []

which could be refactored to

const { title = 'Untitled', authors = [], orig_nbformat, ...rest } = notebook.metadata

or

function decodeName(name: xml.Element): stencila.Person {
const person: stencila.Person = { type: 'Person' }
const givenNames = child(name, 'given-names')
if (givenNames) person.givenNames = text(givenNames).split(/\s+/)
const surname = child(name, 'surname')
if (surname) person.familyNames = text(surname).split(/\s+/)
const prefix = child(name, 'prefix')
if (prefix) person.honorificPrefix = text(prefix)
const suffix = child(name, 'suffix')
if (suffix) person.honorificSuffix = text(suffix)
return person

which with compacting factory functions could now be refactored to:

const surname = child(name, 'surname')
const familyNames = surname ? text(surname).split(/\s+/) : undefined
//... etc
return stencila.person({
  givenNames,
  familyNames,
  honorificPrefix,
  honorificSuffix
})

So, I propose to:

  • renable strict-boolean-expressions at the package level
  • add /* eslint-disable @typescript-eslint/strict-boolean-expressions */ to every file that currently needs it
  • as we tidy up and refactor each codec remove dangerous expressions and eventually remove these disabling comment
@nokome
Copy link
Member Author

nokome commented Sep 13, 2019

Just to note that Typescript 3.7, due out in early November, will have null coalescing and optional chaining which will allow us to refactor away many of the existing non-strict boolean expressions e.g.

const format = options.format || 'html'

to

const format = options.format ?? 'html'

nokome added a commit that referenced this issue Jan 22, 2020
Closes #199: fixes linting errors in files with 3 or less and adds a `eslint-disable` (with accompanying note) to other files.
@stencila-ci
Copy link
Collaborator

🎉 This issue has been resolved in version 0.83.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants