Skip to content

Commit

Permalink
Merge pull request #32 from unexpectedjs/ssimonsen/improved-prettier-…
Browse files Browse the repository at this point in the history
…setup

Changed the prettier setup to be similar to unexpected
  • Loading branch information
sunesimonsen authored Oct 31, 2018
2 parents f372001 + 89a009b commit c2dfda9
Show file tree
Hide file tree
Showing 12 changed files with 1,737 additions and 1,406 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
root = true

[*]
indent_size = 2
end_of_line = lf
insert_final_newline = true
indent_size = 4
indent_style = space

[Makefile]
Expand Down
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
site-build
test/instrumentAst.spec.js
22 changes: 14 additions & 8 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
module.exports = {
extends: [
'onelint'
],
env: {
es6: true
},
parserOptions: null
const config = {
extends: ['pretty-standard']
};

if (process.stdin.isTTY) {
// Enable plugin-prettier when running in a terminal. Allows us to have
// eslint verify prettier formatting, while not being bothered by it in our
// editors.
config.plugins = config.plugins || [];
config.plugins.push('prettier');
config.rules = config.rules || {};
config.rules['prettier/prettier'] = 'error';
}

module.exports = config;
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package.json
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
2 changes: 1 addition & 1 deletion bootstrap-unexpected-markdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*global unexpected:true, expect:true*///eslint-disable-line no-unused-vars
/*global unexpected:true, expect:true*/ //eslint-disable-line no-unused-vars
unexpected = require('unexpected').clone();
unexpected.output.preferredWidth = 80;
unexpected.use(require('./lib/unexpected-check'));
Expand Down
263 changes: 135 additions & 128 deletions lib/addRequireHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,156 +10,154 @@ const fs = require('fs');
let ignoreDirectivesRelativeTo;

function addDefaultIgnores() {
deepIgnore.add([
'node_modules/unexpected*',
'node_modules/array-changes',
'node_modules/arraydiff-papandreou',
'node_modules/array-changes-async',
'node_modules/arraydiff-async',
'node_modules/greedy-interval-packer',
'node_modules/magicpen',
'node_modules/mocha',
'node_modules/jest',
'node_modules/jasmine',
'node_modules/chance',
'node_modules/chance-generators',
'node_modules/sinon'
]);
shallowIgnore.add([
'test',
'*.spec.js',
'../../unexpected-check/lib/unexpected-check.js'
]);
deepIgnore.add([
'node_modules/unexpected*',
'node_modules/array-changes',
'node_modules/arraydiff-papandreou',
'node_modules/array-changes-async',
'node_modules/arraydiff-async',
'node_modules/greedy-interval-packer',
'node_modules/magicpen',
'node_modules/mocha',
'node_modules/jest',
'node_modules/jasmine',
'node_modules/chance',
'node_modules/chance-generators',
'node_modules/sinon'
]);
shallowIgnore.add([
'test',
'*.spec.js',
'../../unexpected-check/lib/unexpected-check.js'
]);
}

if (process.env.DFL_IGNORE) {
// Root-relative paths will be interpreted as absolute
shallowIgnore.add(process.env.DFL_IGNORE);
// Root-relative paths will be interpreted as absolute
shallowIgnore.add(process.env.DFL_IGNORE);
} else {
const closestPackageJson = pkgUp.sync();
if (closestPackageJson) {
try {
const packageDirName = pathModule.dirname(closestPackageJson);
ignoreDirectivesRelativeTo = packageDirName;

const dflIgnorePath = pathModule.resolve(
packageDirName,
'.dflignore'
);
// Root-relative paths will be interpreted as relative
// to the .dflignore file, as per the .gitignore convention:
shallowIgnore.add(fs.readFileSync(dflIgnorePath, 'utf-8'));
} catch (err) {
addDefaultIgnores();
}
} else {
addDefaultIgnores();
const closestPackageJson = pkgUp.sync();
if (closestPackageJson) {
try {
const packageDirName = pathModule.dirname(closestPackageJson);
ignoreDirectivesRelativeTo = packageDirName;

const dflIgnorePath = pathModule.resolve(packageDirName, '.dflignore');
// Root-relative paths will be interpreted as relative
// to the .dflignore file, as per the .gitignore convention:
shallowIgnore.add(fs.readFileSync(dflIgnorePath, 'utf-8'));
} catch (err) {
addDefaultIgnores();
}
} else {
addDefaultIgnores();
}
}

let locations;
let prevLocation;

function initialize() {
global.recordLocation.locations = locations = Object.create(null);
global.recordProximity.proximity = Object.create(null);
prevLocation = 0;
global.recordLocation.locations = locations = Object.create(null);
global.recordProximity.proximity = Object.create(null);
prevLocation = 0;
}

global.recordLocation = (location) => {
const key = location ^ prevLocation;
locations[key] = (locations[key] || 0) + 1;
prevLocation = location >> 1;
global.recordLocation = location => {
const key = location ^ prevLocation;
locations[key] = (locations[key] || 0) + 1;
prevLocation = location >> 1;
};

function evaluate(left, operator, right) {
switch (operator) {
switch (operator) {
case '===':
return left === right;
return left === right;
case '!==':
return left !== right;
return left !== right;
case '<':
return left < right;
return left < right;
case '>':
return left > right;
return left > right;
case '<=':
return left <= right;
return left <= right;
case '>=':
return left >= right;
return left >= right;
case '==':
return left == right; // eslint-disable-line eqeqeq
}
return left == right; // eslint-disable-line eqeqeq
}
}

function calculateNumberProximity(left, operator, right) {
var difference = Math.abs(left - right);
if (difference > 1000) {
return null;
}
var difference = Math.abs(left - right);
if (difference > 1000) {
return null;
}

switch (operator) {
switch (operator) {
case '!==':
return Number.isInteger(left) && Number.isInteger(right) ? 1 : null;
return Number.isInteger(left) && Number.isInteger(right) ? 1 : null;
case '===':
case '==':
return Number.isInteger(left) && Number.isInteger(right)
? difference
: null;
return Number.isInteger(left) && Number.isInteger(right)
? difference
: null;
case '<':
case '>':
return difference;
return difference;
case '<=':
case '>=':
return difference + 1;
return difference + 1;
default:
return null;
}
return null;
}
}

function calculateStringProximity(left, operator, right) {
switch (operator) {
switch (operator) {
case '!==':
return 1;
return 1;
case '===':
case '==':
const lengthDifference = Math.abs(left.length - right.length);
const limit = 30;

let difference = lengthDifference;
if (difference >= limit) {
for (var i = 0; i < Math.min(left.length, right.length); i += 1) {
if (left[i] !== right[i]) {
difference++;
}
}
const lengthDifference = Math.abs(left.length - right.length);
const limit = 30;

let difference = lengthDifference;
if (difference >= limit) {
for (var i = 0; i < Math.min(left.length, right.length); i += 1) {
if (left[i] !== right[i]) {
difference++;
}
}
}

return difference > limit ? null : difference;
return difference > limit ? null : difference;
default:
return null;
}
return null;
}
}

global.recordProximity = (left, operator, right) => {
let result = evaluate(left, operator, right);
const leftType = typeof left;
const rightType = typeof right;
let result = evaluate(left, operator, right);
const leftType = typeof left;
const rightType = typeof right;

let proximity = null;
let proximity = null;

if (!result) {
if (leftType === 'number' && rightType === 'number') {
proximity = calculateNumberProximity(left, operator, right);
} else if (leftType === 'string' && rightType === 'string') {
proximity = calculateStringProximity(left, operator, right);
}
if (!result) {
if (leftType === 'number' && rightType === 'number') {
proximity = calculateNumberProximity(left, operator, right);
} else if (leftType === 'string' && rightType === 'string') {
proximity = calculateStringProximity(left, operator, right);
}
}

if (proximity !== null && proximity > 0) {
global.recordProximity.proximity[proximity] = (global.recordProximity.proximity[proximity] || 0) + 1;
}
if (proximity !== null && proximity > 0) {
global.recordProximity.proximity[proximity] =
(global.recordProximity.proximity[proximity] || 0) + 1;
}

return result;
return result;
};

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

var nextLocationNumber = 1;
function getNextLocationNumber() {
return nextLocationNumber++;
return nextLocationNumber++;
}

const oldRequireHook = require.extensions['.js'];
require.extensions['.js'] = function (module, absoluteFileName) {
let code;
oldRequireHook(
Object.create(module, {
_compile: {
value: _code => code = _code
}
}),
absoluteFileName
require.extensions['.js'] = function(module, absoluteFileName) {
let code;
oldRequireHook(
Object.create(module, {
_compile: {
value: _code => (code = _code)
}
}),
absoluteFileName
);
let fileNameToCheck;
if (ignoreDirectivesRelativeTo) {
fileNameToCheck = pathModule.relative(
ignoreDirectivesRelativeTo,
absoluteFileName
);
let fileNameToCheck;
if (ignoreDirectivesRelativeTo) {
fileNameToCheck = pathModule.relative(ignoreDirectivesRelativeTo, absoluteFileName);
} else {
fileNameToCheck = absoluteFileName;
}

if (deepIgnore.ignores(fileNameToCheck) || (module.parent && module.parent._unexpectedCheckDeepIgnored)) {
module._unexpectedCheckDeepIgnored = true;
} else if (!shallowIgnore.ignores(fileNameToCheck)) {
console.log('instrument', fileNameToCheck);
const ast = esprima.parseScript(code, {
source: pathModule.relative(process.cwd(), absoluteFileName)
});
const { instrumentedAst, magicValues } = instrumentAst(ast, getNextLocationNumber);
code = escodegen.generate(instrumentedAst);
for (const magicValue of magicValues) {
global.recordLocation.magicValues.add(magicValue);
}
} else {
fileNameToCheck = absoluteFileName;
}

if (
deepIgnore.ignores(fileNameToCheck) ||
(module.parent && module.parent._unexpectedCheckDeepIgnored)
) {
module._unexpectedCheckDeepIgnored = true;
} else if (!shallowIgnore.ignores(fileNameToCheck)) {
console.log('instrument', fileNameToCheck);
const ast = esprima.parseScript(code, {
source: pathModule.relative(process.cwd(), absoluteFileName)
});
const { instrumentedAst, magicValues } = instrumentAst(
ast,
getNextLocationNumber
);
code = escodegen.generate(instrumentedAst);
for (const magicValue of magicValues) {
global.recordLocation.magicValues.add(magicValue);
}
module._compile(code, absoluteFileName);
}
module._compile(code, absoluteFileName);
};
Loading

0 comments on commit c2dfda9

Please sign in to comment.