Skip to content

Commit 87b2cfa

Browse files
author
Mat Brown
committed
Move async loading of validations to saga
For reasons I’m no longer totally clear on, we asynchronously loaded each linter library discretely. Instead, just load the entire validations module when needed, substantially reducing the footprint of asynchronous loading, and also allowing the validations module to be tested without needing to do async loading in a test environment (which anyway seems weird for unit tests).
1 parent 7bc726c commit 87b2cfa

File tree

11 files changed

+40
-61
lines changed

11 files changed

+40
-61
lines changed

src/sagas/errors.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@ import {
1010
} from 'redux-saga/effects';
1111
import Analyzer from '../analyzers';
1212
import {getCurrentProject} from '../selectors';
13-
import validations from '../validations';
1413
import {validatedSource} from '../actions/errors';
14+
import retryingFailedImports from '../util/retryingFailedImports';
15+
16+
export async function importValidations() {
17+
return retryingFailedImports(
18+
() => import(
19+
/* webpackChunkName: 'mainAsync' */
20+
'../validations',
21+
),
22+
);
23+
}
1524

1625
export function* toggleLibrary(tasks) {
1726
yield call(validateCurrentProject, tasks);
@@ -49,6 +58,7 @@ export function* validateSource(
4958
if (tasks.has(language)) {
5059
yield cancel(tasks.get(language));
5160
}
61+
const validations = yield call(importValidations);
5262
const task = yield fork(validations[language], source, projectAttributes);
5363
tasks.set(language, task);
5464
const errors = yield join(task);

src/validations/css/css.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import css from 'css';
12
import Validator from '../Validator';
2-
import retryingFailedImports from '../../util/retryingFailedImports';
33

44
const errorMap = {
55
'missing \'{\'': () => ({reason: 'missing-opening-curly'}),
@@ -21,10 +21,6 @@ class CssValidator extends Validator {
2121
}
2222

2323
async _getRawErrors() {
24-
const css = await retryingFailedImports(() => import(
25-
/* webpackChunkName: 'mainAsync' */
26-
'css',
27-
));
2824
return css.parse(this._source, {silent: true}).stylesheet.parsingErrors;
2925
}
3026

src/validations/css/prettycss.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import prettyCSS from 'PrettyCSS';
12
import trim from 'lodash/trim';
23
import endsWith from 'lodash/endsWith';
34
import Validator from '../Validator';
4-
import retryingFailedImports from '../../util/retryingFailedImports';
55

66
const RADIAL_GRADIENT_EXPR =
77
/^(?:(?:-(?:ms|moz|o|webkit)-)?radial-gradient|-webkit-gradient)/;
@@ -133,10 +133,6 @@ class PrettyCssValidator extends Validator {
133133
}
134134

135135
async _getRawErrors() {
136-
const prettyCSS = await retryingFailedImports(() => import(
137-
/* webpackChunkName: 'mainAsync' */
138-
'PrettyCSS',
139-
));
140136
try {
141137
const result = prettyCSS.parse(this._source);
142138
return result.getProblems();

src/validations/css/stylelint.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import stylelint from '../../util/minimalStylelint';
12
import Validator from '../Validator';
2-
import retryingFailedImports from '../../util/retryingFailedImports';
33

44
const errorMap = {
55
'syntaxError/Unclosed block': () => ({
@@ -21,10 +21,6 @@ class StyleLintValidator extends Validator {
2121
}
2222

2323
async _getRawErrors() {
24-
const {'default': stylelint} = await retryingFailedImports(() => import(
25-
/* webpackChunkName: 'mainAsync' */
26-
'../../util/minimalStylelint',
27-
));
2824
let result;
2925
try {
3026
result = await stylelint(this._source);

src/validations/html/htmlInspector.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import HTMLInspector from 'html-inspector';
12
import last from 'lodash/last';
23
import isNull from 'lodash/isNull';
34
import trim from 'lodash/trim';
45
import {localizedArrayToSentence} from '../../util/arrayToSentence';
5-
import retryingFailedImports from '../../util/retryingFailedImports';
66
import Validator from '../Validator';
77

88
const specialCases = {
@@ -82,6 +82,11 @@ function noListsWithTextChildrenValidator(listener, reporter) {
8282
});
8383
}
8484

85+
HTMLInspector.rules.add(
86+
'validate-list-children',
87+
noListsWithTextChildrenValidator,
88+
);
89+
8590
class HtmlInspectorValidator extends Validator {
8691
constructor(source) {
8792
super(source, 'html', errorMap);
@@ -93,16 +98,6 @@ class HtmlInspectorValidator extends Validator {
9398
return Promise.resolve([]);
9499
}
95100

96-
const HTMLInspector = await retryingFailedImports(() => import(
97-
/* webpackChunkName: 'mainAsync' */
98-
'html-inspector',
99-
));
100-
101-
HTMLInspector.rules.add(
102-
'validate-list-children',
103-
noListsWithTextChildrenValidator,
104-
);
105-
106101
return new Promise((resolve) => {
107102
HTMLInspector.inspect({
108103
domRoot: this._doc.documentElement,

src/validations/html/htmllint.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import clone from 'lodash/clone';
22
import defaults from 'lodash/defaults';
3+
import {Linter, rules} from 'htmllint';
34
import reduce from 'lodash/reduce';
45
import Validator from '../Validator';
5-
import retryingFailedImports from '../../util/retryingFailedImports';
66

77
const errorMap = {
88
E001: (error) => {
@@ -135,25 +135,19 @@ const htmlLintOptions = {
135135
'title-no-dup': true,
136136
};
137137

138+
const linter = new Linter(rules);
139+
const options = reduce(
140+
linter.rules.options,
141+
(acc, {name}) => defaults(acc, {[name]: false}),
142+
clone(htmlLintOptions),
143+
);
144+
138145
class HtmllintValidator extends Validator {
139146
constructor(source) {
140147
super(source, 'html', errorMap);
141148
}
142149

143150
async _getRawErrors() {
144-
const {Linter, rules} = await retryingFailedImports(
145-
() => import(
146-
/* webpackChunkName: 'mainAsync' */
147-
'htmllint',
148-
),
149-
);
150-
const linter = new Linter(rules);
151-
const options = reduce(
152-
linter.rules.options,
153-
(acc, {name}) => defaults(acc, {[name]: false}),
154-
clone(htmlLintOptions),
155-
);
156-
157151
try {
158152
const results = await linter.lint(this._source, options);
159153
return results;

src/validations/html/slowparse.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import Slowparse from '../../util/slowparse';
12
import Validator from '../Validator';
2-
import retryingFailedImports from '../../util/retryingFailedImports';
33

44
const errorMap = {
55
ATTRIBUTE_IN_CLOSING_TAG: error => ({
@@ -125,10 +125,6 @@ class SlowparseValidator extends Validator {
125125
}
126126

127127
async _getRawErrors() {
128-
const {'default': Slowparse} = await retryingFailedImports(() => import(
129-
/* webpackChunkName: 'mainAsync' */
130-
'../../util/slowparse',
131-
));
132128
let error;
133129
try {
134130
({error} = Slowparse.HTML(document, this._source, {errorDetectors}));

src/validations/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ import html from './html';
22
import css from './css';
33
import javascript from './javascript';
44

5-
export default {html, css, javascript};
5+
export {html, css, javascript};

src/validations/javascript/esprima.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import esprima from 'esprima';
12
import find from 'lodash/find';
23
import inRange from 'lodash/inRange';
34
import Validator from '../Validator';
4-
import retryingFailedImports from '../../util/retryingFailedImports';
55

66
const UNEXPECTED_TOKEN_EXPR = /^Unexpected token (.+)$/;
77

@@ -79,10 +79,6 @@ class EsprimaValidator extends Validator {
7979
}
8080

8181
async _getRawErrors() {
82-
const esprima = await retryingFailedImports(() => import(
83-
/* webpackChunkName: 'mainAsync' */
84-
'esprima',
85-
));
8682
try {
8783
esprima.parse(this._source);
8884
} catch (error) {

src/validations/javascript/jshint.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import compact from 'lodash/compact';
55
import defaults from 'lodash/defaults';
66
import find from 'lodash/find';
77
import includes from 'lodash/includes';
8+
import {JSHINT as jshint} from 'jshint';
89
import libraries from '../../config/libraries';
9-
import retryingFailedImports from '../../util/retryingFailedImports';
1010
import Validator from '../Validator';
1111

1212
const jshintrc = {
@@ -171,10 +171,6 @@ class JsHintValidator extends Validator {
171171
}
172172

173173
async _getRawErrors() {
174-
const {JSHINT: jshint} = await retryingFailedImports(() => import(
175-
/* webpackChunkName: 'mainAsync' */
176-
'jshint',
177-
));
178174
try {
179175
jshint(this._source, this._jshintOptions);
180176
} catch (e) {

0 commit comments

Comments
 (0)