Skip to content

Commit

Permalink
refactor(core/public): Convert to ES modules (dequelabs#2167)
Browse files Browse the repository at this point in the history
* webpack lib/public

* convert lib/public to modules

* test: fix cleanupPlugins

* test: fix failing tests in configure

* test: fix addRepoter failures

* fix runRules tests

* skip a bunch of tests which mock

* update todo note with issue link

* updates based on review
  • Loading branch information
jeeyyy committed Apr 20, 2020
1 parent e8eec73 commit 91fce8c
Show file tree
Hide file tree
Showing 18 changed files with 1,055 additions and 1,058 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"overrides": [
{
"files": [
"lib/core/public/**/*.js",
"lib/core/reporters/**/*.js",
"lib/core/utils/*.js",
"lib/commons/**/*.js"
Expand Down
14 changes: 13 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ module.exports = function(grunt) {
cwd: 'lib/core',
src: [
'**/*.js',
'!public/**/*.js',
'!reporters/**/*.js',
'!utils/**/*.js',
'!imports/index.js'
Expand All @@ -113,7 +114,12 @@ module.exports = function(grunt) {
{
expand: true,
cwd: 'tmp',
src: ['*.js', 'core/reporters/reporters.js', 'core/utils/utils.js'],
src: [
'*.js',
'core/public/public.js',
'core/reporters/reporters.js',
'core/utils/utils.js'
],
dest: 'tmp'
}
]
Expand Down Expand Up @@ -165,6 +171,12 @@ module.exports = function(grunt) {
}
},
webpack: {
corePublic: createWebpackConfig(
'lib/core/public/public.js',
'tmp/core/public',
// Due to how the Babel/concat stuff works, this cannot be called `index.js`.
'public.js'
),
coreReporters: createWebpackConfig(
'lib/core/reporters/reporters.js',
'tmp/core/reporters',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function cleanupPlugins(resolve, reject) {
function cleanup(resolve, reject) {
'use strict';

resolve = resolve || function() {};
reject = reject || axe.log;

Expand Down Expand Up @@ -50,4 +51,5 @@ function cleanupPlugins(resolve, reject) {
}
}).catch(reject);
}
axe.cleanup = cleanupPlugins;

export default cleanup;
9 changes: 5 additions & 4 deletions lib/core/public/configure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global reporters */
function configureChecksRulesAndBranding(spec) {
import { hasReporter } from './reporter';

function configure(spec) {
'use strict';
var audit;

Expand Down Expand Up @@ -38,7 +39,7 @@ function configureChecksRulesAndBranding(spec) {

if (
spec.reporter &&
(typeof spec.reporter === 'function' || reporters[spec.reporter])
(typeof spec.reporter === 'function' || hasReporter(spec.reporter))
) {
audit.reporter = spec.reporter;
}
Expand Down Expand Up @@ -81,4 +82,4 @@ function configureChecksRulesAndBranding(spec) {
}
}

axe.configure = configureChecksRulesAndBranding;
export default configure;
6 changes: 4 additions & 2 deletions lib/core/public/get-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {Array} tags Optional array of tags
* @return {Array} Array of rules
*/
axe.getRules = function(tags) {
function getRules(tags) {
'use strict';

tags = tags || [];
Expand All @@ -26,4 +26,6 @@ axe.getRules = function(tags) {
tags: matchingRule.tags
};
});
};
}

export default getRules;
16 changes: 12 additions & 4 deletions lib/core/public/load.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
/*global Audit, runRules, cleanupPlugins */
/*global Audit */
/*eslint indent: 0*/

// todo: import Audit when it has been converted to es modules

import cleanup from './cleanup';
import runRules from './run-rules';

function runCommand(data, keepalive, callback) {
'use strict';
var resolve = callback;
Expand Down Expand Up @@ -29,7 +35,7 @@ function runCommand(data, keepalive, callback) {
reject
);
case 'cleanup-plugin':
return cleanupPlugins(resolve, reject);
return cleanup(resolve, reject);
default:
// go through the registered commands
if (
Expand All @@ -47,7 +53,7 @@ function runCommand(data, keepalive, callback) {
* @param {Object} audit The "audit specification" object
* @private
*/
axe._load = function(audit) {
function load(audit) {
'use strict';

axe.utils.respondable.subscribe('axe.ping', function(
Expand All @@ -63,4 +69,6 @@ axe._load = function(audit) {
axe.utils.respondable.subscribe('axe.start', runCommand);

axe._audit = new Audit(audit);
};
}

export default load;
8 changes: 4 additions & 4 deletions lib/core/public/plugins.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/*eslint no-use-before-define:0 */
var axe = axe || {};
axe.plugins = {};

function Plugin(spec) {
'use strict';
Expand Down Expand Up @@ -41,7 +39,9 @@ Plugin.prototype.add = function(impl) {
this._registry[impl.id] = impl;
};

axe.registerPlugin = function(plugin) {
function registerPlugin(plugin) {
'use strict';
axe.plugins[plugin.id] = new Plugin(plugin);
};
}

export default registerPlugin;
24 changes: 24 additions & 0 deletions lib/core/public/public.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import cleanup from './cleanup';
import configure from './configure';
import getRules from './get-rules';
import load from './load';
import registerPlugin from './plugins';
import { hasReporter, getReporter, addReporter } from './reporter';
import reset from './reset';
import runRules from './run-rules';
import runVirtualRule from './run-virtual-rule';
import run from './run';

axe.cleanup = cleanup;
axe.configure = configure;
axe.getRules = getRules;
axe._load = load;
axe.plugins = {};
axe.registerPlugin = registerPlugin;
axe.hasReporter = hasReporter;
axe.getReporter = getReporter;
axe.addReporter = addReporter;
axe.reset = reset;
axe._runRules = runRules;
axe.runVirtualRule = runVirtualRule;
axe.run = run;
16 changes: 10 additions & 6 deletions lib/core/public/reporter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
var reporters = {};
var defaultReporter;
const reporters = {};
let defaultReporter;

axe.getReporter = function(reporter) {
export function hasReporter(reporterName) {
return reporters.hasOwnProperty(reporterName);
}

export function getReporter(reporter) {
'use strict';
if (typeof reporter === 'string' && reporters[reporter]) {
return reporters[reporter];
Expand All @@ -12,13 +16,13 @@ axe.getReporter = function(reporter) {
}

return defaultReporter;
};
}

axe.addReporter = function registerReporter(name, cb, isDefault) {
export function addReporter(name, cb, isDefault) {
'use strict';

reporters[name] = cb;
if (isDefault) {
defaultReporter = cb;
}
};
}
4 changes: 2 additions & 2 deletions lib/core/public/reset.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*global axe */
function resetConfiguration() {
function reset() {
'use strict';
var audit = axe._audit;

Expand All @@ -9,4 +9,4 @@ function resetConfiguration() {
audit.resetRulesAndChecks();
}

axe.reset = resetConfiguration;
export default reset;
2 changes: 1 addition & 1 deletion lib/core/public/run-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,4 @@ function runRules(context, options, resolve, reject) {
});
}

axe._runRules = runRules;
export default runRules;
6 changes: 4 additions & 2 deletions lib/core/public/run-virtual-rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @param {Object} options (optional) Set of options passed into rules or checks
* @return {Object} axe results for the rule run
*/
axe.runVirtualRule = function(ruleId, vNode, options = {}) {
function runVirtualRule(ruleId, vNode, options = {}) {
options.reporter = options.reporter || axe._audit.reporter || 'v1';
axe._selectorData = {};

Expand Down Expand Up @@ -47,4 +47,6 @@ axe.runVirtualRule = function(ruleId, vNode, options = {}) {
...results,
toolOptions: options
};
};
}

export default runVirtualRule;
10 changes: 7 additions & 3 deletions lib/core/public/run.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getReporter } from './reporter';

function isContext(potential) {
'use strict';
switch (true) {
Expand Down Expand Up @@ -79,7 +81,7 @@ function normalizeRunParams(context, options, callback) {
* - Results The results object / array, or undefined on error
* @return {Promise} Resolves with the axe results. Only available when natively supported
*/
axe.run = function(context, options, callback) {
function run(context, options, callback) {
'use strict';
if (!axe._audit) {
throw new Error('No audit configured');
Expand Down Expand Up @@ -136,7 +138,7 @@ axe.run = function(context, options, callback) {
}

try {
let reporter = axe.getReporter(options.reporter);
let reporter = getReporter(options.reporter);
let results = reporter(rawResults, options, respond);
if (results !== undefined) {
respond(results);
Expand All @@ -156,4 +158,6 @@ axe.run = function(context, options, callback) {
);

return p;
};
}

export default run;
Loading

0 comments on commit 91fce8c

Please sign in to comment.