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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate jest-validate to TypeScript #7991

Merged
merged 2 commits into from Feb 28, 2019

Conversation

SimenB
Copy link
Member

@SimenB SimenB commented Feb 26, 2019

Summary

This is not good enough for external consumers of jest-validate, but it works for our internal usage, which is better than nothing 馃檪 Help making it better is greatly appreciated!

Built diff:

diff --git c/packages/jest-validate/build/condition.js w/packages/jest-validate/build/condition.js
index e9ae48a46..0cd6b4aca 100644
--- c/packages/jest-validate/build/condition.js
+++ w/packages/jest-validate/build/condition.js
@@ -12,8 +12,6 @@ exports.multipleValidOptions = multipleValidOptions;
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const toString = Object.prototype.toString;
 const MULTIPLE_VALID_OPTIONS_SYMBOL = Symbol('JEST_MULTIPLE_VALID_OPTIONS');
@@ -29,7 +27,7 @@ function validationConditionSingle(option, validOption) {
 
 function getValues(validOption) {
   if (
-    Array.isArray(validOption) &&
+    Array.isArray(validOption) && // @ts-ignore
     validOption[MULTIPLE_VALID_OPTIONS_SYMBOL]
   ) {
     return validOption;
@@ -40,10 +38,11 @@ function getValues(validOption) {
 
 function validationCondition(option, validOption) {
   return getValues(validOption).some(e => validationConditionSingle(option, e));
-}
+} // TODO: This should infer the types of its arguments, and return a union type of the types
+// See https://github.com/Microsoft/TypeScript/issues/5453
 
 function multipleValidOptions(...args) {
-  const options = [...args]; // $FlowFixMe
+  const options = [...args]; // @ts-ignore
 
   options[MULTIPLE_VALID_OPTIONS_SYMBOL] = true;
   return options;
diff --git c/packages/jest-validate/build/defaultConfig.js w/packages/jest-validate/build/defaultConfig.js
index 8cae136e7..7700033b8 100644
--- c/packages/jest-validate/build/defaultConfig.js
+++ w/packages/jest-validate/build/defaultConfig.js
@@ -20,10 +20,8 @@ var _utils = require('./utils');
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
-var _default = {
+const validationOptions = {
   comment: '',
   condition: _condition.validationCondition,
   deprecate: _deprecated.deprecationWarning,
@@ -40,4 +38,5 @@ var _default = {
   },
   unknown: _warnings.unknownOptionWarning
 };
+var _default = validationOptions;
 exports.default = _default;
diff --git c/packages/jest-validate/build/deprecated.js w/packages/jest-validate/build/deprecated.js
index 437530f3d..08be871ed 100644
--- c/packages/jest-validate/build/deprecated.js
+++ w/packages/jest-validate/build/deprecated.js
@@ -12,8 +12,6 @@ var _utils = require('./utils');
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const deprecationMessage = (message, options) => {
   const comment = options.comment;
diff --git c/packages/jest-validate/build/errors.js w/packages/jest-validate/build/errors.js
index 1996cf8cf..03851fa81 100644
--- c/packages/jest-validate/build/errors.js
+++ w/packages/jest-validate/build/errors.js
@@ -38,8 +38,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const errorMessage = (option, received, defaultValue, options, path) => {
   const conditions = (0, _condition.getValues)(defaultValue);
diff --git c/packages/jest-validate/build/exampleConfig.js w/packages/jest-validate/build/exampleConfig.js
index 09c3b796b..c4eda4d06 100644
--- c/packages/jest-validate/build/exampleConfig.js
+++ w/packages/jest-validate/build/exampleConfig.js
@@ -10,17 +10,15 @@ exports.default = void 0;
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const config = {
   comment: '  A comment',
-  condition: (option, validOption) => true,
-  deprecate: (config, option, deprecatedOptions, options) => false,
+  condition: () => true,
+  deprecate: () => false,
   deprecatedConfig: {
-    key: config => {}
+    key: () => {}
   },
-  error: (option, received, defaultValue, options) => {},
+  error: () => {},
   exampleConfig: {
     key: 'value',
     test: 'case'
@@ -32,7 +30,7 @@ const config = {
     error: 'Validation Error',
     warning: 'Validation Warning'
   },
-  unknown: (config, option, options) => {}
+  unknown: () => {}
 };
 var _default = config;
 exports.default = _default;
diff --git c/packages/jest-validate/build/index.js w/packages/jest-validate/build/index.js
index a1ee651fd..b0a84cb45 100644
--- c/packages/jest-validate/build/index.js
+++ w/packages/jest-validate/build/index.js
@@ -19,8 +19,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 module.exports = {
   ValidationError: _utils.ValidationError,
diff --git c/packages/jest-validate/build/utils.js w/packages/jest-validate/build/utils.js
index 5997be60d..a5b102fd9 100644
--- c/packages/jest-validate/build/utils.js
+++ w/packages/jest-validate/build/utils.js
@@ -44,8 +44,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const BULLET = _chalk().default.bold('\u25cf');
 
diff --git c/packages/jest-validate/build/validate.js w/packages/jest-validate/build/validate.js
index ad903f6ce..553f6445d 100644
--- c/packages/jest-validate/build/validate.js
+++ w/packages/jest-validate/build/validate.js
@@ -73,7 +73,7 @@ const _validate = (config, exampleConfig, options, path = []) => {
         options
       );
       hasDeprecationWarnings = hasDeprecationWarnings || isDeprecatedKey;
-    } else if (hasOwnProperty.call(exampleConfig, key)) {
+    } else if (Object.hasOwnProperty.call(exampleConfig, key)) {
       if (
         typeof options.condition === 'function' &&
         typeof options.error === 'function' &&
@@ -108,10 +108,10 @@ const _validate = (config, exampleConfig, options, path = []) => {
 const validate = (config, options) => {
   hasDeprecationWarnings = false; // Preserve default blacklist entries even with user-supplied blacklist
 
-  const combinedBlacklist = [].concat(
-    _defaultConfig.default.recursiveBlacklist || [],
-    options.recursiveBlacklist || []
-  );
+  const combinedBlacklist = [
+    ...(_defaultConfig.default.recursiveBlacklist || []),
+    ...(options.recursiveBlacklist || [])
+  ];
   const defaultedOptions = Object.assign(
     _objectSpread({}, _defaultConfig.default, options, {
       recursiveBlacklist: combinedBlacklist,
diff --git c/packages/jest-validate/build/validateCLIOptions.js w/packages/jest-validate/build/validateCLIOptions.js
index 761a24cd3..236f69069 100644
--- c/packages/jest-validate/build/validateCLIOptions.js
+++ w/packages/jest-validate/build/validateCLIOptions.js
@@ -139,9 +139,10 @@ function validateCLIOptions(argv, options, rawArgv = []) {
     (acc, entry) => {
       if (options[entry]) {
         acc[entry] = deprecationEntries[entry];
+        const alias = options[entry].alias;
 
-        if (options[entry].alias) {
-          acc[options[entry].alias] = deprecationEntries[entry];
+        if (alias) {
+          acc[alias] = deprecationEntries[entry];
         }
       }
 
diff --git c/packages/jest-validate/build/warnings.js w/packages/jest-validate/build/warnings.js
index 26f445057..d69f7f296 100644
--- c/packages/jest-validate/build/warnings.js
+++ w/packages/jest-validate/build/warnings.js
@@ -26,8 +26,6 @@ function _interopRequireDefault(obj) {
  *
  * This source code is licensed under the MIT license found in the
  * LICENSE file in the root directory of this source tree.
- *
- *
  */
 const unknownOptionWarning = (config, exampleConfig, option, options, path) => {
   const didYouMean = (0, _utils.createDidYouMeanMessage)(

Test plan

Green CI

import {multipleValidOptions} from 'jest-validate';
import {NODE_MODULES} from './constants';

const NODE_MODULES_REGEXP = replacePathSepForRegex(NODE_MODULES);

const initialOptions: Config.InitialOptions = {
automock: false,
// @ts-ignore TODO: type this properly
Copy link
Member Author

Choose a reason for hiding this comment

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

@jeysal do you know how to type this? Essentially, the return-type should be one a union of the types of all arguments

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure I understand, wouldn't that typing be a lie? multipleValidOptions doesn't return one of the argument (in that case the return type would be the union of them), but an array of all of them.


// @ts-ignore: not the same arguments
// @ts-ignore: fix this at some point
validateCLIOptions(argv, {...args.options, deprecationEntries});
Copy link
Collaborator

Choose a reason for hiding this comment

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

This error here is nuts, I gave up :p

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants