@elliemae/ds-props-helpers 2.2.0-next.3 → 2.2.0-next.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cjs/globalProps/globalAttributesPropTypes.js +372 -362
- package/cjs/globalProps/useGetGlobalAttributes.js +1 -1
- package/cjs/index.js +4 -0
- package/cjs/propTypes/PropTypes.js +130 -0
- package/cjs/propTypes/customPropTypes.js +21 -0
- package/cjs/propTypes/describe.js +64 -0
- package/cjs/propTypes/describeConversions.js +104 -0
- package/cjs/propTypes/describeGuards.js +21 -0
- package/cjs/propTypes/index.js +11 -0
- package/cjs/propTypes/toTypescript.js +160 -0
- package/cjs/propTypes/types.js +2 -0
- package/cjs/validation/typescriptValidator.js +3 -2
- package/esm/globalProps/globalAttributesPropTypes.js +372 -362
- package/esm/globalProps/useGetGlobalAttributes.js +1 -1
- package/esm/index.js +2 -0
- package/esm/propTypes/PropTypes.js +124 -0
- package/esm/propTypes/customPropTypes.js +17 -0
- package/esm/propTypes/describe.js +62 -0
- package/esm/propTypes/describeConversions.js +96 -0
- package/esm/propTypes/describeGuards.js +10 -0
- package/esm/propTypes/index.js +2 -0
- package/esm/propTypes/toTypescript.js +154 -0
- package/esm/propTypes/types.js +1 -0
- package/esm/validation/typescriptValidator.js +2 -1
- package/package.json +40 -4
- package/types/globalProps/globalAttributesPropTypes.d.ts +361 -2167
- package/types/index.d.ts +1 -0
- package/types/propTypes/PropTypes.d.ts +3 -0
- package/types/propTypes/customPropTypes.d.ts +2 -0
- package/types/propTypes/describe.d.ts +3 -0
- package/types/propTypes/describeConversions.d.ts +3 -0
- package/types/propTypes/describeGuards.d.ts +9 -0
- package/types/propTypes/index.d.ts +3 -0
- package/types/propTypes/toTypescript.d.ts +3 -0
- package/types/propTypes/types.d.ts +96 -0
- package/types/tests/globalProps/TestComponent.d.ts +1 -0
- package/types/tests/{any.validation.test.d.ts → globalProps/globalAttributes.test.d.ts} +0 -0
- package/types/tests/{array.validation.test.d.ts → validation/any.validation.test.d.ts} +0 -0
- package/types/tests/{boolean.validation.test.d.ts → validation/array.validation.test.d.ts} +0 -0
- package/types/tests/{function.validation.test.d.ts → validation/boolean.validation.test.d.ts} +0 -0
- package/types/tests/{number.validation.test.d.ts → validation/function.validation.test.d.ts} +0 -0
- package/types/tests/{object.validation.test.d.ts → validation/number.validation.test.d.ts} +0 -0
- package/types/tests/{schema.validation.test.d.ts → validation/object.validation.test.d.ts} +0 -0
- package/types/tests/{string.validation.test.d.ts → validation/schema.validation.test.d.ts} +0 -0
- package/types/tests/{union.validation.test.d.ts → validation/string.validation.test.d.ts} +0 -0
- package/types/tests/{test.schema.d.ts → validation/test.schema.d.ts} +0 -0
- package/types/tests/validation/union.validation.test.d.ts +1 -0
- package/types/validation/typescriptValidator.d.ts +2 -2
|
@@ -16,7 +16,7 @@ const useGetGlobalAttributes = (props, overrides) => {
|
|
|
16
16
|
let [key, value] = _ref;
|
|
17
17
|
|
|
18
18
|
if (key in constants.globalAttributes || key.startsWith('data-')) {
|
|
19
|
-
if (key in overrides && typeof value === 'function' && typeof overrides[key] === 'function') {
|
|
19
|
+
if (overrides && key in overrides && typeof value === 'function' && typeof overrides[key] === 'function') {
|
|
20
20
|
const newFunc = function () {
|
|
21
21
|
value(...arguments);
|
|
22
22
|
overrides[key](...arguments);
|
package/cjs/index.js
CHANGED
|
@@ -9,6 +9,8 @@ var typescriptValidator = require('./validation/typescriptValidator.js');
|
|
|
9
9
|
var index = require('./getProps/index.js');
|
|
10
10
|
var useGetGlobalAttributes = require('./globalProps/useGetGlobalAttributes.js');
|
|
11
11
|
var globalAttributesPropTypes = require('./globalProps/globalAttributesPropTypes.js');
|
|
12
|
+
var PropTypes = require('./propTypes/PropTypes.js');
|
|
13
|
+
var describe = require('./propTypes/describe.js');
|
|
12
14
|
|
|
13
15
|
|
|
14
16
|
|
|
@@ -22,3 +24,5 @@ exports.getAriaProps = index.getAriaProps;
|
|
|
22
24
|
exports.getDataProps = index.getDataProps;
|
|
23
25
|
exports.useGetGlobalAttributes = useGetGlobalAttributes.useGetGlobalAttributes;
|
|
24
26
|
exports.globalAttributesPropTypes = globalAttributesPropTypes.globalAttributesPropTypes;
|
|
27
|
+
exports.PropTypes = PropTypes;
|
|
28
|
+
exports.describe = describe;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
4
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
5
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
6
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
7
|
+
require('core-js/modules/esnext.async-iterator.filter.js');
|
|
8
|
+
require('core-js/modules/esnext.iterator.filter.js');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
|
|
13
|
+
|
|
14
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
15
|
+
|
|
16
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
17
|
+
|
|
18
|
+
const addPropTypeDocumentationField = fieldName => function addFieldToReactDesc(value) {
|
|
19
|
+
if (!this.reactDesc) {
|
|
20
|
+
this.reactDesc = {};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let realValue = value;
|
|
24
|
+
if (fieldName === 'global' || fieldName === 'hidden') realValue = true;
|
|
25
|
+
this.reactDesc[fieldName] = realValue;
|
|
26
|
+
return this;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const documentedPropType = {
|
|
30
|
+
defaultValue: addPropTypeDocumentationField('defaultValue'),
|
|
31
|
+
description: addPropTypeDocumentationField('description'),
|
|
32
|
+
deprecated: addPropTypeDocumentationField('deprecated'),
|
|
33
|
+
format: addPropTypeDocumentationField('format'),
|
|
34
|
+
global: addPropTypeDocumentationField('global'),
|
|
35
|
+
hidden: addPropTypeDocumentationField('hidden')
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const createPropType = type => {
|
|
39
|
+
const propTypeObj = _objectSpread({
|
|
40
|
+
type
|
|
41
|
+
}, documentedPropType);
|
|
42
|
+
|
|
43
|
+
Object.defineProperty(propTypeObj, 'isRequired', {
|
|
44
|
+
get: function getRequired() {
|
|
45
|
+
if (!this.reactDesc) {
|
|
46
|
+
this.reactDesc = {};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
this.reactDesc.required = true;
|
|
50
|
+
return this;
|
|
51
|
+
},
|
|
52
|
+
enumerable: true,
|
|
53
|
+
configurable: true
|
|
54
|
+
});
|
|
55
|
+
return propTypeObj;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const createPropTypeWithArgs = type => args => {
|
|
59
|
+
const propTypeObj = _objectSpread({
|
|
60
|
+
args,
|
|
61
|
+
type
|
|
62
|
+
}, documentedPropType);
|
|
63
|
+
|
|
64
|
+
Object.defineProperty(propTypeObj, 'isRequired', {
|
|
65
|
+
get: function getRequired() {
|
|
66
|
+
if (!this.reactDesc) {
|
|
67
|
+
this.reactDesc = {};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.reactDesc.required = true;
|
|
71
|
+
return this;
|
|
72
|
+
},
|
|
73
|
+
enumerable: true,
|
|
74
|
+
configurable: true
|
|
75
|
+
});
|
|
76
|
+
return propTypeObj;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const PropTypes = {
|
|
80
|
+
custom: callback => {
|
|
81
|
+
const target = callback.bind(null);
|
|
82
|
+
target.type = 'func';
|
|
83
|
+
Object.keys(documentedPropType).forEach(fieldName => {
|
|
84
|
+
const fieldNameCasted = fieldName; // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
85
|
+
|
|
86
|
+
target[fieldNameCasted] = documentedPropType[fieldNameCasted];
|
|
87
|
+
});
|
|
88
|
+
return target;
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
function definePropType(type) {
|
|
93
|
+
Object.defineProperty(PropTypes, type, {
|
|
94
|
+
get: function getPropType() {
|
|
95
|
+
return createPropType(type);
|
|
96
|
+
},
|
|
97
|
+
enumerable: true,
|
|
98
|
+
configurable: true
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function definePropTypeWithArgs(type) {
|
|
103
|
+
Object.defineProperty(PropTypes, type, {
|
|
104
|
+
get: function getPropType() {
|
|
105
|
+
return createPropTypeWithArgs(type);
|
|
106
|
+
},
|
|
107
|
+
enumerable: true,
|
|
108
|
+
configurable: true
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
definePropType('any');
|
|
113
|
+
definePropType('array');
|
|
114
|
+
definePropType('bool');
|
|
115
|
+
definePropType('element');
|
|
116
|
+
definePropType('func');
|
|
117
|
+
definePropType('node');
|
|
118
|
+
definePropType('number');
|
|
119
|
+
definePropType('object');
|
|
120
|
+
definePropType('symbol');
|
|
121
|
+
definePropType('string');
|
|
122
|
+
definePropTypeWithArgs('arrayOf');
|
|
123
|
+
definePropTypeWithArgs('instanceOf');
|
|
124
|
+
definePropTypeWithArgs('objectOf');
|
|
125
|
+
definePropTypeWithArgs('oneOfType');
|
|
126
|
+
definePropTypeWithArgs('oneOf');
|
|
127
|
+
definePropTypeWithArgs('shape');
|
|
128
|
+
definePropTypeWithArgs('tuple');
|
|
129
|
+
|
|
130
|
+
module.exports = PropTypes;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('core-js/modules/web.dom-collections.iterator.js');
|
|
6
|
+
|
|
7
|
+
/* eslint-disable max-params */
|
|
8
|
+
const tupleValidator = tupleShape => {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
|
10
|
+
const func = function (value, index) {
|
|
11
|
+
for (var _len = arguments.length, rest = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
|
12
|
+
rest[_key - 2] = arguments[_key];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return tupleShape[index](value, index, ...rest);
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
return func;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
exports.tupleValidator = tupleValidator;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
4
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
5
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
6
|
+
var describeConversions = require('./describeConversions.js');
|
|
7
|
+
var toTypescript = require('./toTypescript.js');
|
|
8
|
+
|
|
9
|
+
function describe(ComponentInstance) {
|
|
10
|
+
if (!ComponentInstance) {
|
|
11
|
+
throw new Error('react-desc: component is required');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const documentation = {
|
|
15
|
+
propTypes: {}
|
|
16
|
+
};
|
|
17
|
+
const DocumentedComponent = ComponentInstance;
|
|
18
|
+
|
|
19
|
+
const addDocumentationProp = propName => {
|
|
20
|
+
const func = value => {
|
|
21
|
+
documentation[propName] = value;
|
|
22
|
+
return DocumentedComponent;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return func;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
DocumentedComponent.availableAt = addDocumentationProp('availableAt');
|
|
29
|
+
DocumentedComponent.description = addDocumentationProp('description');
|
|
30
|
+
DocumentedComponent.details = addDocumentationProp('details');
|
|
31
|
+
DocumentedComponent.deprecated = addDocumentationProp('deprecated');
|
|
32
|
+
DocumentedComponent.usage = addDocumentationProp('usage');
|
|
33
|
+
DocumentedComponent.intrinsicElement = addDocumentationProp('intrinsicElement');
|
|
34
|
+
|
|
35
|
+
DocumentedComponent.toTypescript = () => toTypescript(ComponentInstance, documentation);
|
|
36
|
+
|
|
37
|
+
Object.defineProperty(DocumentedComponent, 'propTypes', {
|
|
38
|
+
get: () => DocumentedComponent.propTypesValue,
|
|
39
|
+
set: value => {
|
|
40
|
+
if (!DocumentedComponent.propTypesValue) {
|
|
41
|
+
DocumentedComponent.propTypesValue = {};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
Object.keys(value).forEach(name => {
|
|
45
|
+
const propType = value[name];
|
|
46
|
+
let realPropType;
|
|
47
|
+
documentation.propTypes[name] = propType;
|
|
48
|
+
realPropType = describeConversions.convertPropType(propType);
|
|
49
|
+
|
|
50
|
+
if (value[name].reactDesc.required) {
|
|
51
|
+
realPropType = realPropType.isRequired;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
DocumentedComponent.propTypesValue[name] = realPropType;
|
|
55
|
+
return propType;
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
enumerable: true,
|
|
59
|
+
configurable: true
|
|
60
|
+
});
|
|
61
|
+
return DocumentedComponent;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
module.exports = describe;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('core-js/modules/web.dom-collections.iterator.js');
|
|
6
|
+
require('core-js/modules/esnext.async-iterator.map.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.map.js');
|
|
8
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
9
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
10
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
11
|
+
var PropTypes = require('prop-types');
|
|
12
|
+
var customPropTypes = require('./customPropTypes.js');
|
|
13
|
+
var describeGuards = require('./describeGuards.js');
|
|
14
|
+
|
|
15
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
16
|
+
|
|
17
|
+
var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
|
|
18
|
+
|
|
19
|
+
const enrichPropType = (propType, reactDesc) => {
|
|
20
|
+
const func = function (props, propName, componentName) {
|
|
21
|
+
if (reactDesc && reactDesc.deprecated && propName in props && !reactDesc.warned) {
|
|
22
|
+
const {
|
|
23
|
+
version = '',
|
|
24
|
+
message = ''
|
|
25
|
+
} = reactDesc.deprecated;
|
|
26
|
+
const warning = "\"".concat(propName, "\" property of \"").concat(componentName, "\" will be deprecated on VERSION: ").concat(version, ".\n").concat(message);
|
|
27
|
+
console.warn(warning);
|
|
28
|
+
reactDesc.warned = true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
for (var _len = arguments.length, rest = new Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) {
|
|
32
|
+
rest[_key - 3] = arguments[_key];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return propType(props, propName, componentName, ...rest);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
Object.defineProperty(func, 'isRequired', Object.getOwnPropertyDescriptor(propType, 'isRequired'));
|
|
39
|
+
return func;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const convertArray = array => array.map(type => convertPropType(type));
|
|
43
|
+
|
|
44
|
+
const convertShape = _shape => {
|
|
45
|
+
const result = {};
|
|
46
|
+
Object.keys(_shape).forEach(key => {
|
|
47
|
+
result[key] = convertPropType(_shape[key]);
|
|
48
|
+
});
|
|
49
|
+
return result;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const convertPropType = propType => {
|
|
53
|
+
if (!propType || !propType.type) {
|
|
54
|
+
throw new Error("react-desc: unknown error -- proptype is not well defined");
|
|
55
|
+
} // DimSum PropTypes conversion
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
if (propType.type === 'tuple') {
|
|
59
|
+
return enrichPropType(PropTypes__default["default"].arrayOf(customPropTypes.tupleValidator(convertArray(propType.args))), propType.reactDesc);
|
|
60
|
+
} // Default PropType conversion
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
const realPropType = PropTypes__default["default"][propType.type];
|
|
64
|
+
|
|
65
|
+
if (!realPropType) {
|
|
66
|
+
throw new Error("react-desc: unknown type ".concat(propType.type));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (describeGuards.hasArguments(propType)) {
|
|
70
|
+
if (describeGuards.isOneOfType(propType)) {
|
|
71
|
+
return enrichPropType(realPropType(convertArray(propType.args)), propType.reactDesc);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (describeGuards.isArrayOf(propType)) {
|
|
75
|
+
return enrichPropType(realPropType(convertPropType(propType.args)), propType.reactDesc);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (describeGuards.isShape(propType)) {
|
|
79
|
+
return enrichPropType(realPropType(convertShape(propType.args)), propType.reactDesc);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (describeGuards.isInstanceOf(propType)) {
|
|
83
|
+
return enrichPropType(realPropType(propType.args), propType.reactDesc);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (describeGuards.isOneOf(propType)) {
|
|
87
|
+
return enrichPropType(realPropType(propType.args), propType.reactDesc);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (describeGuards.isObjectOf(propType)) {
|
|
91
|
+
return enrichPropType(realPropType(propType.args), propType.reactDesc);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (describeGuards.isExact(propType)) {
|
|
95
|
+
return enrichPropType(realPropType(propType.args), propType.reactDesc);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
throw new Error("react-desc: unknown error -- proptype with args is not matching");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return enrichPropType(realPropType, propType.reactDesc);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
exports.convertPropType = convertPropType;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const hasArguments = (propType, realPropType) => !!propType.args;
|
|
6
|
+
const isOneOfType = (propType, realPropType) => propType.type === 'oneOfType';
|
|
7
|
+
const isArrayOf = (propType, realPropType) => propType.type === 'arrayOf';
|
|
8
|
+
const isShape = (propType, realPropType) => propType.type === 'shape';
|
|
9
|
+
const isInstanceOf = (propType, realPropType) => propType.type === 'instanceOf';
|
|
10
|
+
const isOneOf = (propType, realPropType) => propType.type === 'oneOf';
|
|
11
|
+
const isObjectOf = (propType, realPropType) => propType.type === 'objectOf';
|
|
12
|
+
const isExact = (propType, realPropType) => propType.type === 'exact';
|
|
13
|
+
|
|
14
|
+
exports.hasArguments = hasArguments;
|
|
15
|
+
exports.isArrayOf = isArrayOf;
|
|
16
|
+
exports.isExact = isExact;
|
|
17
|
+
exports.isInstanceOf = isInstanceOf;
|
|
18
|
+
exports.isObjectOf = isObjectOf;
|
|
19
|
+
exports.isOneOf = isOneOf;
|
|
20
|
+
exports.isOneOfType = isOneOfType;
|
|
21
|
+
exports.isShape = isShape;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
4
|
+
require('core-js/modules/esnext.async-iterator.map.js');
|
|
5
|
+
require('core-js/modules/esnext.iterator.map.js');
|
|
6
|
+
require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
7
|
+
require('core-js/modules/esnext.iterator.constructor.js');
|
|
8
|
+
require('core-js/modules/esnext.iterator.for-each.js');
|
|
9
|
+
require('core-js/modules/esnext.async-iterator.filter.js');
|
|
10
|
+
require('core-js/modules/esnext.iterator.filter.js');
|
|
11
|
+
|
|
12
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
+
|
|
14
|
+
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
|
|
15
|
+
|
|
16
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
17
|
+
|
|
18
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
19
|
+
|
|
20
|
+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
|
21
|
+
|
|
22
|
+
/* eslint-disable @typescript-eslint/no-use-before-define */
|
|
23
|
+
|
|
24
|
+
/* eslint-disable complexity */
|
|
25
|
+
|
|
26
|
+
/* eslint-disable react/forbid-foreign-prop-types */
|
|
27
|
+
const arrayFormat = array => array.map(propType => propTypeFormat(propType));
|
|
28
|
+
|
|
29
|
+
const shapeFormat = shape => {
|
|
30
|
+
const props = Object.keys(shape).map(key => {
|
|
31
|
+
const value = shape[key];
|
|
32
|
+
let valueFormat;
|
|
33
|
+
|
|
34
|
+
if (value.type && (value.type === 'arrayOf' || value.type === 'oneOfType' || value.type === 'oneOf') && Array.isArray(value.args)) {
|
|
35
|
+
valueFormat = "".concat(propTypeFormat(value));
|
|
36
|
+
} else if (value.type === 'shape') {
|
|
37
|
+
valueFormat = "".concat(propTypeFormat(value));
|
|
38
|
+
} else {
|
|
39
|
+
valueFormat = propTypeFormat(value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return "".concat(key).concat(value.reactDesc && value.reactDesc.required ? '' : '?', ": ").concat(valueFormat);
|
|
43
|
+
});
|
|
44
|
+
return "{".concat(props.join(','), "}");
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const propTypeFormat = function (propType) {
|
|
48
|
+
let joinWith = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
49
|
+
let result;
|
|
50
|
+
|
|
51
|
+
if (Array.isArray(propType)) {
|
|
52
|
+
result = arrayFormat(propType).join(joinWith);
|
|
53
|
+
} else if (typeof propType !== 'function' && propType.type) {
|
|
54
|
+
switch (propType.type) {
|
|
55
|
+
case 'array':
|
|
56
|
+
result = 'any[]';
|
|
57
|
+
break;
|
|
58
|
+
|
|
59
|
+
case 'arrayOf':
|
|
60
|
+
if (propType.args.type === 'oneOfType') {
|
|
61
|
+
result = "(".concat(propTypeFormat(propType.args, ' | '), ")[]");
|
|
62
|
+
} else {
|
|
63
|
+
result = "".concat(propTypeFormat(propType.args, '\n'), "[]");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
break;
|
|
67
|
+
|
|
68
|
+
case 'tuple':
|
|
69
|
+
result = "[".concat(propTypeFormat(propType.args, ', '), "]");
|
|
70
|
+
break;
|
|
71
|
+
|
|
72
|
+
case 'bool':
|
|
73
|
+
result = 'boolean';
|
|
74
|
+
break;
|
|
75
|
+
|
|
76
|
+
case 'func':
|
|
77
|
+
result = '((...args: any[]) => any)';
|
|
78
|
+
break;
|
|
79
|
+
|
|
80
|
+
case 'node':
|
|
81
|
+
result = 'React.ReactNode';
|
|
82
|
+
break;
|
|
83
|
+
|
|
84
|
+
case 'element':
|
|
85
|
+
result = 'JSX.Element';
|
|
86
|
+
break;
|
|
87
|
+
|
|
88
|
+
case 'instanceOf':
|
|
89
|
+
result = 'any';
|
|
90
|
+
break;
|
|
91
|
+
|
|
92
|
+
case 'symbol':
|
|
93
|
+
result = 'any';
|
|
94
|
+
break;
|
|
95
|
+
|
|
96
|
+
case 'objectOf':
|
|
97
|
+
result = "{ [key: string]: ".concat(propTypeFormat(propType.args), " }");
|
|
98
|
+
break;
|
|
99
|
+
|
|
100
|
+
case 'oneOf':
|
|
101
|
+
result = propType.args.map(a => "\"".concat(a, "\"")).join(' | ');
|
|
102
|
+
break;
|
|
103
|
+
|
|
104
|
+
case 'oneOfType':
|
|
105
|
+
result = "".concat(propTypeFormat(propType.args, ' | '));
|
|
106
|
+
break;
|
|
107
|
+
|
|
108
|
+
case 'shape':
|
|
109
|
+
result = "".concat(shapeFormat(propType.args));
|
|
110
|
+
break;
|
|
111
|
+
|
|
112
|
+
default:
|
|
113
|
+
result = "".concat(propType.type);
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
result = 'any';
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return result;
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const propTypeAsTypescript = (propType, propName) => {
|
|
124
|
+
const documentation = _objectSpread(_objectSpread({}, propType.reactDesc), {}, {
|
|
125
|
+
name: propName
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
documentation.format = propTypeFormat(propType);
|
|
129
|
+
return documentation;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
function descToTypescript(component, reactDesc) {
|
|
133
|
+
if (!component) {
|
|
134
|
+
throw new Error('react-desc: component is required');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const documentation = _objectSpread({
|
|
138
|
+
name: component.displayName || component.name
|
|
139
|
+
}, reactDesc);
|
|
140
|
+
|
|
141
|
+
if (reactDesc) {
|
|
142
|
+
delete documentation.propTypes;
|
|
143
|
+
|
|
144
|
+
if (reactDesc.propTypes) {
|
|
145
|
+
const propTypes = [];
|
|
146
|
+
Object.keys(reactDesc.propTypes).forEach(propName => {
|
|
147
|
+
const propType = reactDesc.propTypes[propName];
|
|
148
|
+
propTypes.push(propTypeAsTypescript(propType, propName));
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
if (propTypes.length > 0) {
|
|
152
|
+
documentation.properties = propTypes;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return documentation;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
module.exports = descToTypescript;
|
|
@@ -6,8 +6,9 @@ require('core-js/modules/esnext.async-iterator.for-each.js');
|
|
|
6
6
|
require('core-js/modules/esnext.iterator.constructor.js');
|
|
7
7
|
require('core-js/modules/esnext.iterator.for-each.js');
|
|
8
8
|
require('core-js/modules/web.dom-collections.iterator.js');
|
|
9
|
-
var reactDesc = require('react-desc');
|
|
10
9
|
var react = require('react');
|
|
10
|
+
require('../propTypes/PropTypes.js');
|
|
11
|
+
var describe = require('../propTypes/describe.js');
|
|
11
12
|
var errorTemplates = require('./errorTemplates.js');
|
|
12
13
|
var typescriptGuards = require('./typescriptGuards.js');
|
|
13
14
|
var typescriptParsers = require('./typescriptParsers.js');
|
|
@@ -191,7 +192,7 @@ const useValidateTypescriptPropTypes = (props, propTypes) => {
|
|
|
191
192
|
const Component = () => {}; // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-call
|
|
192
193
|
|
|
193
194
|
|
|
194
|
-
return
|
|
195
|
+
return describe(Component);
|
|
195
196
|
}, []); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment
|
|
196
197
|
|
|
197
198
|
ComponentWithSchema.propTypes = propTypes;
|