Skip to content

Commit

Permalink
feat: eslint lint updates; use typescript parser for js files
Browse files Browse the repository at this point in the history
Signed-off-by: Charlike Mike Reagent <opensource@tunnckocore.com>
  • Loading branch information
tunnckoCore committed Nov 21, 2019
1 parent e619e5b commit 36e29cf
Show file tree
Hide file tree
Showing 63 changed files with 379 additions and 232 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
'use strict';

module.exports = require('@tunnckocore/eslint-config/recommended');
2 changes: 2 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
'use strict';

module.exports = require('@tunnckocore/prettier-config');
6 changes: 3 additions & 3 deletions @hela/dev/src/configs/build/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path');
const utils = require('@tunnckocore/utils');
import path from 'path';
import utils from '@tunnckocore/utils';

module.exports = (options) => {
export default (options) => {
const opts = { cwd: process.cwd(), ...options };
const { exts, alias, workspaces } = utils.createAliases(opts.cwd);
const ignores = []
Expand Down
6 changes: 3 additions & 3 deletions @hela/dev/src/configs/bundle/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path');
const utils = require('@tunnckocore/utils');
import path from 'path';
import utils from '@tunnckocore/utils';

module.exports = (options) => {
export default (options) => {
const opts = { cwd: process.cwd(), ...options };
const { exts, alias, workspaces } = utils.createAliases(opts.cwd);
const ignores = []
Expand Down
4 changes: 2 additions & 2 deletions @hela/dev/src/configs/docs/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const utils = require('@tunnckocore/utils');
import utils from '@tunnckocore/utils';

module.exports = (options) => {
export default (options) => {
const opts = { cwd: process.cwd(), ...options };
const { exts, alias, workspaces } = utils.createAliases(opts.cwd);
const ignores = []
Expand Down
11 changes: 8 additions & 3 deletions @hela/dev/src/configs/lint/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const utils = require('@tunnckocore/utils');
import path from 'path';
import utils from '@tunnckocore/utils';

module.exports = (options) => {
export default (options) => {
const opts = { cwd: process.cwd(), ...options };
const { exts, alias, workspaces } = utils.createAliases(opts.cwd);
const ignores = []
Expand All @@ -14,7 +15,11 @@ module.exports = (options) => {

// if monorepo setup
if (isMonorepo) {
testMatch = Object.values(alias).map((source) => `${source}/**/*`);
testMatch = Object.values(alias).map((source) => {
const src = source.endsWith('src') ? path.dirname(source) : source;

return `${src}/**/*`;
});
} else {
testMatch =
inputs.length > 0
Expand Down
12 changes: 6 additions & 6 deletions @hela/dev/src/configs/test/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable import/no-dynamic-require, global-require */

const fs = require('fs');
const path = require('path');
const utils = require('@tunnckocore/utils');
const isCI = require('is-ci');
const picomatch = require('picomatch');
import fs from 'fs';
import path from 'path';
import utils from '@tunnckocore/utils';
import isCI from 'is-ci';
import picomatch from 'picomatch';

// eslint-disable-next-line max-statements
module.exports = (options) => {
export default (options) => {
const opts = { cwd: process.cwd(), ...options };
const { exts, workspaces, info, alias } = utils.createAliases(opts.cwd);
const ignores = []
Expand Down
4 changes: 2 additions & 2 deletions @hela/dev/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ function createCommand(name, description) {
const configDir = path.join(__dirname, 'configs', name);
const configPath = path.join(configDir, 'config.js');

// eslint-disable-next-line import/no-dynamic-require, global-require
const createConfig = require(configDir);
// const createConfig = require(configDir);
const { default: createConfig } = await import(configDir);

const config = createConfig({ ...opts, ignores, input: inputs });
const contents = `module.exports=${JSON.stringify(config)}`;
Expand Down
9 changes: 8 additions & 1 deletion @hela/dev/test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { build, bundle, docs, lint, test as testCmd, runAll } from '../src';
import {
build,
bundle,
docs,
lint,
test as testCmd,
runAll,
} from '../src/index.js';

test('command: build', async () => {
expect(typeof build).toStrictEqual('function');
Expand Down
2 changes: 1 addition & 1 deletion @tunnckocore/create-jest-runner/test/failing.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import runJest from './support/runJest';
import runJest from './support/runJest.js';

it('Works when it has failing tests', async () => {
const resultString = await runJest('failing');
Expand Down
2 changes: 1 addition & 1 deletion @tunnckocore/create-jest-runner/test/passing.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import runJest from './support/runJest';
import runJest from './support/runJest.js';

it('Works when it has only passing tests', async () => {
const resultString = await runJest('passing');
Expand Down
2 changes: 1 addition & 1 deletion @tunnckocore/create-jest-runner/test/skipped.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import runJest from './support/runJest';
import runJest from './support/runJest.js';

it('Works when it has skipped tests', async () => {
const resultString = await runJest('skipped');
Expand Down
2 changes: 1 addition & 1 deletion @tunnckocore/create-jest-runner/test/todo.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import runJest from './support/runJest';
import runJest from './support/runJest.js';

it('Works when it has todo tests', async () => {
const resultString = await runJest('todo');
Expand Down
2 changes: 1 addition & 1 deletion @tunnckocore/execa/test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import execa, { exec, shell } from '../src';
import execa, { exec, shell } from '../src/index.js';

test('default export execa v2 and named {shell, exec}', () => {
expect(typeof execa).toBe('function');
Expand Down
2 changes: 1 addition & 1 deletion @tunnckocore/jest-runner-babel/test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import runner from '../src/runner';
import runner from '../src/runner.js';

test('todo runner babel testing', () => {
expect(typeof runner).toStrictEqual('function');
Expand Down
2 changes: 1 addition & 1 deletion @tunnckocore/jest-runner-eslint/test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import runner from '../src/runner';
import runner from '../src/runner.js';

test('todo jest-runner-eslint tests', () => {
expect(typeof runner).toStrictEqual('function');
Expand Down
2 changes: 1 addition & 1 deletion @tunnckocore/pretty-config/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolveConfig, resolveConfigPath } from './utils';
import { resolveConfig, resolveConfigPath } from './utils.js';

export { resolveConfig, resolveConfigPath };

Expand Down
2 changes: 1 addition & 1 deletion @tunnckocore/pretty-config/test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mod from '../src';
import mod from '../src/index.js';

test('todo tests for @tunnckocore/pretty-config package', async () => {
expect(typeof mod).toStrictEqual('function');
Expand Down
20 changes: 20 additions & 0 deletions @tunnckocore/utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module.exports = {
isMonorepo,
testCoverage,
coverageColor,
tsconfigResolver,
EXTENSIONS,
};

// we cannot test it because we are in monorepo where the cwd is.
Expand Down Expand Up @@ -276,3 +278,21 @@ function coverageColor(value, colors = {}) {
}
return 'green';
}

function tsconfigResolver(rootDir) {
/* istanbul ignore next */
const cwd = rootDir || process.cwd();
const { isMonorepo: isMono, workspaces } = getWorkspacesAndExtensions(cwd);
const TSCONFIG_ESLINT = path.join(cwd, 'tsconfig.eslint.json');
const TSCONFIG = path.join(cwd, 'tsconfig.json');
const project = [TSCONFIG_ESLINT, TSCONFIG].find((x) => fs.existsSync(x));

return isMono
? [project].concat(
workspaces.reduce(
(acc, ws) => acc.concat(path.join(cwd, ws, '*', 'tsconfig.json')),
[],
),
)
: project;
}
12 changes: 5 additions & 7 deletions configs/babel-preset/test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const { transformSync } = require('@babel/core');
const config = require('../src');
const config = require('../src/index.js');

process.env.SELF_TEST_TUNNCKOCORE = 'yes';

Expand All @@ -12,17 +12,15 @@ test('to throw error when typescript: false', async () => {
const opts = { typescript: false, presetEnv: false };
const inputCode = `export const bar = (foo: number, bar: number) => {};`;

try {
const fn = () =>
transformSync(inputCode, {
babelrc: false,
filename: 'fixture-foo.ts',
presets: [[path.join(path.dirname(__dirname), 'src', 'index.js'), opts]],
});
} catch (err) {
expect(err).toBeTruthy();
expect(err.name).toStrictEqual('SyntaxError');
expect(err.message).toMatch(/Unexpected token, expected/);
}

expect(fn).toThrow(SyntaxError);
expect(fn).toThrow(/Unexpected token, expected/);
});

test('to transform typescript properly', () => {
Expand Down
2 changes: 2 additions & 0 deletions configs/browserslist-config/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

module.exports = [
'defaults',
// 'current node', // disable temporarily, see https://github.com/amilajack/eslint-plugin-compat/issues/264
Expand Down
2 changes: 2 additions & 0 deletions configs/browserslist-config/latest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

module.exports = [
'last 1 Android version',
'last 1 Chrome version',
Expand Down
7 changes: 6 additions & 1 deletion configs/browserslist-config/modern.js
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
module.exports = require('.').concat(['not ie <= 11', 'not ie_mob <= 11']);
'use strict';

module.exports = require('./index.js').concat([
'not ie <= 11',
'not ie_mob <= 11',
]);
73 changes: 55 additions & 18 deletions configs/eslint-config/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict';

const path = require('path');
const { createAliases } = require('@tunnckocore/utils');

const CWD = process.cwd();
const { alias, workspaces } = createAliases(CWD);

const { createAliases, tsconfigResolver } = require('@tunnckocore/utils');
const airbnbBase = require('eslint-config-airbnb-base');

const { alias, workspaces } = createAliases();

// eslint-disable-next-line import/no-dynamic-require
const bestPractices = require(airbnbBase.extends[0]);

Expand Down Expand Up @@ -61,6 +58,10 @@ const unicornRules = {
// It is pretty common to name it `err`, and there is almost no reason to be any other.
// https://github.com/sindresorhus/eslint-plugin-unicorn/blob/master/docs/rules/catch-error-name.md
'unicorn/catch-error-name': ['error', { name: 'err' }],

// Doesn't work well in node-land. We have `.on/.off` emitters in Nodejs.
'unicorn/prefer-add-event-listener': 'off',
'unicorn/no-process-exit': 'error',
};

// Sindre's XO and mine preferences too.
Expand Down Expand Up @@ -93,7 +94,7 @@ const importRules = {
'import/no-self-import': 'error',

// Enable this sometime in the future when Node.js has ES2015 module support
// 'import/no-cycle': 'error'
// 'import/no-cycle': 'error',

'import/no-useless-path-segments': ['error', { noUselessIndex: true }],

Expand Down Expand Up @@ -201,6 +202,7 @@ const eslintCommentsRules = {
],
};

// Additional rules that are specific and overiding previous
const additionalChanges = {
'no-extend-native': 'error',
'no-use-extend-native/no-use-extend-native': 'error',
Expand Down Expand Up @@ -282,6 +284,27 @@ const additionalChanges = {

'node/shebang': 'off',
'import/prefer-default-export': 'off',

// Ensure more web-compat
// ! note that it doesn't work in CommonJS
// https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/extensions.md
'import/extensions': ['error', 'always', { ignorePackages: true }],
// ! note that it doesn't work in CommonJS
'node/file-extension-in-import': ['error', 'always'],

// ? Always use named exports. Enable?
// 'import/no-default-export': 'error',

// ? enable?
'import/exports-last': 'off',

// todo: Enable in future.
// Ensures everything is tested (all exports should be used).
// For cases when you don't want or can't test, add eslint-ignore comment!
// see: https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/no-unused-modules.md
'import/no-unused-modules': 'off',

'import/no-useless-path-segments': ['error', { noUselessIndex: false }],
};

const importResolverAliasMap = Object.keys(alias).reduce((acc, key) => {
Expand All @@ -299,24 +322,34 @@ const EXTENSIONS = [
'.js',
'.jsx',
'.js.flow',
'.md',
'.mdx',
'.json',
];

module.exports = {
parser: 'babel-eslint',
// use typescript parser, always
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 11, // ECMAScript 2020
requireConfigFile: true,
sourceType: 'module',
ecmaFeatures: {
impliedStrict: true,
jsx: true,
},
babelOptions: {
configFile: path.join(CWD, 'babe-eslint.config.js'),
},
jsx: true,
project: tsconfigResolver(),
},

// parser: 'babel-eslint',
// parserOptions: {
// ecmaVersion: 11, // ECMAScript 2020
// requireConfigFile: true,
// sourceType: 'module',
// ecmaFeatures: {
// impliedStrict: true,
// jsx: true,
// },
// babelOptions: {
// configFile: path.join(CWD, 'babe-eslint.config.js'),
// },
// },

// parser: path.join(__dirname, 'buntis-parser.js'),
// parserOptions: {
// loc: true,
Expand All @@ -332,10 +365,14 @@ module.exports = {
allowModules: Object.keys(alias),
tryExtensions: EXTENSIONS,
},
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx', '.d.ts'],
},
'import/resolver': {
node: {
paths: workspaces,
extensions: EXTENSIONS,
tryExtensions: EXTENSIONS,
moduleDirectory: ['node_modules']
.concat(workspaces)
.concat(Object.values(alias)),
Expand Down Expand Up @@ -380,11 +417,11 @@ module.exports = {
'eslint-comments',
],
rules: {
...additionalChanges,
...unicornRules,
...promiseRules,
...importRules,
...nodeRules,
...eslintCommentsRules,
...additionalChanges,
},
};
Loading

0 comments on commit 36e29cf

Please sign in to comment.