Skip to content

Commit 872ed53

Browse files
authored
Merge pull request #1166 from data-driven-forms/add-missing-renderer-typings
Update types generator to use correct variable/function names.
2 parents c1380d2 + a69ea1e commit 872ed53

File tree

6 files changed

+55
-18
lines changed

6 files changed

+55
-18
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Meta } from "@data-driven-forms/react-form-renderer";
22

3-
export type validationError = (meta: Meta<any>, validateOnMount?: boolean) => boolean | any | undefined
3+
export function validationError(meta: Meta<any>, validateOnMount?: boolean): boolean | any | undefined
44

55
export default validationError;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Meta } from "@data-driven-forms/react-form-renderer";
22

3-
export type validationError = (meta: Meta<any>, validateOnMount?: boolean) => boolean | any | undefined;
3+
export function validationError(meta: Meta<any>, validateOnMount?: boolean): boolean | any | undefined;
44

55
export default validationError;

packages/react-form-renderer/src/component-types/component-types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export type ComponentType = 'text-field'|'field-array'|'checkbox'|'sub-form'|'radio'|'tabs'|'tab-item'|'date-picker'|'time-picker'|'wizard'|'switch'|'textarea'|'select'|'plain-text'|'button'|'input-addon-group'|'input-addon-button-group'|'dual-list-select'|'slider';
22

3-
interface componentTypes {
3+
interface IcomponentTypes {
44
TEXT_FIELD: 'text-field';
55
FIELD_ARRAY: 'field-array';
66
CHECKBOX: 'checkbox';
@@ -22,6 +22,6 @@ interface componentTypes {
2222
SLIDER: 'slider';
2323
}
2424

25-
declare const componentTypes: componentTypes;
25+
declare const componentTypes: IcomponentTypes;
2626

2727
export default componentTypes;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
export type DataType = 'integer'|'float'|'number'|'boolean'|'string';
22

3-
interface dataTypes {
3+
interface IdataTypes {
44
INTEGER: 'integer';
55
FLOAT: 'float';
66
NUMBER: 'number';
77
BOOLEAN: 'boolean';
88
STRING: 'string';
99
}
1010

11-
declare const dataTypes: dataTypes;
11+
declare const dataTypes: IdataTypes;
1212

1313
export default dataTypes;

packages/react-form-renderer/src/validator-types/validator-types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
interface validatorTypes {
1+
interface IvalidatorTypes {
22
REQUIRED: 'required';
33
MIN_LENGTH: 'min-length';
44
MAX_LENGTH: 'max-length';
@@ -10,6 +10,6 @@ interface validatorTypes {
1010
URL: 'url';
1111
}
1212

13-
declare const validatorTypes: validatorTypes;
13+
declare const validatorTypes: IvalidatorTypes;
1414

1515
export default validatorTypes;

scripts/generate-typings.js

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,54 @@ async function generateIndexTypes(from, to) {
1919
const files = glob
2020
.sync(`${from}/*/`)
2121
.filter((name) => !name.includes('/tests/'))
22-
.map((path) =>
23-
path
24-
.replace(/\/$/, '')
25-
.split('/')
26-
.pop()
27-
);
28-
const content = `${files.map(
29-
(file) => `export { default as ${kebabToCamel(file.split('/').shift())} } from './${file.split('.').shift()}';
22+
.map((path) => path.replace(/\/$/, '').split('/').pop());
23+
const content = `${files.map((file) => {
24+
let module;
25+
let exportName;
26+
const moduleSource = `${from}/${file.split('.').shift()}/${file}.js`.replace('//', '/');
27+
try {
28+
module = require(`${to}/${file.split('.').shift()}`);
29+
} catch (error) {
30+
console.log('Unable to find module: ', moduleSource);
31+
module = {};
32+
exportName = kebabToCamel(file.split('/').shift());
33+
}
34+
35+
let fileSource;
36+
/**
37+
* Transform default module name to build index.d.ts export name
38+
*/
39+
if (module.default) {
40+
try {
41+
fileSource = fse.readFileSync(moduleSource, { encoding: 'utf-8' });
42+
} catch (error) {
43+
console.error(`Unable to read file ${moduleSource}`);
44+
exportName = kebabToCamel(file.split('/').shift());
45+
}
46+
47+
if (fileSource) {
48+
let name = fileSource.match(/export default *[a-zA-Z\d;]+\n/gm);
49+
if (name !== null) {
50+
name = name.pop().replace('export default', '').replace(/\n/, '').replace(';', '').trim();
51+
} else {
52+
name = kebabToCamel(file.split('/').shift());
53+
}
54+
55+
exportName = name;
56+
if (!name) {
57+
throw new Error(`module name missing!: ${file}\n`);
58+
}
59+
}
60+
}
61+
62+
if (!exportName) {
63+
exportName = kebabToCamel(file.split('/').shift());
64+
}
65+
66+
return `export { default as ${exportName} } from './${file.split('.').shift()}';
3067
export * from './${file.split('.').shift()}';
31-
`
32-
)}`.replace(/,/g, '');
68+
`;
69+
})}`.replace(/,/g, '');
3370
return Promise.all([fse.writeFile(path.resolve(to, 'index.d.ts'), content)]);
3471
}
3572

0 commit comments

Comments
 (0)