From ffa2c18e4900d65e4f725bcf2177dc0d19541b60 Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Sun, 10 Dec 2023 01:01:37 -0800 Subject: [PATCH] macros! --- .../-ember-data/addon-test-support/index.js | 2 +- .../src/-private/builders/save-record.ts | 2 +- packages/adapter/src/-private/utils/fetch.ts | 2 +- .../src/-private/utils/serialize-into-hash.ts | 2 +- .../-private/utils/serialize-query-params.ts | 2 +- packages/adapter/src/error.js | 2 +- packages/adapter/src/index.ts | 2 +- packages/adapter/src/json-api.ts | 2 +- packages/debug/addon/index.js | 2 +- .../graph/src/-private/-edge-definition.ts | 2 +- .../-private/debug/assert-polymorphic-type.ts | 2 +- packages/graph/src/-private/graph.ts | 2 +- .../operations/add-to-related-records.ts | 2 +- .../operations/remove-from-related-records.ts | 2 +- .../src/-private/builders/save-record.ts | 2 +- packages/json-api/src/-private/cache.ts | 2 +- packages/json-api/src/-private/serialize.ts | 2 +- packages/legacy-compat/src/index.ts | 2 +- .../identifier-has-id.ts | 3 +- .../legacy-data-fetch.js | 3 +- .../legacy-network-handler.ts | 3 +- .../serializer-response.ts | 3 +- .../src/legacy-network-handler/snapshot.ts | 3 +- packages/model/src/-private/attr.js | 2 +- .../-private/debug/assert-polymorphic-type.ts | 2 +- packages/model/src/-private/hooks.ts | 2 +- .../-private/legacy-relationships-support.ts | 3 +- packages/model/src/-private/many-array.ts | 2 +- packages/model/src/-private/model-methods.ts | 3 +- packages/model/src/-private/notify-changes.ts | 2 +- .../model/src/-private/promise-belongs-to.ts | 2 +- .../model/src/-private/promise-many-array.ts | 2 +- packages/model/src/-private/record-state.ts | 2 +- .../model/src/-private/references/has-many.ts | 2 +- packages/model/src/migration-support.ts | 3 +- .../index.js | 72 +++++++++++++++++++ .../virtual-packages/macros.d.ts | 24 +++++++ packages/request-utils/src/index.ts | 3 +- .../rest/src/-private/builders/save-record.ts | 2 +- packages/schema-record/src/-base-fields.ts | 2 +- packages/schema-record/src/record.ts | 2 +- packages/schema-record/src/schema.ts | 2 +- packages/store/src/-private/cache-handler.ts | 3 +- .../store/src/-private/caches/cache-utils.ts | 3 +- packages/store/src/-private/document.ts | 3 +- .../legacy-model-support/record-reference.ts | 3 +- .../managers/cache-capabilities-manager.ts | 3 +- .../-private/managers/notification-manager.ts | 2 +- .../src/-private/network/request-cache.ts | 3 +- .../record-arrays/identifier-array.ts | 3 +- packages/store/src/-private/store-service.ts | 2 +- .../src/-private/utils/construct-resource.ts | 3 +- packages/tracking/src/index.ts | 3 +- .../app/utils/pagination-links.ts | 2 +- 54 files changed, 150 insertions(+), 68 deletions(-) create mode 100644 packages/private-build-infra/src/transforms/babel-plugin-transform-debug-macros/index.js create mode 100644 packages/private-build-infra/virtual-packages/macros.d.ts diff --git a/packages/-ember-data/addon-test-support/index.js b/packages/-ember-data/addon-test-support/index.js index 96baceef96..30bae26546 100644 --- a/packages/-ember-data/addon-test-support/index.js +++ b/packages/-ember-data/addon-test-support/index.js @@ -1,9 +1,9 @@ -import { assert } from '@ember/debug'; import { render as renderTemplate, settled } from '@ember/test-helpers'; import * as QUnit from 'qunit'; import { PRODUCTION } from '@ember-data/env'; +import { assert } from '@ember-data/macros'; /* Temporary replacement for the render test helper diff --git a/packages/active-record/src/-private/builders/save-record.ts b/packages/active-record/src/-private/builders/save-record.ts index 7b6a5da0e4..eec4ae5b0e 100644 --- a/packages/active-record/src/-private/builders/save-record.ts +++ b/packages/active-record/src/-private/builders/save-record.ts @@ -1,8 +1,8 @@ -import { assert } from '@ember/debug'; import { underscore } from '@ember/string'; import { pluralize } from 'ember-inflector'; +import { assert } from '@ember-data/macros'; import { buildBaseURL, type CreateRecordUrlOptions, diff --git a/packages/adapter/src/-private/utils/fetch.ts b/packages/adapter/src/-private/utils/fetch.ts index 8dd752dff9..514e668897 100644 --- a/packages/adapter/src/-private/utils/fetch.ts +++ b/packages/adapter/src/-private/utils/fetch.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; type FetchFunction = (input: RequestInfo, init?: RequestInit | undefined) => Promise; diff --git a/packages/adapter/src/-private/utils/serialize-into-hash.ts b/packages/adapter/src/-private/utils/serialize-into-hash.ts index 8bc1651c42..cfb5a94b5e 100644 --- a/packages/adapter/src/-private/utils/serialize-into-hash.ts +++ b/packages/adapter/src/-private/utils/serialize-into-hash.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { type Snapshot, upgradeStore } from '@ember-data/legacy-compat/-private'; import type { diff --git a/packages/adapter/src/-private/utils/serialize-query-params.ts b/packages/adapter/src/-private/utils/serialize-query-params.ts index d9315ea68a..56b0e47120 100644 --- a/packages/adapter/src/-private/utils/serialize-query-params.ts +++ b/packages/adapter/src/-private/utils/serialize-query-params.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; const RBRACKET = /\[\]$/; diff --git a/packages/adapter/src/error.js b/packages/adapter/src/error.js index 23eb11c62b..92174c4696 100644 --- a/packages/adapter/src/error.js +++ b/packages/adapter/src/error.js @@ -1,7 +1,7 @@ /** @module @ember-data/adapter/error */ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; /** ## Overview diff --git a/packages/adapter/src/index.ts b/packages/adapter/src/index.ts index a1751a64c3..dd05387d45 100644 --- a/packages/adapter/src/index.ts +++ b/packages/adapter/src/index.ts @@ -187,7 +187,7 @@ By default when using with Ember you only need to implement this hook if you wan @main @ember-data/adapter */ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import EmberObject from '@ember/object'; import { inject as service } from '@ember/service'; diff --git a/packages/adapter/src/json-api.ts b/packages/adapter/src/json-api.ts index 98e2a21611..48c07b2b31 100644 --- a/packages/adapter/src/json-api.ts +++ b/packages/adapter/src/json-api.ts @@ -1,7 +1,7 @@ /** @module @ember-data/adapter/json-api */ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { dasherize } from '@ember/string'; import { pluralize } from 'ember-inflector'; diff --git a/packages/debug/addon/index.js b/packages/debug/addon/index.js index 159fa0cbc3..40577a9f13 100644 --- a/packages/debug/addon/index.js +++ b/packages/debug/addon/index.js @@ -24,7 +24,7 @@ @main @ember-data/debug */ import { A } from '@ember/array'; -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import DataAdapter from '@ember/debug/data-adapter'; import { addObserver, removeObserver } from '@ember/object/observers'; import { inject as service } from '@ember/service'; diff --git a/packages/graph/src/-private/-edge-definition.ts b/packages/graph/src/-private/-edge-definition.ts index cd74e2ac07..90812dc37f 100644 --- a/packages/graph/src/-private/-edge-definition.ts +++ b/packages/graph/src/-private/-edge-definition.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { DEBUG } from '@ember-data/env'; import type Store from '@ember-data/store'; diff --git a/packages/graph/src/-private/debug/assert-polymorphic-type.ts b/packages/graph/src/-private/debug/assert-polymorphic-type.ts index 3f10c4dc76..823b1600af 100644 --- a/packages/graph/src/-private/debug/assert-polymorphic-type.ts +++ b/packages/graph/src/-private/debug/assert-polymorphic-type.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { DEBUG } from '@ember-data/env'; import type { UpgradedMeta } from '../-edge-definition'; import type { StableRecordIdentifier } from '@warp-drive/core-types'; diff --git a/packages/graph/src/-private/graph.ts b/packages/graph/src/-private/graph.ts index 9153fb0678..cd5b91b60c 100644 --- a/packages/graph/src/-private/graph.ts +++ b/packages/graph/src/-private/graph.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { LOG_GRAPH } from '@ember-data/debugging'; import { DEBUG } from '@ember-data/env'; diff --git a/packages/graph/src/-private/operations/add-to-related-records.ts b/packages/graph/src/-private/operations/add-to-related-records.ts index 65d37e06f3..923eb56a0e 100644 --- a/packages/graph/src/-private/operations/add-to-related-records.ts +++ b/packages/graph/src/-private/operations/add-to-related-records.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import type { StableRecordIdentifier } from '@warp-drive/core-types'; import type { AddToRelatedRecordsOperation } from '@warp-drive/core-types/graph'; diff --git a/packages/graph/src/-private/operations/remove-from-related-records.ts b/packages/graph/src/-private/operations/remove-from-related-records.ts index a7d7536065..1bb3bac723 100644 --- a/packages/graph/src/-private/operations/remove-from-related-records.ts +++ b/packages/graph/src/-private/operations/remove-from-related-records.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import type { StableRecordIdentifier } from '@warp-drive/core-types'; import type { RemoveFromRelatedRecordsOperation } from '@warp-drive/core-types/graph'; diff --git a/packages/json-api/src/-private/builders/save-record.ts b/packages/json-api/src/-private/builders/save-record.ts index 1304666dde..cd4af1dee3 100644 --- a/packages/json-api/src/-private/builders/save-record.ts +++ b/packages/json-api/src/-private/builders/save-record.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { pluralize } from 'ember-inflector'; diff --git a/packages/json-api/src/-private/cache.ts b/packages/json-api/src/-private/cache.ts index e38bafb1f8..8515a0d13c 100644 --- a/packages/json-api/src/-private/cache.ts +++ b/packages/json-api/src/-private/cache.ts @@ -1,7 +1,7 @@ /** * @module @ember-data/json-api */ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { LOG_MUTATIONS, LOG_OPERATIONS, LOG_REQUESTS } from '@ember-data/debugging'; import { DEPRECATE_RELATIONSHIP_REMOTE_UPDATE_CLEARING_LOCAL_STATE } from '@ember-data/deprecations'; diff --git a/packages/json-api/src/-private/serialize.ts b/packages/json-api/src/-private/serialize.ts index df166dcd53..e8728cd044 100644 --- a/packages/json-api/src/-private/serialize.ts +++ b/packages/json-api/src/-private/serialize.ts @@ -1,7 +1,7 @@ /** * @module @ember-data/json-api/request */ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import type { AttributesHash, JsonApiResource } from '@ember-data/store/-types/q/record-data-json-api'; import type { StableRecordIdentifier } from '@warp-drive/core-types'; diff --git a/packages/legacy-compat/src/index.ts b/packages/legacy-compat/src/index.ts index 41b2b6bcd7..fc3c9390c8 100644 --- a/packages/legacy-compat/src/index.ts +++ b/packages/legacy-compat/src/index.ts @@ -1,6 +1,6 @@ import { getOwner } from '@ember/application'; -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import type Store from '@ember-data/store'; import { recordIdentifierFor } from '@ember-data/store'; import { _deprecatingNormalize } from '@ember-data/store/-private'; diff --git a/packages/legacy-compat/src/legacy-network-handler/identifier-has-id.ts b/packages/legacy-compat/src/legacy-network-handler/identifier-has-id.ts index 0e4164be40..824e5db74c 100644 --- a/packages/legacy-compat/src/legacy-network-handler/identifier-has-id.ts +++ b/packages/legacy-compat/src/legacy-network-handler/identifier-has-id.ts @@ -1,5 +1,4 @@ -import { assert } from '@ember/debug'; - +import { assert } from '@ember-data/macros'; import type { StableExistingRecordIdentifier } from '@warp-drive/core-types/identifier'; export function assertIdentifierHasId(identifier: unknown): asserts identifier is StableExistingRecordIdentifier { diff --git a/packages/legacy-compat/src/legacy-network-handler/legacy-data-fetch.js b/packages/legacy-compat/src/legacy-network-handler/legacy-data-fetch.js index 841baf2b7c..8824e9043b 100644 --- a/packages/legacy-compat/src/legacy-network-handler/legacy-data-fetch.js +++ b/packages/legacy-compat/src/legacy-network-handler/legacy-data-fetch.js @@ -1,6 +1,5 @@ -import { assert } from '@ember/debug'; - import { DEBUG } from '@ember-data/env'; +import { assert } from '@ember-data/macros'; import { iterateData, payloadIsNotBlank } from './legacy-data-utils'; import { normalizeResponseHelper } from './serializer-response'; diff --git a/packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts b/packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts index c741caf29c..686c2561c5 100644 --- a/packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts +++ b/packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts @@ -1,9 +1,8 @@ -import { assert } from '@ember/debug'; - import { importSync } from '@embroider/macros'; import { LOG_PAYLOADS } from '@ember-data/debugging'; import { DEBUG, TESTING } from '@ember-data/env'; +import { assert } from '@ember-data/macros'; import type { Future, Handler, NextFn, StructuredDataDocument } from '@ember-data/request'; import type Store from '@ember-data/store'; import type { StoreRequestContext, StoreRequestInfo } from '@ember-data/store/-private/cache-handler'; diff --git a/packages/legacy-compat/src/legacy-network-handler/serializer-response.ts b/packages/legacy-compat/src/legacy-network-handler/serializer-response.ts index fcab8bdeb2..b32479e2c5 100644 --- a/packages/legacy-compat/src/legacy-network-handler/serializer-response.ts +++ b/packages/legacy-compat/src/legacy-network-handler/serializer-response.ts @@ -1,6 +1,5 @@ -import { assert } from '@ember/debug'; - import { DEBUG } from '@ember-data/env'; +import { assert } from '@ember-data/macros'; import type Store from '@ember-data/store'; import type { ModelSchema } from '@ember-data/store/-types/q/ds-model'; import type { JsonApiDocument } from '@warp-drive/core-types/spec/raw'; diff --git a/packages/legacy-compat/src/legacy-network-handler/snapshot.ts b/packages/legacy-compat/src/legacy-network-handler/snapshot.ts index 79ac2bd88b..e87a6d297c 100644 --- a/packages/legacy-compat/src/legacy-network-handler/snapshot.ts +++ b/packages/legacy-compat/src/legacy-network-handler/snapshot.ts @@ -1,13 +1,12 @@ /** @module @ember-data/store */ -import { assert } from '@ember/debug'; - import { importSync } from '@embroider/macros'; import { DEBUG } from '@ember-data/env'; import type { CollectionEdge } from '@ember-data/graph/-private/edges/collection'; import type { ResourceEdge } from '@ember-data/graph/-private/edges/resource'; +import { assert } from '@ember-data/macros'; import { HAS_JSON_API_PACKAGE } from '@ember-data/packages'; import type Store from '@ember-data/store'; import type { RecordInstance } from '@ember-data/store/-types/q/record-instance'; diff --git a/packages/model/src/-private/attr.js b/packages/model/src/-private/attr.js index 62a2f27965..3489b503ee 100644 --- a/packages/model/src/-private/attr.js +++ b/packages/model/src/-private/attr.js @@ -1,7 +1,7 @@ -import { assert } from '@ember/debug'; import { computed } from '@ember/object'; import { DEBUG } from '@ember-data/env'; +import { assert } from '@ember-data/macros'; import { recordIdentifierFor } from '@ember-data/store'; import { peekCache } from '@ember-data/store/-private'; diff --git a/packages/model/src/-private/debug/assert-polymorphic-type.ts b/packages/model/src/-private/debug/assert-polymorphic-type.ts index 79fdb33247..9bf07b3445 100644 --- a/packages/model/src/-private/debug/assert-polymorphic-type.ts +++ b/packages/model/src/-private/debug/assert-polymorphic-type.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { DEBUG } from '@ember-data/env'; import type { StableRecordIdentifier } from '@warp-drive/core-types'; import type Store from '@ember-data/store'; diff --git a/packages/model/src/-private/hooks.ts b/packages/model/src/-private/hooks.ts index 0fbfed67c4..a038af5df9 100644 --- a/packages/model/src/-private/hooks.ts +++ b/packages/model/src/-private/hooks.ts @@ -1,6 +1,6 @@ import { getOwner, setOwner } from '@ember/application'; -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { setCacheFor, setRecordIdentifier, type Store, StoreMap } from '@ember-data/store/-private'; import type { StableRecordIdentifier } from '@warp-drive/core-types'; import type { Cache } from '@warp-drive/core-types/cache'; diff --git a/packages/model/src/-private/legacy-relationships-support.ts b/packages/model/src/-private/legacy-relationships-support.ts index 0e27e3f387..306c343a51 100644 --- a/packages/model/src/-private/legacy-relationships-support.ts +++ b/packages/model/src/-private/legacy-relationships-support.ts @@ -1,5 +1,3 @@ -import { assert } from '@ember/debug'; - import { importSync } from '@embroider/macros'; import { DEBUG } from '@ember-data/env'; @@ -8,6 +6,7 @@ import type { CollectionEdge } from '@ember-data/graph/-private/edges/collection import type { ResourceEdge } from '@ember-data/graph/-private/edges/resource'; import type { Graph, GraphEdge } from '@ember-data/graph/-private/graph'; import { upgradeStore } from '@ember-data/legacy-compat/-private'; +import { assert } from '@ember-data/macros'; import { HAS_JSON_API_PACKAGE } from '@ember-data/packages'; import type Store from '@ember-data/store'; import { diff --git a/packages/model/src/-private/many-array.ts b/packages/model/src/-private/many-array.ts index 0b57eb2605..c5c3486026 100644 --- a/packages/model/src/-private/many-array.ts +++ b/packages/model/src/-private/many-array.ts @@ -1,7 +1,7 @@ /** @module @ember-data/store */ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import type Store from '@ember-data/store'; import { diff --git a/packages/model/src/-private/model-methods.ts b/packages/model/src/-private/model-methods.ts index 381d3e9aba..714146c891 100644 --- a/packages/model/src/-private/model-methods.ts +++ b/packages/model/src/-private/model-methods.ts @@ -1,8 +1,7 @@ -import { assert } from '@ember/debug'; - import { importSync } from '@embroider/macros'; import { upgradeStore } from '@ember-data/legacy-compat/-private'; +import { assert } from '@ember-data/macros'; import type Store from '@ember-data/store'; import { recordIdentifierFor } from '@ember-data/store'; import { peekCache } from '@ember-data/store/-private'; diff --git a/packages/model/src/-private/notify-changes.ts b/packages/model/src/-private/notify-changes.ts index 12d95c4609..0a817b0621 100644 --- a/packages/model/src/-private/notify-changes.ts +++ b/packages/model/src/-private/notify-changes.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { cacheFor } from '@ember/object/internals'; import type Store from '@ember-data/store'; diff --git a/packages/model/src/-private/promise-belongs-to.ts b/packages/model/src/-private/promise-belongs-to.ts index 67a2673d96..c8f3b55730 100644 --- a/packages/model/src/-private/promise-belongs-to.ts +++ b/packages/model/src/-private/promise-belongs-to.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { computed } from '@ember/object'; import type PromiseProxyMixin from '@ember/object/promise-proxy-mixin'; import type ObjectProxy from '@ember/object/proxy'; diff --git a/packages/model/src/-private/promise-many-array.ts b/packages/model/src/-private/promise-many-array.ts index 439ee3ff2f..a026132b35 100644 --- a/packages/model/src/-private/promise-many-array.ts +++ b/packages/model/src/-private/promise-many-array.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { DEPRECATE_COMPUTED_CHAINS } from '@ember-data/deprecations'; import type { FindOptions } from '@ember-data/store/-types/q/store'; diff --git a/packages/model/src/-private/record-state.ts b/packages/model/src/-private/record-state.ts index 741c148903..b3690f1a53 100644 --- a/packages/model/src/-private/record-state.ts +++ b/packages/model/src/-private/record-state.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import type Store from '@ember-data/store'; import { storeFor } from '@ember-data/store'; diff --git a/packages/model/src/-private/references/has-many.ts b/packages/model/src/-private/references/has-many.ts index a8676acb12..f49d2f6870 100644 --- a/packages/model/src/-private/references/has-many.ts +++ b/packages/model/src/-private/references/has-many.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { DEBUG } from '@ember-data/env'; import type { CollectionEdge } from '@ember-data/graph/-private/edges/collection'; diff --git a/packages/model/src/migration-support.ts b/packages/model/src/migration-support.ts index 01ef20f390..a45ee93d90 100644 --- a/packages/model/src/migration-support.ts +++ b/packages/model/src/migration-support.ts @@ -1,5 +1,4 @@ -import { assert } from '@ember/debug'; - +import { assert } from '@ember-data/macros'; import { recordIdentifierFor } from '@ember-data/store'; import type { FieldSchema } from '@ember-data/store/-types/q/schema-service'; diff --git a/packages/private-build-infra/src/transforms/babel-plugin-transform-debug-macros/index.js b/packages/private-build-infra/src/transforms/babel-plugin-transform-debug-macros/index.js new file mode 100644 index 0000000000..b62a07cdad --- /dev/null +++ b/packages/private-build-infra/src/transforms/babel-plugin-transform-debug-macros/index.js @@ -0,0 +1,72 @@ +const { ImportUtil } = require('babel-import-util'); + +const Utils = new Set(['assert']); + +function buildAssertFn(t) { + return t.functionDeclaration( + t.identifier('assert'), + [t.identifier('desc'), t.identifier('test')], + t.blockStatement([ + t.ifStatement( + t.unaryExpression('!', t.identifier('test')), + t.blockStatement([ + t.throwStatement( + t.newExpression(t.identifier('Error'), [ + t.identifier('desc') + ]) + ), + ]) + ), + ]) + ); +} + +function findInsertionPoint(path) { + const program = path.parent; + let lastNode = null; + for (const node of program.body) { + if (node.type !== 'ImportDeclaration') { + return lastNode; + } + lastNode = node; + } + return lastNode; +} + +module.exports = function (babel) { + const { types: t } = babel; + + return { + name: 'ast-transform', // not required + visitor: { + ImportDeclaration(path, state) { + const importPath = path.node.source.value; + + if (importPath === '@ember-data/macros') { + const specifiers = path.get('specifiers'); + const insertLocation = findInsertionPoint(path); + + specifiers.forEach((specifier) => { + const name = specifier.node.imported.name; + if (!Utils.has(name)) { + throw new Error(`Unexpected import '${name}' imported from '@ember-data/macros'`); + } + + if (name === 'assert') { + const fn = buildAssertFn(t); + const index = path.parent.body.indexOf(insertLocation) + 1; + path.parent.body.splice(index, 0, fn); + specifier.remove(); + } + }); + + if (specifiers.length === 0) path.remove(); + } + }, + + Program(path, state) { + state.importer = new ImportUtil(t, path); + }, + }, + }; +}; diff --git a/packages/private-build-infra/virtual-packages/macros.d.ts b/packages/private-build-infra/virtual-packages/macros.d.ts new file mode 100644 index 0000000000..9196de3216 --- /dev/null +++ b/packages/private-build-infra/virtual-packages/macros.d.ts @@ -0,0 +1,24 @@ + +export function assert(message: string): never; +export function assert(message: string, condition: unknown): asserts condition; + +interface Available { + available: string; +} +interface Enabled extends Available { + enabled: string; +} +interface DeprecationOptions { + id: string; + until: string; + url?: string; + for: string; + since: Available | Enabled; +} + +// not available yet so not exported +function deprecate( + message: string, + test?: boolean, + options?: DeprecationOptions +): void; diff --git a/packages/request-utils/src/index.ts b/packages/request-utils/src/index.ts index a1242f6c55..c99c04440d 100644 --- a/packages/request-utils/src/index.ts +++ b/packages/request-utils/src/index.ts @@ -1,5 +1,6 @@ -import { assert, deprecate } from '@ember/debug'; +import { deprecate } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import type { Cache } from '@warp-drive/core-types/cache'; import type { StableDocumentIdentifier } from '@warp-drive/core-types/identifier'; import type { QueryParamsSerializationOptions, QueryParamsSource, Serializable } from '@warp-drive/core-types/params'; diff --git a/packages/rest/src/-private/builders/save-record.ts b/packages/rest/src/-private/builders/save-record.ts index 22add8af75..399e34915b 100644 --- a/packages/rest/src/-private/builders/save-record.ts +++ b/packages/rest/src/-private/builders/save-record.ts @@ -1,8 +1,8 @@ -import { assert } from '@ember/debug'; import { camelize } from '@ember/string'; import { pluralize } from 'ember-inflector'; +import { assert } from '@ember-data/macros'; import { buildBaseURL, type CreateRecordUrlOptions, diff --git a/packages/schema-record/src/-base-fields.ts b/packages/schema-record/src/-base-fields.ts index d01d7649b1..b3ad1947d7 100644 --- a/packages/schema-record/src/-base-fields.ts +++ b/packages/schema-record/src/-base-fields.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { recordIdentifierFor } from '@ember-data/store'; import type { RecordInstance } from '@ember-data/store/-types/q/record-instance'; diff --git a/packages/schema-record/src/record.ts b/packages/schema-record/src/record.ts index 00a9a44f72..91a4437a83 100644 --- a/packages/schema-record/src/record.ts +++ b/packages/schema-record/src/record.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { DEBUG } from '@ember-data/env'; import type { Future } from '@ember-data/request'; diff --git a/packages/schema-record/src/schema.ts b/packages/schema-record/src/schema.ts index 30dd320f74..1bdf0520b0 100644 --- a/packages/schema-record/src/schema.ts +++ b/packages/schema-record/src/schema.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import type { FieldSchema } from '@ember-data/store/-types/q/schema-service'; import { createCache, getValue } from '@ember-data/tracking'; diff --git a/packages/store/src/-private/cache-handler.ts b/packages/store/src/-private/cache-handler.ts index a7e1e7d3b4..4528036f07 100644 --- a/packages/store/src/-private/cache-handler.ts +++ b/packages/store/src/-private/cache-handler.ts @@ -1,8 +1,7 @@ /** * @module @ember-data/store */ -import { assert } from '@ember/debug'; - +import { assert } from '@ember-data/macros'; import type { Future, Handler, NextFn } from '@ember-data/request/-private/types'; import type { Cache } from '@warp-drive/core-types/cache'; import type { StableDocumentIdentifier } from '@warp-drive/core-types/identifier'; diff --git a/packages/store/src/-private/caches/cache-utils.ts b/packages/store/src/-private/caches/cache-utils.ts index 52647659de..935081564d 100644 --- a/packages/store/src/-private/caches/cache-utils.ts +++ b/packages/store/src/-private/caches/cache-utils.ts @@ -1,5 +1,4 @@ -import { assert } from '@ember/debug'; - +import { assert } from '@ember-data/macros'; import type { StableRecordIdentifier } from '@warp-drive/core-types/identifier'; import type { Cache } from '../../-types/q/cache'; diff --git a/packages/store/src/-private/document.ts b/packages/store/src/-private/document.ts index 1ae30fb81a..23f2efcc15 100644 --- a/packages/store/src/-private/document.ts +++ b/packages/store/src/-private/document.ts @@ -1,8 +1,7 @@ /** * @module @ember-data/store */ -import { assert } from '@ember/debug'; - +import { assert } from '@ember-data/macros'; import { defineSignal } from '@ember-data/tracking/-private'; import type { StableDocumentIdentifier } from '@warp-drive/core-types/identifier'; import type { RequestInfo } from '@warp-drive/core-types/request'; diff --git a/packages/store/src/-private/legacy-model-support/record-reference.ts b/packages/store/src/-private/legacy-model-support/record-reference.ts index 40485d4dc8..4f7ba67b50 100644 --- a/packages/store/src/-private/legacy-model-support/record-reference.ts +++ b/packages/store/src/-private/legacy-model-support/record-reference.ts @@ -1,5 +1,4 @@ -import { assert } from '@ember/debug'; - +import { assert } from '@ember-data/macros'; import { defineSignal } from '@ember-data/tracking/-private'; import type { StableRecordIdentifier } from '@warp-drive/core-types/identifier'; /** diff --git a/packages/store/src/-private/managers/cache-capabilities-manager.ts b/packages/store/src/-private/managers/cache-capabilities-manager.ts index afaa814819..6986e075c3 100644 --- a/packages/store/src/-private/managers/cache-capabilities-manager.ts +++ b/packages/store/src/-private/managers/cache-capabilities-manager.ts @@ -1,5 +1,4 @@ -import { assert } from '@ember/debug'; - +import { assert } from '@ember-data/macros'; import type { StableDocumentIdentifier, StableRecordIdentifier } from '@warp-drive/core-types/identifier'; import type { CacheCapabilitiesManager as StoreWrapper } from '../../-types/q/cache-store-wrapper'; diff --git a/packages/store/src/-private/managers/notification-manager.ts b/packages/store/src/-private/managers/notification-manager.ts index b27381c66e..b3d2965712 100644 --- a/packages/store/src/-private/managers/notification-manager.ts +++ b/packages/store/src/-private/managers/notification-manager.ts @@ -1,12 +1,12 @@ /** * @module @ember-data/store */ -import { assert } from '@ember/debug'; // eslint-disable-next-line no-restricted-imports import { _backburner } from '@ember/runloop'; import { LOG_NOTIFICATIONS } from '@ember-data/debugging'; import { DEBUG } from '@ember-data/env'; +import { assert } from '@ember-data/macros'; import type { StableDocumentIdentifier, StableRecordIdentifier } from '@warp-drive/core-types/identifier'; import { isDocumentIdentifier, isStableIdentifier } from '../caches/identifier-cache'; diff --git a/packages/store/src/-private/network/request-cache.ts b/packages/store/src/-private/network/request-cache.ts index 9a5f1954b6..ead15eb827 100644 --- a/packages/store/src/-private/network/request-cache.ts +++ b/packages/store/src/-private/network/request-cache.ts @@ -1,9 +1,8 @@ /** * @module @ember-data/store */ -import { assert } from '@ember/debug'; - import { DEBUG } from '@ember-data/env'; +import { assert } from '@ember-data/macros'; import type { StableRecordIdentifier } from '@warp-drive/core-types/identifier'; import type { FindOptions } from '../../-types/q/store'; diff --git a/packages/store/src/-private/record-arrays/identifier-array.ts b/packages/store/src/-private/record-arrays/identifier-array.ts index 58a1c25f9e..9341fe9e4c 100644 --- a/packages/store/src/-private/record-arrays/identifier-array.ts +++ b/packages/store/src/-private/record-arrays/identifier-array.ts @@ -1,8 +1,7 @@ /** @module @ember-data/store */ -import { assert } from '@ember/debug'; - +import { assert } from '@ember-data/macros'; import { compat } from '@ember-data/tracking'; import type { Signal } from '@ember-data/tracking/-private'; import { diff --git a/packages/store/src/-private/store-service.ts b/packages/store/src/-private/store-service.ts index b75203dc4e..405f4b07e1 100644 --- a/packages/store/src/-private/store-service.ts +++ b/packages/store/src/-private/store-service.ts @@ -2,11 +2,11 @@ @module @ember-data/store */ // this import location is deprecated but breaks in 4.8 and older -import { assert } from '@ember/debug'; import EmberObject from '@ember/object'; import { LOG_PAYLOADS, LOG_REQUESTS } from '@ember-data/debugging'; import { DEBUG, TESTING } from '@ember-data/env'; +import { assert } from '@ember-data/macros'; import type RequestManager from '@ember-data/request'; import type { Future } from '@ember-data/request/-private/types'; import type { Graph } from '@warp-drive/core-types/graph'; diff --git a/packages/store/src/-private/utils/construct-resource.ts b/packages/store/src/-private/utils/construct-resource.ts index f6ce3b4b4c..1bfd046257 100644 --- a/packages/store/src/-private/utils/construct-resource.ts +++ b/packages/store/src/-private/utils/construct-resource.ts @@ -1,5 +1,4 @@ -import { assert } from '@ember/debug'; - +import { assert } from '@ember-data/macros'; import type { ExistingResourceIdentifierObject, ResourceIdentifierObject } from '@warp-drive/core-types/spec/raw'; import { isStableIdentifier } from '../caches/identifier-cache'; diff --git a/packages/tracking/src/index.ts b/packages/tracking/src/index.ts index 3829c9e273..c6887dc30c 100644 --- a/packages/tracking/src/index.ts +++ b/packages/tracking/src/index.ts @@ -1,6 +1,7 @@ -import { assert } from '@ember/debug'; import { createCache, getValue } from '@glimmer/tracking/primitives/cache'; +import { assert } from '@ember-data/macros'; + export { transact, memoTransact, untracked } from './-private'; // temporary so we can remove the glimmer and ember imports elsewhere diff --git a/tests/recommended-json-api/app/utils/pagination-links.ts b/tests/recommended-json-api/app/utils/pagination-links.ts index 419db283fb..9e92865086 100644 --- a/tests/recommended-json-api/app/utils/pagination-links.ts +++ b/tests/recommended-json-api/app/utils/pagination-links.ts @@ -1,4 +1,4 @@ -import { assert } from '@ember/debug'; +import { assert } from '@ember-data/macros'; import { tracked } from '@glimmer/tracking'; type ApiMeta = {