Skip to content

Commit

Permalink
chore: Update dependencies (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
danez authored Feb 10, 2019
1 parent 7dff3f5 commit 9ea29f1
Show file tree
Hide file tree
Showing 10 changed files with 3,932 additions and 2,681 deletions.
17 changes: 10 additions & 7 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
{
"extends": "airbnb-base",
"extends": [
"eslint:recommended",
"plugin:import/errors",
"plugin:import/warnings",
"prettier"
],
"plugins": ["prettier"],
"parser": "babel-eslint",
"rules": {
"no-multi-assign": "off",
"no-param-reassign": "off",
"no-plusplus": "off",
"no-underscore-dangle": "off",
"max-len": "off"
}
"prettier/prettier": "error"
},
"env": { "node": true }
}
9 changes: 9 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"arrowParens": "always",
"bracketSpacing": true,
"printWidth": 80,
"trailingComma": "all",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
sudo: false
language: node_js
cache:
yarn: true
directories:
- "node_modules"
node_js:
- '11'
- '10'
- '8'
- '6'
install: yarn --frozen-lockfile --ignore-engines
before_script:
before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash
- export PATH="$HOME/.yarn/bin:$PATH"
install: yarn --frozen-lockfile --ignore-engines
script:
- yarn test
after_success:
Expand Down
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"clean": "rimraf lib",
"build": "babel src -d lib",
"test": "yarn run lint && cross-env BABEL_ENV=test yarn run build && nyc ava",
"lint": "eslint .",
"lint": "eslint --report-unused-disable-directives .",
"prepublish": "yarn run clean && yarn run build"
},
"files": [
Expand All @@ -28,21 +28,23 @@
"node-modules-regexp": "^1.0.0"
},
"devDependencies": {
"@babel/cli": "^7.0.0-beta.53",
"@babel/core": "^7.0.0-beta.53",
"@babel/preset-env": "^7.0.0-beta.53",
"ava": "^0.25.0",
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"ava": "^1.2.1",
"babel-core": "^7.0.0-0",
"babel-eslint": "^9.0.0-beta.2",
"babel-plugin-istanbul": "^4.1.1",
"babel-eslint": "^10.0.1",
"babel-plugin-istanbul": "^5.1.0",
"cross-env": "^5.0.5",
"cz-conventional-changelog": "^2.0.0",
"decache": "^4.1.0",
"eslint": "^5.1.0",
"eslint-config-airbnb-base": "^13.0.0",
"eslint-config-prettier": "^4.0.0",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-prettier": "^3.0.1",
"mock-require": "^3.0.2",
"nyc": "^12.0.2",
"nyc": "^13.2.0",
"prettier": "^1.16.4",
"rewire": "^4.0.1",
"rimraf": "^2.6.1",
"semantic-release": "^15.7.0"
Expand All @@ -61,7 +63,7 @@
"files": [
"test/*.js"
],
"source": [
"sources": [
"lib/**/*.js"
]
},
Expand Down
49 changes: 30 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,40 @@ import path from 'path';
import nodeModulesRegex from 'node-modules-regexp';

// Guard against poorly mocked module constructors.
const Module = module.constructor.length > 1
? module.constructor
: BuiltinModule;
const Module =
module.constructor.length > 1 ? module.constructor : BuiltinModule;

const HOOK_RETURNED_NOTHING_ERROR_MESSAGE = '[Pirates] A hook returned a non-string, or nothing at all! This is a'
+ ' violation of intergalactic law!\n'
+ '--------------------\n'
+ 'If you have no idea what this means or what Pirates is, let me explain: '
+ 'Pirates is a module that makes is easy to implement require hooks. One of'
+ ' the require hooks you\'re using uses it. One of these require hooks'
+ ' didn\'t return anything from it\'s handler, so we don\'t know what to'
+ ' do. You might want to debug this.';
const HOOK_RETURNED_NOTHING_ERROR_MESSAGE =
'[Pirates] A hook returned a non-string, or nothing at all! This is a' +
' violation of intergalactic law!\n' +
'--------------------\n' +
'If you have no idea what this means or what Pirates is, let me explain: ' +
'Pirates is a module that makes is easy to implement require hooks. One of' +
" the require hooks you're using uses it. One of these require hooks" +
" didn't return anything from it's handler, so we don't know what to" +
' do. You might want to debug this.';

function shouldCompile(filename, exts, matcher, ignoreNodeModules) {
if (typeof filename !== 'string') return false;
if (exts.indexOf(path.extname(filename)) === -1) return false;
if (typeof filename !== 'string') {
return false;
}
if (exts.indexOf(path.extname(filename)) === -1) {
return false;
}

const resolvedFilename = path.resolve(filename);

if (ignoreNodeModules && nodeModulesRegex.test(resolvedFilename)) return false;
if (matcher && typeof matcher === 'function') return !!matcher(resolvedFilename);
if (ignoreNodeModules && nodeModulesRegex.test(resolvedFilename)) {
return false;
}
if (matcher && typeof matcher === 'function') {
return !!matcher(resolvedFilename);
}

return true;
}

/**
*
* Add a require hook.
*
* @param {Function} hook - The hook. Accepts the code of the module and the filename. Required.
Expand All @@ -40,7 +47,7 @@ function shouldCompile(filename, exts, matcher, ignoreNodeModules) {
* @param {Boolean} [opts.ignoreNodeModules=true] - Auto-ignore node_modules. Independent of any matcher.
* @returns {Function} revert - Reverts the hooks.
*/
export function addHook(hook, opts = {}) { // eslint-disable-line import/prefer-default-export
export function addHook(hook, opts = {}) {
let reverted = false;
const loaders = [];
const oldLoaders = [];
Expand All @@ -54,10 +61,14 @@ export function addHook(hook, opts = {}) { // eslint-disable-line import/prefer-
const matcher = opts.matcher || null;
const ignoreNodeModules = opts.ignoreNodeModules !== false;
exts = opts.extensions || opts.exts || opts.extension || opts.ext || ['.js'];
if (!Array.isArray(exts)) exts = [exts];
if (!Array.isArray(exts)) {
exts = [exts];
}

exts.forEach((ext) => {
if (typeof ext !== 'string') throw new TypeError(`Invalid Extension: ${ext}`);
if (typeof ext !== 'string') {
throw new TypeError(`Invalid Extension: ${ext}`);
}
const oldLoader = Module._extensions[ext] || originalJSLoader;
oldLoaders[ext] = oldLoader;

Expand Down
10 changes: 5 additions & 5 deletions test/GoodNonPirateHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import test from 'ava';
import rewire from 'rewire';
import { makeNonPiratesHook, assertModule } from './helpers/utils';

const call = f => (typeof f === 'function' ? f() : undefined);
const call = (f) => (typeof f === 'function' ? f() : undefined);

test.beforeEach((t) => {
t.context = rewire('../');
});

test('non-pirates hooks', (t) => {
const reverts = [
t.context.addHook(code => code.replace('@@a', 'a! @@b')),
t.context.addHook((code) => code.replace('@@a', 'a! @@b')),
makeNonPiratesHook('@@b', 'b!'),
t.context.addHook(code => code.replace('@@c', 'c!'), {
matcher: filename => filename.indexOf('foo') === -1,
t.context.addHook((code) => code.replace('@@c', 'c!'), {
matcher: (filename) => filename.indexOf('foo') === -1,
}),
makeNonPiratesHook('@@d', 'd! @@e'),
t.context.addHook(code => code.replace('@@e', 'e!')),
t.context.addHook((code) => code.replace('@@e', 'e!')),
];

assertModule(t, 'chain-foo.js', 'in chain-foo a! b! @@c d! e!');
Expand Down
14 changes: 7 additions & 7 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import test from 'ava';
import rewire from 'rewire';
import { assertModule } from './helpers/utils';

const call = f => f();
const call = (f) => f();

test.beforeEach((t) => {
t.context = rewire('../');
});

test('basics', (t) => {
const reverts = [
t.context.addHook(code => code.replace('@@a', '<a>')),
t.context.addHook(code => code.replace('@@b', '<b>')),
t.context.addHook((code) => code.replace('@@a', '<a>')),
t.context.addHook((code) => code.replace('@@b', '<b>')),
];

assertModule(t, 'basics-foo.js', 'in basics-foo <a> <b>');
Expand All @@ -23,10 +23,10 @@ test('basics', (t) => {

test('matchers', (t) => {
const reverts = [
t.context.addHook(code => code.replace('@@a', '<a>'), {
matcher: filename => filename.indexOf('foo') === -1,
t.context.addHook((code) => code.replace('@@a', '<a>'), {
matcher: (filename) => filename.indexOf('foo') === -1,
}),
t.context.addHook(code => code.replace('@@b', '<b>')),
t.context.addHook((code) => code.replace('@@b', '<b>')),
];

assertModule(t, 'basics-foo.js', 'in basics-foo @@a <b>');
Expand All @@ -39,7 +39,7 @@ test('matcher is called only once per file', (t) => {
let timesMatcherCalled = 0;
require.extensions['.foojs'] = require.extensions['.js'];
const reverts = [
t.context.addHook(code => code, {
t.context.addHook((code) => code, {
matcher: () => {
timesMatcherCalled++;
return true;
Expand Down
26 changes: 17 additions & 9 deletions test/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ import test from 'ava';
import rewire from 'rewire';
import { assertModule } from './helpers/utils';

const call = f => f();
const call = (f) => f();

test.beforeEach((t) => {
t.context = rewire('../');
});

test('exts', (t) => {
const reverts = [
t.context.addHook(code => code.replace('@@a', 'a!')),
];
const reverts = [t.context.addHook((code) => code.replace('@@a', 'a!'))];

assertModule(t, 'extensions-main.js', 'a! @@a');

Expand All @@ -33,11 +31,21 @@ test('chain', (t) => {
require.extensions['.foojs'] = require.extensions['.js'];

const reverts = [
t.context.addHook(code => code.replace('@@a', 'a! @@b'), { exts: ['.js', '.foojs'] }),
t.context.addHook(code => code.replace('@@b', 'b! @@c'), { exts: ['.foojs'] }),
t.context.addHook(code => code.replace('@@c', 'c!'), { exts: ['.js', '.foojs'] }),
t.context.addHook(code => code.replace('@@d', 'd! @@e'), { exts: ['.js', '.foojs'] }),
t.context.addHook(code => code.replace('@@e', 'e!'), { exts: ['.js', '.foojs'] }),
t.context.addHook((code) => code.replace('@@a', 'a! @@b'), {
exts: ['.js', '.foojs'],
}),
t.context.addHook((code) => code.replace('@@b', 'b! @@c'), {
exts: ['.foojs'],
}),
t.context.addHook((code) => code.replace('@@c', 'c!'), {
exts: ['.js', '.foojs'],
}),
t.context.addHook((code) => code.replace('@@d', 'd! @@e'), {
exts: ['.js', '.foojs'],
}),
t.context.addHook((code) => code.replace('@@e', 'e!'), {
exts: ['.js', '.foojs'],
}),
() => delete require.extensions['.foojs'],
];

Expand Down
9 changes: 3 additions & 6 deletions test/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ function assertModule(t, filename, expected) {
const absFilename = path.join(__dirname, '../fixture', filename);
decache(absFilename);

// eslint-disable-next-line import/no-dynamic-require, global-require
return t.is(require(absFilename), expected);
}

Expand All @@ -15,14 +14,12 @@ function makeNonPiratesHook(macro, value) {
require.extensions['.js'] = function loader(mod, filename) {
const { _compile } = mod;
mod._compile = function newCompile(code) {
if (path.resolve(filename, '..') === __dirname) return _compile.call(mod, code, filename);
if (path.resolve(filename, '..') === __dirname)
return _compile.call(mod, code, filename);
return _compile.call(mod, code.replace(macro, value), filename);
};
oldLoader(mod, filename);
};
}

export {
assertModule,
makeNonPiratesHook,
};
export { assertModule, makeNonPiratesHook };
Loading

0 comments on commit 9ea29f1

Please sign in to comment.