Skip to content

Commit a9dc701

Browse files
authored
refactor!: remove v8 deprecated faker class parts (#2682)
1 parent 260ffc6 commit a9dc701

2 files changed

Lines changed: 27 additions & 240 deletions

File tree

docs/guide/upgrading_v9/2682.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
### Remove deprecated faker constructor and deprecated JS backwards compatibility
2+
3+
Removed deprecated faker constructor, so it is not possible anymore to just pass a locale string identifier.
4+
5+
Also removed the accessors and method that were only for JS backwards compatibility.
6+
7+
- `get/set locales`
8+
- `get/set locale`
9+
- `get/set localeFallback`
10+
- `setLocale`
11+
12+
To use the new constructor, you need to pass a locale object like:
13+
14+
```ts
15+
import { Faker, es, base } from '@faker-js/faker';
16+
17+
// A custom faker instance that does not have any fallbacks
18+
const customEsFakerWithoutFallback = new Faker({ locale: es });
19+
20+
// A custom faker instance that has only base-data as fallback, but not english data
21+
const customEsFakerWithFallback = new Faker({ locale: [es, base] });
22+
```

src/faker.ts

Lines changed: 5 additions & 240 deletions
Original file line numberDiff line numberDiff line change
@@ -163,169 +163,13 @@ export class Faker extends SimpleFaker {
163163
*/
164164
randomizer?: Randomizer;
165165
});
166-
/**
167-
* Creates a new instance of Faker.
168-
*
169-
* In most cases you should use one of the prebuilt Faker instances instead of the constructor, for example `fakerDE`, `fakerFR`, ...
170-
*
171-
* You only need to use the constructor if you need custom fallback logic or a custom locale.
172-
*
173-
* For more information see our [Localization Guide](https://fakerjs.dev/guide/localization.html).
174-
*
175-
* @param options The options to use.
176-
* @param options.locales The locale data to use.
177-
* @param options.locale The name of the main locale to use.
178-
* @param options.localeFallback The name of the fallback locale to use.
179-
*
180-
* @example
181-
* import { Faker, allLocales } from '@faker-js/faker';
182-
* // const { Faker, allLocales } = require('@faker-js/faker');
183-
*
184-
* new Faker({ locales: allLocales });
185-
*
186-
* @since 6.0.0
187-
*
188-
* @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
189-
*/
190-
constructor(options: {
191-
/**
192-
* The locale data to use for this instance.
193-
*
194-
* @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
195-
*/
196-
locales: Record<string, LocaleDefinition>;
197-
/**
198-
* The name of the main locale to use.
199-
*
200-
* @default 'en'
201-
*
202-
* @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
203-
*/
204-
locale?: string;
205-
/**
206-
* The name of the fallback locale to use.
207-
*
208-
* @default 'en'
209-
*
210-
* @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
211-
*/
212-
localeFallback?: string;
213-
});
214-
// This is somehow required for `ConstructorParameters<typeof Faker>[0]` to work
215-
/**
216-
* Creates a new instance of Faker.
217-
*
218-
* In most cases you should use one of the prebuilt Faker instances instead of the constructor, for example `fakerDE`, `fakerFR`, ...
219-
*
220-
* You only need to use the constructor if you need custom fallback logic or a custom locale.
221-
*
222-
* For more information see our [Localization Guide](https://fakerjs.dev/guide/localization.html).
223-
*
224-
* @param options The options to use.
225-
* @param options.locale The locale data to use or the name of the main locale.
226-
* @param options.locales The locale data to use.
227-
* @param options.localeFallback The name of the fallback locale to use.
228-
* @param options.randomizer The Randomizer to use.
229-
* Specify this only if you want to use it to achieve a specific goal,
230-
* such as sharing the same random generator with other instances/tools.
231-
* Defaults to faker's Mersenne Twister based pseudo random number generator.
232-
*
233-
* @example
234-
* import { Faker, es } from '@faker-js/faker';
235-
* // const { Faker, es } = require('@faker-js/faker');
236-
*
237-
* // create a Faker instance with only es data and no en fallback (=> smaller bundle size)
238-
* const customFaker = new Faker({ locale: [es] });
239-
*
240-
* customFaker.person.firstName(); // 'Javier'
241-
* customFaker.person.lastName(); // 'Ocampo Corrales'
242-
*
243-
* customFaker.music.genre(); // throws Error as this data is not available in `es`
244-
*
245-
* @since 8.0.0
246-
*/
247-
constructor(
248-
options:
249-
| {
250-
/**
251-
* The locale data to use for this instance.
252-
* If an array is provided, the first locale that has a definition for a given property will be used.
253-
*
254-
* @see mergeLocales(): For more information about how the locales are merged.
255-
*/
256-
locale: LocaleDefinition | LocaleDefinition[];
257166

258-
/**
259-
* The Randomizer to use.
260-
* Specify this only if you want to use it to achieve a specific goal,
261-
* such as sharing the same random generator with other instances/tools.
262-
*
263-
* @default generateMersenne32Randomizer()
264-
*/
265-
randomizer?: Randomizer;
266-
}
267-
| {
268-
/**
269-
* The locale data to use for this instance.
270-
*
271-
* @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
272-
*/
273-
locales: Record<string, LocaleDefinition>;
274-
/**
275-
* The name of the main locale to use.
276-
*
277-
* @default 'en'
278-
*
279-
* @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
280-
*/
281-
locale?: string;
282-
/**
283-
* The name of the fallback locale to use.
284-
*
285-
* @default 'en'
286-
*
287-
* @deprecated Use `new Faker({ locale: [locale, localeFallback] })` instead.
288-
*/
289-
localeFallback?: string;
290-
}
291-
);
292-
constructor(
293-
options:
294-
| {
295-
locale: LocaleDefinition | LocaleDefinition[];
296-
randomizer?: Randomizer;
297-
}
298-
| {
299-
locales: Record<string, LocaleDefinition>;
300-
locale?: string;
301-
localeFallback?: string;
302-
randomizer?: Randomizer;
303-
}
304-
) {
167+
constructor(options: {
168+
locale: LocaleDefinition | LocaleDefinition[];
169+
randomizer?: Randomizer;
170+
}) {
305171
super({ randomizer: options.randomizer });
306172

307-
const { locales } = options as {
308-
locales: Record<string, LocaleDefinition>;
309-
};
310-
311-
if (locales != null) {
312-
deprecated({
313-
deprecated:
314-
"new Faker({ locales: {a, b}, locale: 'a', localeFallback: 'b' })",
315-
proposed:
316-
'new Faker({ locale: [a, b, ...] }) or new Faker({ locale: a })',
317-
since: '8.0',
318-
until: '9.0',
319-
});
320-
const { locale = 'en', localeFallback = 'en' } = options as {
321-
locale: string;
322-
localeFallback: string;
323-
};
324-
options = {
325-
locale: [locales[locale], locales[localeFallback]],
326-
};
327-
}
328-
329173
let { locale } = options;
330174

331175
if (Array.isArray(locale)) {
@@ -338,7 +182,7 @@ export class Faker extends SimpleFaker {
338182
locale = mergeLocales(locale);
339183
}
340184

341-
this.rawDefinitions = locale as LocaleDefinition;
185+
this.rawDefinitions = locale;
342186
this.definitions = createLocaleProxy(this.rawDefinitions);
343187
}
344188

@@ -356,85 +200,6 @@ export class Faker extends SimpleFaker {
356200
getMetadata(): MetadataDefinition {
357201
return this.rawDefinitions.metadata ?? {};
358202
}
359-
360-
// Pure JS backwards compatibility
361-
362-
/**
363-
* Do NOT use. This property has been removed.
364-
*
365-
* @deprecated Use the constructor instead.
366-
*/
367-
private get locales(): never {
368-
throw new FakerError(
369-
'The locales property has been removed. Please use the constructor instead.'
370-
);
371-
}
372-
373-
/**
374-
* Do NOT use. This property has been removed.
375-
*
376-
* @deprecated Use the constructor instead.
377-
*/
378-
private set locales(value: never) {
379-
throw new FakerError(
380-
'The locales property has been removed. Please use the constructor instead.'
381-
);
382-
}
383-
384-
/**
385-
* Do NOT use. This property has been removed.
386-
*
387-
* @deprecated Use the constructor instead.
388-
*/
389-
private get locale(): never {
390-
throw new FakerError(
391-
'The locale property has been removed. Please use the constructor instead.'
392-
);
393-
}
394-
395-
/**
396-
* Do NOT use. This property has been removed.
397-
*
398-
* @deprecated Use the constructor instead.
399-
*/
400-
private set locale(value: never) {
401-
throw new FakerError(
402-
'The locale property has been removed. Please use the constructor instead.'
403-
);
404-
}
405-
406-
/**
407-
* Do NOT use. This property has been removed.
408-
*
409-
* @deprecated Use the constructor instead.
410-
*/
411-
private get localeFallback(): never {
412-
throw new FakerError(
413-
'The localeFallback property has been removed. Please use the constructor instead.'
414-
);
415-
}
416-
417-
/**
418-
* Do NOT use. This property has been removed.
419-
*
420-
* @deprecated Use the constructor instead.
421-
*/
422-
private set localeFallback(value: never) {
423-
throw new FakerError(
424-
'The localeFallback property has been removed. Please use the constructor instead.'
425-
);
426-
}
427-
428-
/**
429-
* Do NOT use. This property has been removed.
430-
*
431-
* @deprecated Use the constructor instead.
432-
*/
433-
private setLocale(): never {
434-
throw new FakerError(
435-
'This method has been removed. Please use the constructor instead.'
436-
);
437-
}
438203
}
439204

440205
export type FakerOptions = ConstructorParameters<typeof Faker>[0];

0 commit comments

Comments
 (0)