Skip to content

Commit 89a009b

Browse files
committed
Changed the prettier setup to be similar to unexpected
1 parent f372001 commit 89a009b

12 files changed

+1737
-1406
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
root = true
22

33
[*]
4+
indent_size = 2
45
end_of_line = lf
56
insert_final_newline = true
6-
indent_size = 4
77
indent_style = space
88

99
[Makefile]

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
site-build
2+
test/instrumentAst.spec.js

.eslintrc.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
module.exports = {
2-
extends: [
3-
'onelint'
4-
],
5-
env: {
6-
es6: true
7-
},
8-
parserOptions: null
1+
const config = {
2+
extends: ['pretty-standard']
93
};
4+
5+
if (process.stdin.isTTY) {
6+
// Enable plugin-prettier when running in a terminal. Allows us to have
7+
// eslint verify prettier formatting, while not being bothered by it in our
8+
// editors.
9+
config.plugins = config.plugins || [];
10+
config.plugins.push('prettier');
11+
config.rules = config.rules || {};
12+
config.rules['prettier/prettier'] = 'error';
13+
}
14+
15+
module.exports = config;

.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package.json

.prettierrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"singleQuote": true
3+
}

bootstrap-unexpected-markdown.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*global unexpected:true, expect:true*///eslint-disable-line no-unused-vars
1+
/*global unexpected:true, expect:true*/ //eslint-disable-line no-unused-vars
22
unexpected = require('unexpected').clone();
33
unexpected.output.preferredWidth = 80;
44
unexpected.use(require('./lib/unexpected-check'));

lib/addRequireHook.js

+135-128
Original file line numberDiff line numberDiff line change
@@ -10,156 +10,154 @@ const fs = require('fs');
1010
let ignoreDirectivesRelativeTo;
1111

1212
function addDefaultIgnores() {
13-
deepIgnore.add([
14-
'node_modules/unexpected*',
15-
'node_modules/array-changes',
16-
'node_modules/arraydiff-papandreou',
17-
'node_modules/array-changes-async',
18-
'node_modules/arraydiff-async',
19-
'node_modules/greedy-interval-packer',
20-
'node_modules/magicpen',
21-
'node_modules/mocha',
22-
'node_modules/jest',
23-
'node_modules/jasmine',
24-
'node_modules/chance',
25-
'node_modules/chance-generators',
26-
'node_modules/sinon'
27-
]);
28-
shallowIgnore.add([
29-
'test',
30-
'*.spec.js',
31-
'../../unexpected-check/lib/unexpected-check.js'
32-
]);
13+
deepIgnore.add([
14+
'node_modules/unexpected*',
15+
'node_modules/array-changes',
16+
'node_modules/arraydiff-papandreou',
17+
'node_modules/array-changes-async',
18+
'node_modules/arraydiff-async',
19+
'node_modules/greedy-interval-packer',
20+
'node_modules/magicpen',
21+
'node_modules/mocha',
22+
'node_modules/jest',
23+
'node_modules/jasmine',
24+
'node_modules/chance',
25+
'node_modules/chance-generators',
26+
'node_modules/sinon'
27+
]);
28+
shallowIgnore.add([
29+
'test',
30+
'*.spec.js',
31+
'../../unexpected-check/lib/unexpected-check.js'
32+
]);
3333
}
3434

3535
if (process.env.DFL_IGNORE) {
36-
// Root-relative paths will be interpreted as absolute
37-
shallowIgnore.add(process.env.DFL_IGNORE);
36+
// Root-relative paths will be interpreted as absolute
37+
shallowIgnore.add(process.env.DFL_IGNORE);
3838
} else {
39-
const closestPackageJson = pkgUp.sync();
40-
if (closestPackageJson) {
41-
try {
42-
const packageDirName = pathModule.dirname(closestPackageJson);
43-
ignoreDirectivesRelativeTo = packageDirName;
44-
45-
const dflIgnorePath = pathModule.resolve(
46-
packageDirName,
47-
'.dflignore'
48-
);
49-
// Root-relative paths will be interpreted as relative
50-
// to the .dflignore file, as per the .gitignore convention:
51-
shallowIgnore.add(fs.readFileSync(dflIgnorePath, 'utf-8'));
52-
} catch (err) {
53-
addDefaultIgnores();
54-
}
55-
} else {
56-
addDefaultIgnores();
39+
const closestPackageJson = pkgUp.sync();
40+
if (closestPackageJson) {
41+
try {
42+
const packageDirName = pathModule.dirname(closestPackageJson);
43+
ignoreDirectivesRelativeTo = packageDirName;
44+
45+
const dflIgnorePath = pathModule.resolve(packageDirName, '.dflignore');
46+
// Root-relative paths will be interpreted as relative
47+
// to the .dflignore file, as per the .gitignore convention:
48+
shallowIgnore.add(fs.readFileSync(dflIgnorePath, 'utf-8'));
49+
} catch (err) {
50+
addDefaultIgnores();
5751
}
52+
} else {
53+
addDefaultIgnores();
54+
}
5855
}
5956

6057
let locations;
6158
let prevLocation;
6259

6360
function initialize() {
64-
global.recordLocation.locations = locations = Object.create(null);
65-
global.recordProximity.proximity = Object.create(null);
66-
prevLocation = 0;
61+
global.recordLocation.locations = locations = Object.create(null);
62+
global.recordProximity.proximity = Object.create(null);
63+
prevLocation = 0;
6764
}
6865

69-
global.recordLocation = (location) => {
70-
const key = location ^ prevLocation;
71-
locations[key] = (locations[key] || 0) + 1;
72-
prevLocation = location >> 1;
66+
global.recordLocation = location => {
67+
const key = location ^ prevLocation;
68+
locations[key] = (locations[key] || 0) + 1;
69+
prevLocation = location >> 1;
7370
};
7471

7572
function evaluate(left, operator, right) {
76-
switch (operator) {
73+
switch (operator) {
7774
case '===':
78-
return left === right;
75+
return left === right;
7976
case '!==':
80-
return left !== right;
77+
return left !== right;
8178
case '<':
82-
return left < right;
79+
return left < right;
8380
case '>':
84-
return left > right;
81+
return left > right;
8582
case '<=':
86-
return left <= right;
83+
return left <= right;
8784
case '>=':
88-
return left >= right;
85+
return left >= right;
8986
case '==':
90-
return left == right; // eslint-disable-line eqeqeq
91-
}
87+
return left == right; // eslint-disable-line eqeqeq
88+
}
9289
}
9390

9491
function calculateNumberProximity(left, operator, right) {
95-
var difference = Math.abs(left - right);
96-
if (difference > 1000) {
97-
return null;
98-
}
92+
var difference = Math.abs(left - right);
93+
if (difference > 1000) {
94+
return null;
95+
}
9996

100-
switch (operator) {
97+
switch (operator) {
10198
case '!==':
102-
return Number.isInteger(left) && Number.isInteger(right) ? 1 : null;
99+
return Number.isInteger(left) && Number.isInteger(right) ? 1 : null;
103100
case '===':
104101
case '==':
105-
return Number.isInteger(left) && Number.isInteger(right)
106-
? difference
107-
: null;
102+
return Number.isInteger(left) && Number.isInteger(right)
103+
? difference
104+
: null;
108105
case '<':
109106
case '>':
110-
return difference;
107+
return difference;
111108
case '<=':
112109
case '>=':
113-
return difference + 1;
110+
return difference + 1;
114111
default:
115-
return null;
116-
}
112+
return null;
113+
}
117114
}
118115

119116
function calculateStringProximity(left, operator, right) {
120-
switch (operator) {
117+
switch (operator) {
121118
case '!==':
122-
return 1;
119+
return 1;
123120
case '===':
124121
case '==':
125-
const lengthDifference = Math.abs(left.length - right.length);
126-
const limit = 30;
127-
128-
let difference = lengthDifference;
129-
if (difference >= limit) {
130-
for (var i = 0; i < Math.min(left.length, right.length); i += 1) {
131-
if (left[i] !== right[i]) {
132-
difference++;
133-
}
134-
}
122+
const lengthDifference = Math.abs(left.length - right.length);
123+
const limit = 30;
124+
125+
let difference = lengthDifference;
126+
if (difference >= limit) {
127+
for (var i = 0; i < Math.min(left.length, right.length); i += 1) {
128+
if (left[i] !== right[i]) {
129+
difference++;
130+
}
135131
}
132+
}
136133

137-
return difference > limit ? null : difference;
134+
return difference > limit ? null : difference;
138135
default:
139-
return null;
140-
}
136+
return null;
137+
}
141138
}
142139

143140
global.recordProximity = (left, operator, right) => {
144-
let result = evaluate(left, operator, right);
145-
const leftType = typeof left;
146-
const rightType = typeof right;
141+
let result = evaluate(left, operator, right);
142+
const leftType = typeof left;
143+
const rightType = typeof right;
147144

148-
let proximity = null;
145+
let proximity = null;
149146

150-
if (!result) {
151-
if (leftType === 'number' && rightType === 'number') {
152-
proximity = calculateNumberProximity(left, operator, right);
153-
} else if (leftType === 'string' && rightType === 'string') {
154-
proximity = calculateStringProximity(left, operator, right);
155-
}
147+
if (!result) {
148+
if (leftType === 'number' && rightType === 'number') {
149+
proximity = calculateNumberProximity(left, operator, right);
150+
} else if (leftType === 'string' && rightType === 'string') {
151+
proximity = calculateStringProximity(left, operator, right);
156152
}
153+
}
157154

158-
if (proximity !== null && proximity > 0) {
159-
global.recordProximity.proximity[proximity] = (global.recordProximity.proximity[proximity] || 0) + 1;
160-
}
155+
if (proximity !== null && proximity > 0) {
156+
global.recordProximity.proximity[proximity] =
157+
(global.recordProximity.proximity[proximity] || 0) + 1;
158+
}
161159

162-
return result;
160+
return result;
163161
};
164162

165163
initialize();
@@ -169,39 +167,48 @@ global.recordLocation.magicValues = new Set();
169167

170168
var nextLocationNumber = 1;
171169
function getNextLocationNumber() {
172-
return nextLocationNumber++;
170+
return nextLocationNumber++;
173171
}
174172

175173
const oldRequireHook = require.extensions['.js'];
176-
require.extensions['.js'] = function (module, absoluteFileName) {
177-
let code;
178-
oldRequireHook(
179-
Object.create(module, {
180-
_compile: {
181-
value: _code => code = _code
182-
}
183-
}),
184-
absoluteFileName
174+
require.extensions['.js'] = function(module, absoluteFileName) {
175+
let code;
176+
oldRequireHook(
177+
Object.create(module, {
178+
_compile: {
179+
value: _code => (code = _code)
180+
}
181+
}),
182+
absoluteFileName
183+
);
184+
let fileNameToCheck;
185+
if (ignoreDirectivesRelativeTo) {
186+
fileNameToCheck = pathModule.relative(
187+
ignoreDirectivesRelativeTo,
188+
absoluteFileName
185189
);
186-
let fileNameToCheck;
187-
if (ignoreDirectivesRelativeTo) {
188-
fileNameToCheck = pathModule.relative(ignoreDirectivesRelativeTo, absoluteFileName);
189-
} else {
190-
fileNameToCheck = absoluteFileName;
191-
}
192-
193-
if (deepIgnore.ignores(fileNameToCheck) || (module.parent && module.parent._unexpectedCheckDeepIgnored)) {
194-
module._unexpectedCheckDeepIgnored = true;
195-
} else if (!shallowIgnore.ignores(fileNameToCheck)) {
196-
console.log('instrument', fileNameToCheck);
197-
const ast = esprima.parseScript(code, {
198-
source: pathModule.relative(process.cwd(), absoluteFileName)
199-
});
200-
const { instrumentedAst, magicValues } = instrumentAst(ast, getNextLocationNumber);
201-
code = escodegen.generate(instrumentedAst);
202-
for (const magicValue of magicValues) {
203-
global.recordLocation.magicValues.add(magicValue);
204-
}
190+
} else {
191+
fileNameToCheck = absoluteFileName;
192+
}
193+
194+
if (
195+
deepIgnore.ignores(fileNameToCheck) ||
196+
(module.parent && module.parent._unexpectedCheckDeepIgnored)
197+
) {
198+
module._unexpectedCheckDeepIgnored = true;
199+
} else if (!shallowIgnore.ignores(fileNameToCheck)) {
200+
console.log('instrument', fileNameToCheck);
201+
const ast = esprima.parseScript(code, {
202+
source: pathModule.relative(process.cwd(), absoluteFileName)
203+
});
204+
const { instrumentedAst, magicValues } = instrumentAst(
205+
ast,
206+
getNextLocationNumber
207+
);
208+
code = escodegen.generate(instrumentedAst);
209+
for (const magicValue of magicValues) {
210+
global.recordLocation.magicValues.add(magicValue);
205211
}
206-
module._compile(code, absoluteFileName);
212+
}
213+
module._compile(code, absoluteFileName);
207214
};

0 commit comments

Comments
 (0)