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

Introduce setup step #117

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
126 changes: 126 additions & 0 deletions addon/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { registerDeprecationHandler } from '@ember/debug';

const LOG_LIMIT = 100;

let hasSetup = false;

export function setup(scope) {
if (hasSetup) {
throw new Error(
'setup of ember-cli-deprecation-workflow must only be called once'
);
}
hasSetup = true;

scope.deprecationWorkflow = scope.deprecationWorkflow || {};
scope.deprecationWorkflow.deprecationLog = {
messages: {},
};
scope.deprecationWorkflow.logCounts = {};

function detectWorkflow(config, message, options) {
if (!config || !config.workflow) {
return;
}

let i, workflow, matcher, idMatcher;
for (i = 0; i < config.workflow.length; i++) {
workflow = config.workflow[i];
matcher = workflow.matchMessage;
idMatcher = workflow.matchId;

if (
typeof idMatcher === 'string' &&
options &&
idMatcher === options.id
) {
return workflow;
} else if (typeof matcher === 'string' && matcher === message) {
return workflow;
} else if (matcher instanceof RegExp && matcher.exec(message)) {
return workflow;
}
}
}

registerDeprecationHandler(function handleDeprecationWorkflow(
message,
options,
next
) {
let config = scope.deprecationWorkflow.config || {};

let matchingWorkflow = detectWorkflow(config, message, options);
if (!matchingWorkflow) {
if (config && config.throwOnUnhandled) {
throw new Error(message);
} else {
next(message, options);
}
} else {
switch (matchingWorkflow.handler) {
case 'silence':
// no-op
break;
case 'log': {
let key = (options && options.id) || message;
let count = scope.deprecationWorkflow.logCounts[key] || 0;
scope.deprecationWorkflow.logCounts[key] = count + 1;

if (count <= LOG_LIMIT) {
console.warn('DEPRECATION: ' + message);
if (count === LOG_LIMIT) {
console.warn(
'To avoid console overflow, this deprecation will not be logged any more in this run.'
);
}
}

break;
}
case 'throw':
throw new Error(message);
default:
next(message, options);
break;
}
}
});

registerDeprecationHandler(function deprecationCollector(
message,
options,
next
) {
let key = (options && options.id) || message;
let matchKey = options && key === options.id ? 'matchId' : 'matchMessage';

scope.deprecationWorkflow.deprecationLog.messages[key] =
' { handler: "silence", ' +
matchKey +
': ' +
JSON.stringify(key) +
' }';
next(message, options);
});

let preamble = [
'self.deprecationWorkflow = self.deprecationWorkflow || {};',
'self.deprecationWorkflow.config = {\n workflow: [\n',
].join('\n');

let postamble = [' ]\n};'].join('\n');

scope.deprecationWorkflow.flushDeprecations = function flushDeprecations() {
let messages = scope.deprecationWorkflow.deprecationLog.messages;
let logs = [];

for (let message in messages) {
logs.push(messages[message]);
}

let deprecations = logs.join(',\n') + '\n';

return preamble + deprecations + postamble;
};
}
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ module.exports = {
app.import(
'vendor/ember-cli-deprecation-workflow/deprecation-workflow.js'
);
app.import('vendor/ember-cli-deprecation-workflow/main.js');
}
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"broccoli-funnel": "^3.0.3",
"broccoli-merge-trees": "^4.2.0",
"broccoli-plugin": "^4.0.5",
"ember-cli-babel": "^7.26.6",
"ember-cli-htmlbars": "^5.3.2"
},
"devDependencies": {
Expand All @@ -39,7 +40,6 @@
"broccoli-asset-rev": "^3.0.0",
"ember-auto-import": "^1.10.1",
"ember-cli": "~3.25.0",
"ember-cli-babel": "^7.23.1",
"ember-cli-dependency-checker": "^3.2.0",
"ember-cli-inject-live-reload": "^2.0.2",
"ember-cli-sri": "^2.1.1",
Expand Down
3 changes: 3 additions & 0 deletions tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import Application from 'dummy/app';
import config from 'dummy/config/environment';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';
import { setup as setupDeprecationWorkflow } from 'ember-cli-deprecation-workflow';

setApplication(Application.create(config.APP));

setupDeprecationWorkflow(self);

start();
100 changes: 0 additions & 100 deletions vendor/ember-cli-deprecation-workflow/main.js

This file was deleted.

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4995,7 +4995,7 @@ ember-cli-babel@^6.0.0-beta.4, ember-cli-babel@^6.8.1:
ember-cli-version-checker "^2.1.2"
semver "^5.5.0"

ember-cli-babel@^7.0.0, ember-cli-babel@^7.11.0, ember-cli-babel@^7.12.0, ember-cli-babel@^7.13.0, ember-cli-babel@^7.20.5, ember-cli-babel@^7.22.1, ember-cli-babel@^7.23.0, ember-cli-babel@^7.23.1, ember-cli-babel@^7.26.2, ember-cli-babel@^7.7.3:
ember-cli-babel@^7.0.0, ember-cli-babel@^7.11.0, ember-cli-babel@^7.12.0, ember-cli-babel@^7.13.0, ember-cli-babel@^7.20.5, ember-cli-babel@^7.22.1, ember-cli-babel@^7.23.0, ember-cli-babel@^7.23.1, ember-cli-babel@^7.26.2, ember-cli-babel@^7.26.6, ember-cli-babel@^7.7.3:
version "7.26.6"
resolved "https://registry.yarnpkg.com/ember-cli-babel/-/ember-cli-babel-7.26.6.tgz#322fbbd3baad9dd99e3276ff05bc6faef5e54b39"
integrity sha512-040svtfj2RC35j/WMwdWJFusZaXmNoytLAMyBDGLMSlRvznudTxZjGlPV6UupmtTBApy58cEF8Fq4a+COWoEmQ==
Expand Down