Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: assert|warn|deprecate macros #9165

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/-ember-data/addon-test-support/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/-private/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from '@ember/debug';
import { assert } from '@ember-data/macros';

type FetchFunction = (input: RequestInfo, init?: RequestInit | undefined) => Promise<Response>;

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/-private/utils/serialize-into-hash.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from '@ember/debug';
import { assert } from '@ember-data/macros';

const RBRACKET = /\[\]$/;

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
@module @ember-data/adapter/error
*/
import { assert } from '@ember/debug';
import { assert } from '@ember-data/macros';

/**
## Overview
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter/src/json-api.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/debug/addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/graph/src/-private/-edge-definition.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/graph/src/-private/graph.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/json-api/src/-private/builders/save-record.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from '@ember/debug';
import { assert } from '@ember-data/macros';

import { pluralize } from 'ember-inflector';

Expand Down
2 changes: 1 addition & 1 deletion packages/json-api/src/-private/cache.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/json-api/src/-private/serialize.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/legacy-compat/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/model/src/-private/attr.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/model/src/-private/hooks.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 1 addition & 2 deletions packages/model/src/-private/legacy-relationships-support.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { assert } from '@ember/debug';

import { importSync } from '@embroider/macros';

import { DEBUG } from '@ember-data/env';
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/model/src/-private/many-array.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions packages/model/src/-private/model-methods.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/model/src/-private/notify-changes.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/model/src/-private/promise-belongs-to.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/model/src/-private/promise-many-array.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/model/src/-private/record-state.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/model/src/-private/references/has-many.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 1 addition & 2 deletions packages/model/src/migration-support.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
},
},
};
};
24 changes: 24 additions & 0 deletions packages/private-build-infra/virtual-packages/macros.d.ts
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion packages/request-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/-private/builders/save-record.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Loading
Loading