Skip to content

Commit

Permalink
fix(imports): always split type and value
Browse files Browse the repository at this point in the history
  • Loading branch information
anymaniax committed Jul 3, 2022
1 parent c594bac commit bd97425
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 66 deletions.
170 changes: 109 additions & 61 deletions src/core/generators/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
GeneratorVerbOptions,
} from '../../types/generator';
import { camel } from '../../utils/case';
import { BODY_TYPE_NAME } from './mutator';

export const generateImports = ({
imports = [],
Expand Down Expand Up @@ -61,33 +60,81 @@ export const generateMutatorImports = (
(a, b) => a.name === b.name && a.default === b.default,
)
.map((mutator) => {
const path = `${oneMore ? '../' : ''}${mutator.path}`;
const importDefault = mutator.default
? `${mutator.name}${
mutator.hasErrorType || mutator.bodyTypeName
? `, { ${
mutator.hasErrorType
? `ErrorType as ${mutator.errorTypeName}`
: ''
}${mutator.hasErrorType && mutator.bodyTypeName ? ',' : ''} ${
mutator.bodyTypeName
? `${BODY_TYPE_NAME} as ${mutator.bodyTypeName}`
: ''
} }`
: ''
}`
: `{ ${mutator.name}${
mutator.hasErrorType ? `, ${mutator.errorTypeName}` : ''
}${mutator.bodyTypeName ? `, ${mutator.bodyTypeName}` : ''} }`;

return `import ${importDefault} from '${oneMore ? '../' : ''}${
mutator.path
}'`;
? mutator.name
: `{ ${mutator.name} }`;

let dep = `import ${importDefault} from '${path}'`;

if (mutator.hasErrorType || mutator.bodyTypeName) {
dep += '\n';
dep += `import type { ${
mutator.hasErrorType ? `${mutator.errorTypeName}` : ''
}${mutator.hasErrorType && mutator.bodyTypeName ? ', ' : ''}${
mutator.bodyTypeName ? `${mutator.bodyTypeName}` : ''
} } from '${path}'`;
}

return dep;
})
.join('\n');

return imports ? imports + '\n' : '';
};

const generateDependency = ({
deps,
isAllowSyntheticDefaultImports,
dependency,
specsName,
key,
onlyTypes,
}: {
key: string;
deps: GeneratorImport[];
dependency: string;
specsName: Record<string, string>;
isAllowSyntheticDefaultImports: boolean;
onlyTypes: boolean;
}) => {
const defaultDep = deps.find(
(e) =>
e.default &&
(isAllowSyntheticDefaultImports || !e.syntheticDefaultImport),
);
const syntheticDefaultImportDep = !isAllowSyntheticDefaultImports
? deps.find((e) => e.syntheticDefaultImport)
: undefined;

const depsString = uniq(
deps
.filter((e) => !e.default && !e.syntheticDefaultImport)
.map(({ name, alias }) => (alias ? `${name} as ${alias}` : name)),
).join(',\n ');

let importString = '';

const syntheticDefaultImport = syntheticDefaultImportDep
? `import * as ${syntheticDefaultImportDep.name} from '${dependency}';`
: '';

if (syntheticDefaultImport) {
if (deps.length === 1) {
return syntheticDefaultImport;
}
importString += `${syntheticDefaultImport}\n`;
}

importString += `import ${onlyTypes ? 'type ' : ''}${
defaultDep ? `${defaultDep.name}${depsString ? ',' : ''}` : ''
}${depsString ? `{\n ${depsString}\n}` : ''} from '${dependency}${
key !== 'default' && specsName[key] ? `/${specsName[key]}` : ''
}'`;

return importString;
};

export const addDependency = ({
implementation,
exports,
Expand All @@ -112,59 +159,60 @@ export const addDependency = ({
}

const groupedBySpecKey = toAdds.reduce<
Record<string, { deps: GeneratorImport[]; values: boolean }>
Record<string, { types: GeneratorImport[]; values: GeneratorImport[] }>
>((acc, dep) => {
const key = hasSchemaDir && dep.specKey ? dep.specKey : 'default';

if (
dep.values &&
(isAllowSyntheticDefaultImports || !dep.syntheticDefaultImport)
) {
acc[key] = {
...acc[key],
values: [...(acc[key]?.values ?? []), dep],
};

return acc;
}

acc[key] = {
values:
acc[key]?.values ||
(dep.values &&
(isAllowSyntheticDefaultImports || !dep.syntheticDefaultImport)) ||
false,
deps: [...(acc[key]?.deps ?? []), dep],
...acc[key],
types: [...(acc[key]?.types ?? []), dep],
};

return acc;
}, {});

return Object.entries(groupedBySpecKey)
.map(([key, { values, deps }]) => {
const defaultDep = deps.find(
(e) =>
e.default &&
(isAllowSyntheticDefaultImports || !e.syntheticDefaultImport),
);
const syntheticDefaultImportDep = !isAllowSyntheticDefaultImports
? deps.find((e) => e.syntheticDefaultImport)
: undefined;

const depsString = uniq(
deps
.filter((e) => !e.default && !e.syntheticDefaultImport)
.map(({ name, alias }) => (alias ? `${name} as ${alias}` : name)),
).join(',\n ');

let importString = '';

const syntheticDefaultImport = syntheticDefaultImportDep
? `import * as ${syntheticDefaultImportDep.name} from '${dependency}';`
: '';

if (syntheticDefaultImport) {
if (deps.length === 1) {
return syntheticDefaultImport;
}
importString += `${syntheticDefaultImport}\n`;
.map(([key, { values, types }]) => {
let dep = '';

if (values) {
dep += generateDependency({
deps: values,
isAllowSyntheticDefaultImports,
dependency,
specsName,
key,
onlyTypes: false,
});
}

importString += `import ${!values ? 'type ' : ''}${
defaultDep ? `${defaultDep.name}${depsString ? ',' : ''}` : ''
}${depsString ? `{\n ${depsString}\n}` : ''} from '${dependency}${
key !== 'default' && specsName[key] ? `/${specsName[key]}` : ''
}'`;
if (types) {
if (values) {
dep += '\n';
}
dep += generateDependency({
deps: types,
isAllowSyntheticDefaultImports,
dependency,
specsName,
key,
onlyTypes: true,
});
}

return importString;
return dep;
})
.join('\n');
};
Expand Down
12 changes: 7 additions & 5 deletions src/core/writers/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ export const writeSpecs = async (
...implementationPaths,
];

await executeHook(
'afterAllFilesWrite',
options.hooks.afterAllFilesWrite,
paths,
);
if (options.hooks.afterAllFilesWrite) {
await executeHook(
'afterAllFilesWrite',
options.hooks.afterAllFilesWrite,
paths,
);
}

if (output.prettier) {
try {
Expand Down

1 comment on commit bd97425

@vercel
Copy link

@vercel vercel bot commented on bd97425 Jul 3, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.