From 59320477584e0241a38f7b20dbb8859bd493b3e9 Mon Sep 17 00:00:00 2001 From: Waseem Dahman Date: Fri, 15 May 2020 23:22:34 -0400 Subject: [PATCH] 0.13.0 Prerelease Updates (#135) * Update rollup * cleanup code --- .eslintrc | 6 ++- jest.setup.js | 5 +-- package.json | 5 ++- rollup.config.js | 12 +++++- src/constants.js | 5 --- src/useFormState.js | 45 ++++++++++----------- src/utils.js | 7 +--- yarn.lock | 99 ++++++++++++++++++++++++++++----------------- 8 files changed, 105 insertions(+), 79 deletions(-) diff --git a/.eslintrc b/.eslintrc index 47a2151..0c7686d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -5,11 +5,15 @@ "@wsmd/eslint-config/prettier", "@wsmd/eslint-config/jest" ], + "globals": { + "__DEV__": false + }, "rules": { "getter-return": "off", "consistent-return": "off", "@typescript-eslint/no-explicit-any": "off", - "no-console": ["error", { "allow": ["warn", "error"] }] + "no-console": ["error", { "allow": ["warn", "error"] }], + "no-underscore-dangle": ["error", { "allow": ["__DEV__"] }] }, "overrides": [ { diff --git a/jest.setup.js b/jest.setup.js index 14883f3..d04452b 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -8,12 +8,11 @@ import 'react-testing-library/cleanup-after-each'; * in the development environment. */ let consoleSpy; -const originalEnv = process.env.NODE_ENV; beforeEach(() => { consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); - process.env.NODE_ENV = 'development'; + global.__DEV__ = 'development'; }); afterEach(() => { consoleSpy.mockRestore(); - process.env.NODE_ENV = originalEnv; + global.__DEV__ = process.env.NODE_ENV; }); diff --git a/package.json b/package.json index 1c1654b..922c54b 100644 --- a/package.json +++ b/package.json @@ -58,6 +58,8 @@ "@babel/plugin-transform-runtime": "^7.3.4", "@babel/preset-env": "^7.1.0", "@babel/preset-react": "^7.0.0", + "@rollup/plugin-babel": "^5.0.0", + "@rollup/plugin-replace": "^2.3.2", "@types/jest": "^24.0.11", "@types/react": "^16.8.4", "@wsmd/eslint-config": "^1.2.0", @@ -72,8 +74,7 @@ "react-dom": "^16.7.0-alpha.0", "react-hooks-testing-library": "^0.3.7", "react-testing-library": "^6.0.0", - "rollup": "^1.27.14", - "rollup-plugin-babel": "^4.3.3", + "rollup": "^2.10.2", "typescript": "^3.7.4" } } diff --git a/rollup.config.js b/rollup.config.js index e309eb0..f4d5a4b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,4 +1,5 @@ -import babel from 'rollup-plugin-babel'; +import babel from '@rollup/plugin-babel'; +import replace from '@rollup/plugin-replace'; import pkg from './package.json'; export default { @@ -14,5 +15,12 @@ export default { }, ], external: Object.keys(pkg.devDependencies), - plugins: [babel()], + plugins: [ + babel({ + babelHelpers: 'bundled', + }), + replace({ + __DEV__: "process.env.NODE_ENV === 'development'", + }), + ], }; diff --git a/src/constants.js b/src/constants.js index 0f83ce7..a739e38 100644 --- a/src/constants.js +++ b/src/constants.js @@ -45,8 +45,3 @@ export const INPUT_TYPES = [ URL, WEEK, ]; - -export const ON_CHANGE_HANDLER = 0; -export const ON_BLUR_HANDLER = 1; - -export const CONSOLE_TAG = '[useFormState]'; diff --git a/src/useFormState.js b/src/useFormState.js index 9c09f61..26eb72e 100644 --- a/src/useFormState.js +++ b/src/useFormState.js @@ -20,9 +20,6 @@ import { TEXTAREA, SELECT_MULTIPLE, LABEL, - ON_CHANGE_HANDLER, - ON_BLUR_HANDLER, - CONSOLE_TAG, } from './constants'; const defaultFormOptions = { @@ -46,14 +43,13 @@ export default function useFormState(initialState, options) { function warn(key, type, message) { if (!devWarnings.has(`${type}:${key}`)) { devWarnings.set(`${type}:${key}`, true); - console.warn(CONSOLE_TAG, message); + console.warn('[useFormState]', message); } } const createPropsGetter = type => (...args) => { - const { name, ownValue, hasOwnValue, ...inputOptions } = parseInputArgs( - args, - ); + const inputOptions = parseInputArgs(args); + const { name, ownValue, hasOwnValue } = inputOptions; const isCheckbox = type === CHECKBOX; const isRadio = type === RADIO; @@ -70,7 +66,7 @@ export default function useFormState(initialState, options) { function setDefaultValue() { /* istanbul ignore else */ - if (process.env.NODE_ENV === 'development') { + if (__DEV__) { if (isRaw) { warn( key, @@ -125,7 +121,7 @@ export default function useFormState(initialState, options) { } return (value, other) => { /* istanbul ignore else */ - if (process.env.NODE_ENV === 'development') { + if (__DEV__) { if (isRaw && ![value, other].every(testIsEqualCompatibility)) { warn( key, @@ -162,7 +158,7 @@ export default function useFormState(initialState, options) { } else if (!isRaw) { isValid = e.target.validity.valid; error = e.target.validationMessage; - } else if (process.env.NODE_ENV === 'development') { + } else if (__DEV__) { warn( key, 'missingValidate', @@ -243,7 +239,7 @@ export default function useFormState(initialState, options) { return hasValueInState ? formState.current.values[name] : ''; }, - onChange: callbacks.getOrSet(ON_BLUR_HANDLER + key, e => { + onChange: callbacks.getOrSet(`onChange.${key}`, e => { setDirty(name, true); let value; if (isRaw) { @@ -254,7 +250,7 @@ export default function useFormState(initialState, options) { // from controlled to uncontrolled value = formState.current.values[name]; /* istanbul ignore else */ - if (process.env.NODE_ENV === 'development') { + if (__DEV__) { warn( key, 'onChangeUndefined', @@ -272,7 +268,7 @@ export default function useFormState(initialState, options) { } else if (isSelectMultiple) { value = getNextSelectMultipleValue(e); } else { - ({ value } = e.target); + value = e.target.value; } inputOptions.onChange(e); } @@ -300,7 +296,7 @@ export default function useFormState(initialState, options) { formState.setValues(partialNewState); }), - onBlur: callbacks.getOrSet(ON_CHANGE_HANDLER + key, e => { + onBlur: callbacks.getOrSet(`onBlur.${key}`, e => { touch(e); inputOptions.onBlur(e); @@ -320,13 +316,15 @@ export default function useFormState(initialState, options) { ...getIdProp('id', name, ownValue), }; - return isRaw - ? { - onChange: inputProps.onChange, - onBlur: inputProps.onBlur, - value: inputProps.value, - } - : inputProps; + if (isRaw) { + return { + onChange: inputProps.onChange, + onBlur: inputProps.onBlur, + value: inputProps.value, + }; + } + + return inputProps; }; const formStateAPI = useRef({ @@ -349,10 +347,9 @@ export default function useFormState(initialState, options) { }); // exposing current form state (e.g. values, touched, validity, etc) - // eslint-disable-next-line guard-for-in, no-restricted-syntax - for (const key in formState.current) { + Object.keys(formState.current).forEach(key => { formStateAPI.current[key] = formState.current[key]; - } + }); const inputPropsCreators = { [LABEL]: (name, ownValue) => getIdProp('htmlFor', name, ownValue), diff --git a/src/utils.js b/src/utils.js index 839a9e1..34a8ba6 100644 --- a/src/utils.js +++ b/src/utils.js @@ -77,12 +77,9 @@ export function isEqual(value, other) { } export function testIsEqualCompatibility(value) { - let result; /* istanbul ignore if */ if (Array.isArray(value)) { - result = value.every(testIsEqualCompatibility); - } else { - result = value == null || /^[sbn]/.test(typeof value); // is primitive + return value.every(testIsEqualCompatibility); } - return result; + return value == null || /^[sbn]/.test(typeof value); // basic primitives } diff --git a/yarn.lock b/yarn.lock index fa694e4..edddc24 100644 --- a/yarn.lock +++ b/yarn.lock @@ -142,7 +142,7 @@ dependencies: "@babel/types" "^7.7.4" -"@babel/helper-module-imports@^7.0.0", "@babel/helper-module-imports@^7.7.4": +"@babel/helper-module-imports@^7.7.4": version "7.7.4" resolved "https://registry.yarnpkg.com/@babel/helper-module-imports/-/helper-module-imports-7.7.4.tgz#e5a92529f8888bf319a6376abfbd1cebc491ad91" integrity sha512-dGcrX6K9l8258WFjyDLJwuVKxR4XZfU0/vTUgOQYWEnRD8mgr+p4d6fCUMq/ys0h4CCt/S5JhbvtyErjWouAUQ== @@ -905,6 +905,31 @@ "@types/istanbul-reports" "^1.1.1" "@types/yargs" "^13.0.0" +"@rollup/plugin-babel@^5.0.0": + version "5.0.0" + resolved "https://registry.yarnpkg.com/@rollup/plugin-babel/-/plugin-babel-5.0.0.tgz#2f01c9555927449c82cb13dd9e56213a358e1ce9" + integrity sha512-YpVnwdUeVj/fDFN75Y3CAzJTMYNcqbH05SJs551wqj+BSwLT9pS3dqJrVDPYl3eH4OrI8ueiEseX5VgUn+0HLA== + dependencies: + "@babel/helper-module-imports" "^7.7.4" + "@rollup/pluginutils" "^3.0.8" + +"@rollup/plugin-replace@^2.3.2": + version "2.3.2" + resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.3.2.tgz#da4e0939047f793c2eb5eedfd6c271232d0a033f" + integrity sha512-KEEL7V2tMNOsbAoNMKg91l1sNXBDoiP31GFlqXVOuV5691VQKzKBh91+OKKOG4uQWYqcFskcjFyh1d5YnZd0Zw== + dependencies: + "@rollup/pluginutils" "^3.0.8" + magic-string "^0.25.5" + +"@rollup/pluginutils@^3.0.8": + version "3.0.10" + resolved "https://registry.yarnpkg.com/@rollup/pluginutils/-/pluginutils-3.0.10.tgz#a659b9025920378494cd8f8c59fbf9b3a50d5f12" + integrity sha512-d44M7t+PjmMrASHbhgpSbVgtL6EFyX7J4mYxwQ/c5eoaE6N2VgCgEcWVzNnwycIloti+/MpwFr8qfw+nRw00sw== + dependencies: + "@types/estree" "0.0.39" + estree-walker "^1.0.1" + picomatch "^2.2.2" + "@sheerun/mutationobserver-shim@^0.3.2": version "0.3.2" resolved "https://registry.yarnpkg.com/@sheerun/mutationobserver-shim/-/mutationobserver-shim-0.3.2.tgz#8013f2af54a2b7d735f71560ff360d3a8176a87b" @@ -948,10 +973,10 @@ resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d" integrity sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag== -"@types/estree@*": - version "0.0.41" - resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.41.tgz#fd90754150b57432b72bf560530500597ff04421" - integrity sha512-rIAmXyJlqw4KEBO7+u9gxZZSQHaCNnIzYrnNmYVpgfJhxTqO0brCX0SYpqUTkVI5mwwUwzmtspLBGBKroMeynA== +"@types/estree@0.0.39": + version "0.0.39" + resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f" + integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw== "@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0": version "2.0.1" @@ -990,11 +1015,6 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4= -"@types/node@*": - version "13.1.2" - resolved "https://registry.yarnpkg.com/@types/node/-/node-13.1.2.tgz#fe94285bf5e0782e1a9e5a8c482b1c34465fa385" - integrity sha512-B8emQA1qeKerqd1dmIsQYnXi+mmAzTB7flExjmy5X1aVAKFNNNDubkavwR13kR6JnpeLp3aLoJhwn9trWPAyFQ== - "@types/prop-types@*": version "15.7.3" resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7" @@ -2312,10 +2332,10 @@ estraverse@^4.0.0, estraverse@^4.1.0, estraverse@^4.1.1, estraverse@^4.2.0: resolved "https://registry.yarnpkg.com/estraverse/-/estraverse-4.3.0.tgz#398ad3f3c5a24948be7725e83d11a7de28cdbd1d" integrity sha512-39nnKffWz8xN1BU/2c79n9nB9HDzo0niYUqx6xyqUnyoAnQyyWpOTdZEeiCch8BBu515t4wp9ZmgVfVhn9EBpw== -estree-walker@^0.6.1: - version "0.6.1" - resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-0.6.1.tgz#53049143f40c6eb918b23671d1fe3219f3a1b362" - integrity sha512-SqmZANLWS0mnatqbSfRP5g8OXZC12Fgg1IwNtLsyHDzJizORW4khDfjPqJZsemPWBB2uqykUah5YpQ6epsqC/w== +estree-walker@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/estree-walker/-/estree-walker-1.0.1.tgz#31bc5d612c96b704106b477e6dd5d8aa138cb700" + integrity sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg== esutils@^2.0.0, esutils@^2.0.2: version "2.0.3" @@ -2603,6 +2623,11 @@ fsevents@^1.2.7: bindings "^1.5.0" nan "^2.12.1" +fsevents@~2.1.2: + version "2.1.3" + resolved "https://registry.yarnpkg.com/fsevents/-/fsevents-2.1.3.tgz#fb738703ae8d2f9fe900c33836ddebee8b97f23e" + integrity sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ== + function-bind@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d" @@ -3879,6 +3904,13 @@ loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.4.0: dependencies: js-tokens "^3.0.0 || ^4.0.0" +magic-string@^0.25.5: + version "0.25.7" + resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.7.tgz#3f497d6fd34c669c6798dcb821f2ef31f5445051" + integrity sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA== + dependencies: + sourcemap-codec "^1.4.4" + make-dir@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5" @@ -4422,6 +4454,11 @@ performance-now@^2.1.0: resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns= +picomatch@^2.2.2: + version "2.2.2" + resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad" + integrity sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg== + pify@^2.0.0: version "2.3.0" resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c" @@ -4935,29 +4972,12 @@ rimraf@^2.5.4, rimraf@^2.6.3: dependencies: glob "^7.1.3" -rollup-plugin-babel@^4.3.3: - version "4.3.3" - resolved "https://registry.yarnpkg.com/rollup-plugin-babel/-/rollup-plugin-babel-4.3.3.tgz#7eb5ac16d9b5831c3fd5d97e8df77ba25c72a2aa" - integrity sha512-tKzWOCmIJD/6aKNz0H1GMM+lW1q9KyFubbWzGiOG540zxPPifnEAHTZwjo0g991Y+DyOZcLqBgqOdqazYE5fkw== - dependencies: - "@babel/helper-module-imports" "^7.0.0" - rollup-pluginutils "^2.8.1" - -rollup-pluginutils@^2.8.1: - version "2.8.2" - resolved "https://registry.yarnpkg.com/rollup-pluginutils/-/rollup-pluginutils-2.8.2.tgz#72f2af0748b592364dbd3389e600e5a9444a351e" - integrity sha512-EEp9NhnUkwY8aif6bxgovPHMoMoNr2FulJziTndpt5H9RdwC47GSGuII9XxpSdzVGM0GWrNPHV6ie1LTNJPaLQ== - dependencies: - estree-walker "^0.6.1" - -rollup@^1.27.14: - version "1.27.14" - resolved "https://registry.yarnpkg.com/rollup/-/rollup-1.27.14.tgz#940718d5eec1a6887e399aa0089944bae5c4f377" - integrity sha512-DuDjEyn8Y79ALYXMt+nH/EI58L5pEw5HU9K38xXdRnxQhvzUTI/nxAawhkAHUQeudANQ//8iyrhVRHJBuR6DSQ== - dependencies: - "@types/estree" "*" - "@types/node" "*" - acorn "^7.1.0" +rollup@^2.10.2: + version "2.10.2" + resolved "https://registry.yarnpkg.com/rollup/-/rollup-2.10.2.tgz#9adfcf8ab36861b5b0f8ca7b436f5866e3e9e200" + integrity sha512-tivFM8UXBlYOUqpBYD3pRktYpZvK/eiCQ190eYlrAyrpE/lzkyG2gbawroNdbwmzyUc7Y4eT297xfzv0BDh9qw== + optionalDependencies: + fsevents "~2.1.2" rsvp@^4.8.4: version "4.8.5" @@ -5163,6 +5183,11 @@ source-map@^0.6.0, source-map@^0.6.1, source-map@~0.6.1: resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.6.1.tgz#74722af32e9614e9c287a8d0bbde48b5e2f1a263" integrity sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g== +sourcemap-codec@^1.4.4: + version "1.4.8" + resolved "https://registry.yarnpkg.com/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz#ea804bd94857402e6992d05a38ef1ae35a9ab4c4" + integrity sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA== + spdx-correct@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/spdx-correct/-/spdx-correct-3.1.0.tgz#fb83e504445268f154b074e218c87c003cd31df4"