Skip to content

Update types generator to use correct variable/function names. #1166

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

Merged
merged 5 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta } from "@data-driven-forms/react-form-renderer";

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

export default validationError;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Meta } from "@data-driven-forms/react-form-renderer";

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

export default validationError;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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';

interface componentTypes {
interface IcomponentTypes {
TEXT_FIELD: 'text-field';
FIELD_ARRAY: 'field-array';
CHECKBOX: 'checkbox';
Expand All @@ -22,6 +22,6 @@ interface componentTypes {
SLIDER: 'slider';
}

declare const componentTypes: componentTypes;
declare const componentTypes: IcomponentTypes;

export default componentTypes;
4 changes: 2 additions & 2 deletions packages/react-form-renderer/src/data-types/data-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export type DataType = 'integer'|'float'|'number'|'boolean'|'string';

interface dataTypes {
interface IdataTypes {
INTEGER: 'integer';
FLOAT: 'float';
NUMBER: 'number';
BOOLEAN: 'boolean';
STRING: 'string';
}

declare const dataTypes: dataTypes;
declare const dataTypes: IdataTypes;

export default dataTypes;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
interface validatorTypes {
interface IvalidatorTypes {
REQUIRED: 'required';
MIN_LENGTH: 'min-length';
MAX_LENGTH: 'max-length';
Expand All @@ -10,6 +10,6 @@ interface validatorTypes {
URL: 'url';
}

declare const validatorTypes: validatorTypes;
declare const validatorTypes: IvalidatorTypes;

export default validatorTypes;
57 changes: 47 additions & 10 deletions scripts/generate-typings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,54 @@ async function generateIndexTypes(from, to) {
const files = glob
.sync(`${from}/*/`)
.filter((name) => !name.includes('/tests/'))
.map((path) =>
path
.replace(/\/$/, '')
.split('/')
.pop()
);
const content = `${files.map(
(file) => `export { default as ${kebabToCamel(file.split('/').shift())} } from './${file.split('.').shift()}';
.map((path) => path.replace(/\/$/, '').split('/').pop());
const content = `${files.map((file) => {
let module;
let exportName;
const moduleSource = `${from}/${file.split('.').shift()}/${file}.js`.replace('//', '/');
try {
module = require(`${to}/${file.split('.').shift()}`);
} catch (error) {
console.log('Unable to find module: ', moduleSource);
module = {};
exportName = kebabToCamel(file.split('/').shift());
}

let fileSource;
/**
* Transform default module name to build index.d.ts export name
*/
if (module.default) {
try {
fileSource = fse.readFileSync(moduleSource, { encoding: 'utf-8' });
} catch (error) {
console.error(`Unable to read file ${moduleSource}`);
exportName = kebabToCamel(file.split('/').shift());
}

if (fileSource) {
let name = fileSource.match(/export default *[a-zA-Z\d;]+\n/gm);
if (name !== null) {
name = name.pop().replace('export default', '').replace(/\n/, '').replace(';', '').trim();
} else {
name = kebabToCamel(file.split('/').shift());
}

exportName = name;
if (!name) {
throw new Error(`module name missing!: ${file}\n`);
}
}
}

if (!exportName) {
exportName = kebabToCamel(file.split('/').shift());
}

return `export { default as ${exportName} } from './${file.split('.').shift()}';
export * from './${file.split('.').shift()}';
`
)}`.replace(/,/g, '');
`;
})}`.replace(/,/g, '');
return Promise.all([fse.writeFile(path.resolve(to, 'index.d.ts'), content)]);
}

Expand Down