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

Make optional options optional in typings and add type tests #622

Merged
merged 6 commits into from
Nov 22, 2023

Conversation

jdforsythe
Copy link
Contributor

@jdforsythe jdforsythe commented Nov 3, 2023

Purpose / Goal

See #612

This changes the type definitions to set all options with defaults to optional in the typings.

It adds documentation to all types so they show up in Intellisense when using the types.

It adds a type test - defining objects as the various options types (parse, validate, build) with none of the optional properties. Simply run npm test-types to attempt to compile the TS file and it will emit any type errors.

Example with no errors:

$ npm run test-types

> fast-xml-parser@4.3.2 test-types
> tsc --noEmit spec/typings/typings-test.ts

$ echo $?
0
## exit code 0 - no error

Example with an error - add someNewProperty to the type definition as non-optional:

fxp.d.ts:

type validationOptions = {
  /**
   * Whether to allow attributes without value
   * 
   * Defaults to `false`
   */
  allowBooleanAttributes?: boolean;
  
  /**
   * List of tags without closing tags
   * 
   * Defaults to `[]`
   */
  unpairedTags?: string[];

  // ADD A NEW PROPERTY - FORGET TO MAKE IT OPTIONAL
  /**
   * Some new property
   *
   * Defaults to `false`
   */
  someNewProperty: boolean;
};

The type test will now fail since the typings-test.ts file does not include this property in the validation options and it's listed as required:

$ npm run test-types

> fast-xml-parser@4.3.2 test-types
> tsc --noEmit spec/typings/typings-test.ts

spec/typings/typings-test.ts:41:7 - error TS2741: Property 'someNewProperty' is missing in type '{}' but required in type 'validationOptions'.

41 const validateOpts: validationOptions = {};
         ~~~~~~~~~~~~

  src/fxp.d.ts:234:2
    234   someNewProperty: boolean;
          ~~~~~~~~~~~~~~~~~~~~~~
    'someNewProperty' is declared here.


Found 1 error in spec/typings/typings-test.ts:41

$ echo $?
2
## exit code 2 - error

This will ensure the test fails if a property is added to the type as non-optional but not added to the typings-test.ts file as a required property.

Type

Please mention the type of PR

  • [ ]Bug Fix
  • [ ]Refactoring / Technology upgrade
  • [ ]New Feature

Note : Please ensure that you've read contribution guidelines before raising this PR. If your PR is in progress, please prepend [WIP] in PR title. Your PR will be reviewed when [WIP] will be removed from the PR title.

Bookmark this repository for further updates.

Copy link

guardrails bot commented Nov 3, 2023

⚠️ We detected 8 security issues in this pull request:

Vulnerable Libraries (8)
Severity Details
Medium pkg:npm/eslint@8.6.0 (t) upgrade to: > 8.6.0
Medium pkg:npm/@babel/core@7.16.7 (t) upgrade to: > 7.16.7
High pkg:npm/webpack@5.65.0 (t) upgrade to: 5.76.0
Medium pkg:npm/@babel/preset-env@7.16.7 (t) upgrade to: > 7.16.7
High pkg:npm/babel-loader@8.2.3 (t) upgrade to: > 8.2.3
High pkg:npm/cytorus@0.2.13 (t) upgrade to: > 0.2.13
Medium pkg:npm/@babel/plugin-transform-runtime@7.16.7 (t) upgrade to: > 7.16.7
Medium pkg:npm/@babel/register@7.16.7 (t) upgrade to: > 7.16.7

More info on how to fix Vulnerable Libraries in JavaScript.


👉 Go to the dashboard for detailed results.

📥 Happy? Share your feedback with us.

package-lock.json Outdated Show resolved Hide resolved
@amitguptagwl
Copy link
Member

Hi @jdforsythe
Yarn.lock or package-lock both files can't be considered in a PR due to security reasons so they need to be removed from the PR.

@jdforsythe
Copy link
Contributor Author

@amitguptagwl this has been fixed

@coveralls
Copy link

Coverage Status

coverage: 98.248%. remained the same
when pulling 451e895 on jdforsythe:type-tests
into fa5a733 on NaturalIntelligence:master.

@amitguptagwl amitguptagwl merged commit 68a4100 into NaturalIntelligence:master Nov 22, 2023
7 checks passed
@amitguptagwl
Copy link
Member

Thanks for this change. I'll confirm and publish soon.

@jdforsythe jdforsythe deleted the type-tests branch January 10, 2024 18:42
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.

3 participants