feat: transform core modules to standalone module functions - #3857
feat: transform core modules to standalone module functions#3857ST-DDT wants to merge 14 commits into
Conversation
✅ Deploy Preview for fakerjs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## next #3857 +/- ##
==========================================
+ Coverage 98.91% 98.94% +0.03%
==========================================
Files 924 961 +37
Lines 3224 3325 +101
Branches 567 609 +42
==========================================
+ Hits 3189 3290 +101
Misses 31 31
Partials 4 4
🚀 New features to boost your workflow:
|
|
Just for clarification: Would you like for us to use qualified method names from the start? This is kind of relevant for the later public API: Will we only export from root or also export via the module paths e.g. |
Partly that was my thought regarding yes, but I did not fully thought that through yet. // instead of
import { arrayElement as helpersArrayElement } from './array-element';
import { arrayElements as helpersArrayElements } from './array-elements';
import { enumValue as helpersEnumValue } from './enum-value';
// something like
import { arrayElement as helpersArrayElement, arrayElements as helpersArrayElements, enumValue as helpersEnumValue } from '../helpers';but thinking further... maybe we move the module class into a separate file, so we can import from index... 🦆 might not work either, because we would need to import the module class in index to pass further to top again 🤔 ... best what we could try to achieve would be something like: import * as helpers from './?whatever?';
import * as helpers from './?whatever?';
import * as string from '../string';
export class SimpleHelpersModule extends SimpleModuleBase {
slugify(string: string = ''): string {
return helpers.slugify(this.faker.fakerCore, string);
}
// string.alphanumericI know that what I start to propose here might need fundamental movements of files, and this is why I raising this right now (I'm sorry 🥲) I can also later (not in company-working-hours try to thinker with claude about this, maybe it has some enlightening ideas) Edit: |
|
IMO moving the module to a separate file (and not including it in the index) would work for me too. Maybe prepare a proposal so that we can talk about it later. Though I dont have time this weekend. Note: I ammended my previous comment. Not sure whether you have seen it. |
👍
🤝 I will also hide myself this weekend deeply into Gothic 1 Remake 😇
THX! I have NOT seen it 👍 It is kinda a sibling topic, because this is then how we structure the external API But maybe we should outsource this discussion to a separate place |
|
Alternatively, split the files as proposed here, then later refactor the imports if needed. |
|
I now had a talk with Claude ( In this PR we can then extend it by adding the standalone functions by just doing this: // ./array-element.ts
import { ... } from '../<other-module>'
import { ... } from './<other-same-module-standalone-fn>'
export function arrayElement
// ./module.ts
import { arrayElement } from './array-element'
export class SimpleHelpersModule
// ./index.ts
import * from './array-element'
import * from './module'I will spin up a PR for the moving, and we can move on there and decide if this is the wanted solution or if it contains any other problems I have not seen yet. |
be55e5f to
71b6418
Compare
|
Ready for review again. The PR may be easier to review if you look at the individual commits. The auto generated commits can be recreated with: pnpm tsx scripts/temp-tranform-once.ts
pnpm tsx scripts/generate-module-tree.ts |
Shinigami92
left a comment
There was a problem hiding this comment.
I stopped reviewing here and first would like to get the commented points discussed
There was a problem hiding this comment.
nitpick (non-blocking): not really relevant for this PR and we can outsource it into a follow-up PR, but I'm not sure if I like the _ prefix for internal in our source folder 🤔
maybe we could just add an internal folder in every module folder? this resolves also potential naming collisions.
There was a problem hiding this comment.
This affects final files:
- 3x color
- 1x date
- 1x finance
- 2x helpers
- 1x internet
- 1x person
- 1x string
- 1x word
You can see them all here using the _ file filter on the left side.
There was a problem hiding this comment.
I don't like folders that only contain a single file.
Also note: The internals are sometimes "faker global"-internals and not "module-exclusive".
The main reason they are prefixed with _ to visually separate them from files that contain SMFs.
We can remove the _ prefix, no collisions will occur, as I renamed the files that are likely to collide beforehand.
There was a problem hiding this comment.
just another soft-contra for _ prefixed: we have e.g. a _local folder in root which is git-ignored, but excluded from pnpm run clean
so we are introducing different meanings for a _ file/folder
| import { arrayElement } from '../helpers/array-element'; | ||
| import { rangeToNumber } from '../helpers/range-to-number'; | ||
| import { weightedArrayElement } from '../helpers/weighted-array-element'; |
There was a problem hiding this comment.
suggestion: here is the point where I would like to see that imports are used from their module/index
| import { arrayElement } from '../helpers/array-element'; | |
| import { rangeToNumber } from '../helpers/range-to-number'; | |
| import { weightedArrayElement } from '../helpers/weighted-array-element'; | |
| import { arrayElement, rangeToNumber, weightedArrayElement } from '../helpers'; |
or does this not work? 🤔
There was a problem hiding this comment.
That does work, at the risk of including any side effects the index contains.
There was a problem hiding this comment.
I'm not sure or aware that we plan to include side-effects in index 😅 I would even call that an anti-pattern if there would be side-effects in index files
There was a problem hiding this comment.
The faker main index has some kind of side effects, mainly regarding the pre-created faker instances.
I think I mentioned that somewhen/somewhere related to PURE annoations.
There was a problem hiding this comment.
A root index (or often also a main.ts in other context) is a bit differently handled as index files in sub directories which just reexport their folder content upwards.
There was a problem hiding this comment.
@xDivisionByZerox Which variant do you prefer for foreign module method imports?
- A) Import from the exact file that exports the method
- B) Import from the foreign module index instead
?
There was a problem hiding this comment.
Currently, most of our imports import directly from the actual source file, not from a parent index:
faker/src/modules/airline/module.ts
Lines 1 to 2 in acd5fda
faker/src/modules/helpers/module.ts
Lines 1 to 4 in acd5fda
faker/src/modules/internet/module.ts
Lines 2 to 5 in acd5fda
(excluding the new module imports)
|
Random fun fact: I just searched and found the issues which laid the foundation for this/these changes.
|
71b6418 to
6b0e50b
Compare
|
Should the SMFs be annotated with |

Split from #3748
Transforms the core modules to standalone module functions.
These core modules are interdependent and thus cannot be transformed on their own.
Most of the conversion steps are done automatically, but some changes have been done manually.
Please note that I renamed some util files to
_, to distinguish files that export an API SMF from those that don't.This PR can be reviewed more easily using two ways
1. Compare automated steps vs PR
2. Verify no diff in modules to #3748 and look at the manual transform commits
Check the individual module transform commits:
Helpers(#3748)Number(#3748)String(#3748)Generate Module Tree(#3748) (Technically automated, but might be interesting anyway)Final Fixes and Cleanup(#3748)Please do not merge any PRs changing anything in the modules after this PR until all modules are transformed.