How to do a build/compile-time check of API usage

Is it possible to a build/compile-time check for usage of a particular API?

My use case is showing a deprecation message for Ember CLI Mirage. We currently expose faker as a named export from ember-cli-mirage:

// host apps can do this:
import { faker } from 'ember-cli-mirage';

I’m looking to deprecate this behavior by removing faker from Mirage entirely (and providing instructions on how host apps can install & use faker themselves).

I’m trying to figure out the best way to do this. I started by trying to get a deprecation message printed at import-time… but couldn’t figure out how to do that, as faker is a named export from ember-cli-mirage/addon/index.js.

My next thought was adding some build-time code that scans the host app for any usage of

import { faker } from 'ember-cli-mirage'

(or some variant thereof) and showing the deprecation message in the terminal. But I’m not sure if this is a good approach or how I would go about doing that.

Figured I’d post this up here before I go any further, in case anyone has some ideas here! Any help much appreciated.

I think the easiest thing would be to deprecate every usage of faker. You could do that by either using a proxy (to intercept each property access on your returned faker object) or by looping over all properties and wrapping them (if you didn’t want to use a proxy).

If you still want to go the build time route, you’d need to:

  • author a babel plugin
  • have the plugin check for imports of faker from ember-cli-mirage
  • issue a deprecation

I think (if I were you) I’d also provide a nice codemod that folks could run to fix all the deprecations (it seems like its basically migrating from import { faker } from 'ember-cli-mirage to import faker from 'faker')…


Believe we have this thanks to the wonderful @CaseyWatts:

Could this code be used to help with the build-time check? Happy to do the run-time one as well. I guess it would just miss imports of faker that were never executed.

Since (AFAICT) faker has no side effects, any imports that are unused would still be “fine” (perhaps a little silly, but still “fine”). It would be no different from:

import { adsfasdfasdfasdfasdfasdf } from 'ember-cli-mirage';

Which shouldn’t fail, but obviously isn’t ideal…

ah. Gotcha. Yeah a little confusing, and ideally could just show em a build-time thing.

If I go the babel plugin route, would users need to run a command? Could I register the plugin so it automatically runs? If so, would that impose a large cost on the app’s build time? (I’d be able to remove it in 1.0…)