Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { existsSync } from 'fs';
import { resolve } from 'path';
// @ts-ignore
import { rules } from '../';

const ruleNames = Object.keys(rules);
Expand Down
41 changes: 18 additions & 23 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
import { readdirSync } from 'fs';
import { basename, join } from 'path';
import { join, parse } from 'path';
import globals from './globals.json';
import * as snapshotProcessor from './processors/snapshot-processor';

// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
/* istanbul ignore next */
function interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
}
const interopRequireDefault = (obj: any): { default: any } =>
obj && obj.__esModule ? obj : { default: obj };

function importDefault(moduleName) {
return interopRequireDefault(require(moduleName)).default;
}
const importDefault = (moduleName: string) =>
// eslint-disable-next-line @typescript-eslint/no-require-imports
interopRequireDefault(require(moduleName)).default;

const rules = readdirSync(join(__dirname, 'rules'))
.filter(
rule =>
rule !== '__tests__' &&
rule !== 'util.js' &&
rule !== 'tsUtils.ts' &&
rule !== 'tsUtils.js',
)
.map(rule =>
rule.endsWith('js') ? basename(rule, '.js') : basename(rule, '.ts'),
)
const rulesDir = join(__dirname, 'rules');
const excludedFiles = ['__tests__', 'util', 'tsUtils'];

const rules = readdirSync(rulesDir)
.map(rule => parse(rule).name)
.filter(rule => !excludedFiles.includes(rule))
.reduce(
(acc, curr) =>
Object.assign(acc, { [curr]: importDefault(`./rules/${curr}`) }),
Object.assign(acc, { [curr]: importDefault(join(rulesDir, curr)) }),
{},
);
let allRules = {};
Object.keys(rules).forEach(function(key) {
allRules[`jest/${key}`] = 'error';
});

const allRules = Object.keys(rules).reduce<Record<string, string>>(
(rules, key) => ({ ...rules, [`jest/${key}`]: 'error' }),
Copy link
Collaborator Author

@G-Rath G-Rath Aug 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually surprisingly "slow" vs using an assignment, b/c everytime you're creating a new object & copying all the keys.

It's by far not a big deal, and even less so since we're doing it the once (I hope? I assume the way eslint runs it typically reuses the same node process), but it's an interesting little fact.

(That might have changed in later versions of node - iirc I last tested this on node8)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doing it once during require, yeah - probably not even measurable except in micro benchmarks. Nothing compared to the FS operations in the same file 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and yeah, object spread has gotten way quicker (although it's currently transpiled down by babel until we drop node 6)

https://twitter.com/bmeurer/status/1015105293301280768?lang=en

{},
);

// eslint-disable-next-line import/no-commonjs
module.exports = {
Expand Down