Skip to content

Commit

Permalink
fix: setup deprecation support as early as possible, restore inspecto…
Browse files Browse the repository at this point in the history
…r support (#9495)

* fix: inflector support

* fix inspector support

* fix lint

* fixup

* run pnpm dedupe

* fixups
  • Loading branch information
runspired committed Jun 20, 2024
1 parent 16f9992 commit 9b12c4e
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 120 deletions.
2 changes: 2 additions & 0 deletions packages/-ember-data/app/initializers/ember-data.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import '@ember-data/request-utils/deprecation-support';

/*
This code initializes EmberData in an Ember application.
*/
Expand Down
2 changes: 0 additions & 2 deletions packages/-ember-data/src/store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import '@ember-data/request-utils/deprecation-support';

import JSONAPICache from '@ember-data/json-api';
import {
adapterFor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export default function (babel) {
specifier.scope.removeOwnBinding(localBindingName);
specifier.remove();
});
}
if (path.get('specifiers').length === 0) {
path.remove();
if (path.get('specifiers').length === 0) {
path.remove();
}
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export default function (babel) {
specifier.scope.removeOwnBinding(localBindingName);
specifier.remove();
});
}
if (path.get('specifiers').length === 0) {
path.remove();
if (path.get('specifiers').length === 0) {
path.remove();
}
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ export default function (babel) {
specifier.scope.removeOwnBinding(localBindingName);
specifier.remove();
});
}
if (path.get('specifiers').length === 0) {
path.remove();
if (path.get('specifiers').length === 0) {
path.remove();
}
}
},

Expand Down
4 changes: 2 additions & 2 deletions packages/build-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export function setConfig(context: object, appRoot: string, config: WarpDriveCon
const FEATURES = getFeatures(env.PRODUCTION);

const includeDataAdapterInProduction =
typeof config.includeDataAdapterInProduction === 'boolean' ? config.includeDataAdapterInProduction : false;
const includeDataAdapter = env.PRODUCTION ? includeDataAdapterInProduction : false;
typeof config.includeDataAdapterInProduction === 'boolean' ? config.includeDataAdapterInProduction : true;
const includeDataAdapter = env.PRODUCTION ? includeDataAdapterInProduction : true;

const finalizedConfig: InternalWarpDriveConfig = {
debug: debugOptions,
Expand Down
5 changes: 4 additions & 1 deletion packages/debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@
"ember-addon": {
"main": "addon-main.cjs",
"type": "addon",
"version": 2
"version": 2,
"app-js": {
"./data-adapter.js": "./dist/data-adapter.js"
}
},
"volta": {
"extends": "../../package.json"
Expand Down
10 changes: 9 additions & 1 deletion packages/debug/src/data-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import DataAdapter from '@ember/debug/data-adapter';
import { addObserver, removeObserver } from '@ember/object/observers';
import { inject as service } from '@ember/service';

import { getGlobalConfig, macroCondition } from '@embroider/macros';

import type Model from '@ember-data/model';
import { capitalize, underscore } from '@ember-data/request-utils/string';
import type Store from '@ember-data/store';
Expand Down Expand Up @@ -151,7 +153,7 @@ function typesMapFor(store: Store): Map<string, boolean> {
@extends DataAdapter
@private
*/
export default class extends DataAdapter<Model> {
class InspectorDataAdapter extends DataAdapter<Model> {
@service('store') declare store: Store;

/**
Expand Down Expand Up @@ -435,3 +437,9 @@ export default class extends DataAdapter<Model> {
return release;
}
}

export default macroCondition(
getGlobalConfig<{ WarpDrive: { includeDataAdapter: boolean } }>().WarpDrive.includeDataAdapter
)
? InspectorDataAdapter
: null;
147 changes: 147 additions & 0 deletions packages/request-utils/src/deprecation-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,153 @@ if (DEPRECATE_EMBER_INFLECTOR) {
// eslint-disable-next-line @typescript-eslint/unbound-method
const originalUncountable = inflector.uncountable;

// copy over any already registered rules
type DefaultRules = {
plurals: [RegExp, string][];
singular: [RegExp, string][];
irregularPairs: [string, string][];
uncountable: string[];
};
type InternalRules = {
plurals: [RegExp, string][];
singular: [RegExp, string][];

// [str1, str2] =>
// { [str1.lower]: str2 }
// { [str2.lower]: str2 }
irregular: Record<string, string>;

// [str1, str2] =>
// { [str2.lower]: str1 }
// { [str1.lower]: str1 }
irregularInverse: Record<string, string>;

// lower cased string
uncountable: Record<string, boolean>;
};
const { defaultRules } = Inflector as unknown as { defaultRules: DefaultRules };
const { rules } = inflector as unknown as { rules: InternalRules };

const pluralMap = new Map(defaultRules.plurals);
const singularMap = new Map(defaultRules.singular);
const irregularMap = new Map<string, string>();
const toIgnore = new Set<string>();
const uncountableSet = new Set(defaultRules.uncountable);

defaultRules.irregularPairs.forEach(([single, plur]) => {
irregularMap.set(single.toLowerCase(), plur);
toIgnore.add(plur.toLowerCase());
});
const irregularLookups = new Map<string, string>();
Object.keys(rules.irregular).forEach((single) => {
const plur = rules.irregular[single];
irregularLookups.set(single, plur);
});

// load plurals
rules.plurals.forEach(([regex, replacement]) => {
if (pluralMap.has(regex)) {
return;
}

plural(regex, replacement);

deprecate(
`WarpDrive/EmberData no longer uses ember-inflector for pluralization.\nPlease \`import { plural } from '@ember-data/request-utils/string';\` instead to register a custom pluralization rule for use with EmberData.`,
false,
{
id: 'warp-drive.ember-inflector',
until: '6.0.0',
for: 'warp-drive',
since: {
enabled: '5.3.4',
available: '5.3.4',
},
url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',
}
);
});

// load singulars
rules.singular.forEach(([regex, replacement]) => {
if (singularMap.has(regex)) {
return;
}

singular(regex, replacement);

deprecate(
`WarpDrive/EmberData no longer uses ember-inflector for singularization.\nPlease \`import { singular } from '@ember-data/request-utils/string';\` instead to register a custom singularization rule for use with EmberData.`,
false,
{
id: 'warp-drive.ember-inflector',
until: '6.0.0',
for: 'warp-drive',
since: {
enabled: '5.3.4',
available: '5.3.4',
},
url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',
}
);
});

// load irregulars
Object.keys(rules.irregular).forEach((single) => {
const plur = rules.irregular[single];
const defaultPlur = irregularMap.get(single);
if (defaultPlur && defaultPlur === plur) {
return;
}

if (toIgnore.has(single)) {
return;
}

const actualSingle = irregularLookups.get(plur.toLowerCase()) || single;
toIgnore.add(plur.toLowerCase());
irregular(actualSingle, plur);

deprecate(
`WarpDrive/EmberData no longer uses ember-inflector for irregular rules.\nPlease \`import { irregular } from '@ember-data/request-utils/string';\` instead to register a custom irregular rule for use with EmberData for '${actualSingle}' <=> '${plur}'.`,
false,
{
id: 'warp-drive.ember-inflector',
until: '6.0.0',
for: 'warp-drive',
since: {
enabled: '5.3.4',
available: '5.3.4',
},
url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',
}
);
});

// load uncountables
Object.keys(rules.uncountable).forEach((word) => {
if (uncountableSet.has(word) || rules.uncountable[word] !== true) {
return;
}

uncountable(word);

deprecate(
`WarpDrive/EmberData no longer uses ember-inflector for uncountable rules.\nPlease \`import { uncountable } from '@ember-data/request-utils/string';\` instead to register a custom uncountable rule for '${word}' for use with EmberData.`,
false,
{
id: 'warp-drive.ember-inflector',
until: '6.0.0',
for: 'warp-drive',
since: {
enabled: '5.3.4',
available: '5.3.4',
},
url: 'https://deprecations.emberjs.com/id/warp-drive.ember-inflector',
}
);
});

inflector.plural = function (...args: Parameters<typeof originalPlural>) {
plural(...args);

Expand Down
Loading

0 comments on commit 9b12c4e

Please sign in to comment.