Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion .c8rc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"functions": 100,
"branches": 100,
"include": [
"**/*.js"
"**/*.js",
"**/*.ts"
],
"reporter": [
"lcov",
Expand Down
3 changes: 3 additions & 0 deletions .depunusedignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
@blackflux/robo-config-plugin
@tsconfig/node14
ts-loader
ts-node
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are these all needed?

20 changes: 20 additions & 0 deletions .eslint/linted.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.c8rc.json
.circleci/config.yml
.dependabot/config.yml
.eslintrc.json
.gally.json
.mocharc.yml
.releaserc.json
.roboconfig.json
CONFDOCS.md
gardener.js
package.json
README.md
src/assert.ts
src/index.ts
test/assert.spec.ts
test/eslint.spec.ts
test/hot.js
test/index.spec.ts
tsconfig.eslint.json
tsconfig.json
112 changes: 69 additions & 43 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,49 +1,75 @@
{
"root": true,
"extends": [
"airbnb-base",
"plugin:mocha/recommended",
"plugin:markdown/recommended"
],
"rules": {
"@blackflux/rules/c8-prevent-ignore": 1,
"@blackflux/rules/kebab-case-enforce": 1,
"max-len": ["error", {"code": 120}],
"mocha/no-exclusive-tests": "error",
"prefer-destructuring": ["error", {"object": false, "array": false}],
"comma-dangle": ["error", "never"],
"indent": ["error", 2, {"SwitchCase": 1}],
"quotes": [2, "single", {"avoidEscape": true}],
"linebreak-style": [2, "unix"],
"semi": [2, "always"],
"no-unused-vars": [
1,
{"vars": "all", "args": "none", "ignoreRestSiblings": true}
],
"no-var": [1],
"no-fallthrough": [1],
"spaced-comment": [
"error",
"always",
{
"line": {"exceptions": ["-", "+"], "markers": ["=", "!"]},
"block": {
"exceptions": ["-", "+"],
"markers": ["=", "!", ":", "::"],
"balanced": true
}
}
],
"@blackflux/rules/prevent-typeof-object": 1,
"mocha/no-mocha-arrows": 0,
"mocha/no-hooks-for-single-case": 0,
"import/no-useless-path-segments": [2, {"commonjs": true}],
"import/extensions": [2, "always"],
"import/prefer-default-export": 0
},
"env": {"es6": true, "node": true, "mocha": true},
"globals": {},
"plugins": ["json", "mocha", "@blackflux/rules"],
"parser": "@babel/eslint-parser",
"parserOptions": {"requireConfigFile": false}
"parserOptions": {"requireConfigFile": false},
"overrides": [
{
"files": ["*.js", "*.cjs", "*.mjs"],
"extends": ["airbnb-base", "plugin:mocha/recommended", "plugin:markdown/recommended"],
"plugins": ["mocha", "@blackflux/rules"]
},
{
"files": ["*.ts", "*.tsx"],
"extends": ["airbnb-base", "plugin:mocha/recommended", "plugin:markdown/recommended", "airbnb-typescript/base"],
"plugins": ["mocha", "@blackflux/rules", "@typescript-eslint"],
"parser": "@typescript-eslint/parser",
"parserOptions": {"project": "./tsconfig.eslint.json"}
},
{
"files": ["*.js", "*.cjs", "*.mjs", "*.ts", "*.tsx"],
"rules": {
"@blackflux/rules/c8-prevent-ignore": 1,
"@blackflux/rules/kebab-case-enforce": 1,
"max-len": ["error", {"code": 120}],
"mocha/no-exclusive-tests": "error",
"prefer-destructuring": ["error", {"object": false, "array": false}],
"comma-dangle": ["error", "never"],
"@typescript-eslint/comma-dangle": ["error", "never"],
"indent": ["error", 2, {"SwitchCase": 1}],
"quotes": [2, "single", {"avoidEscape": true}],
"linebreak-style": [2, "unix"],
"semi": [2, "always"],
"no-unused-vars": [
1,
{"vars": "all", "args": "none", "ignoreRestSiblings": true}
],
"no-var": [1],
"no-fallthrough": [1],
"spaced-comment": [
"error",
"always",
{
"line": {"exceptions": ["-", "+"], "markers": ["=", "!"]},
"block": {
"exceptions": ["-", "+"],
"markers": ["=", "!", ":", "::"],
"balanced": true
}
}
],
"@blackflux/rules/prevent-typeof-object": 1,
"mocha/no-mocha-arrows": 0,
"mocha/no-hooks-for-single-case": 0,
"import/no-useless-path-segments": [2, {"commonjs": true}],
"import/extensions": [2, "always"],
"import/prefer-default-export": 0
}
},
{
"files": ["*.md"],
"plugins": ["markdown"],
"processor": "markdown/markdown"
},
{
"files": ["*.yaml", "*.yml"],
"plugins": ["yaml"],
"extends": ["plugin:yaml/recommended"]
},
{
"files": ["*.json"],
"plugins": ["json"]
}
]
}
3 changes: 3 additions & 0 deletions .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
timeout: 2000
node-option:
- experimental-specifier-resolution=node
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why do we need this?

- loader=ts-node/esm
2 changes: 1 addition & 1 deletion gardener.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import process from 'process';

if (process.argv[1] === fs.filename(import.meta.url)) {
gardener({
skip: []
skip: ['eslint', 'robo']
}).catch(() => process.exit(1));
}
24 changes: 19 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"main": "lib/index.js",
"scripts": {
"clean": "rm -rf lib",
"build": "cp -rf ./src ./lib && ncc build -m -o lib && mv lib/index.js lib/index.min.js && cp src/index.js lib/index.js && rm lib/package.json",
"compile": "tsc -p tsconfig.json",
"build": "cp -rf ./src ./lib && ncc build -m -o lib && mv lib/index.ts lib/index.min.ts && mv lib/index.js lib/index.min.js && cp src/index.ts lib/index.ts && rm lib/package.json",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we need build and compile? What is the difference?

"build-clean": "yarn run clean && yarn run build",
"test-simple": "c8 mocha --experimental-loader=./test/hot.js \"./test/**/*.spec.js\"",
"test-simple": "c8 mocha --experimental-loader=./test/hot.js \"./test/**/*.spec.ts\"",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this include all file extensions?

"test": "yarn run clean && yarn run gardener && yarn run test-simple",
"semantic-release": "yarn run build-clean && npx semantic-release",
"gardener": "node gardener.js",
Expand Down Expand Up @@ -48,20 +49,33 @@
"@babel/core": "7.18.10",
"@babel/eslint-parser": "7.18.9",
"@babel/register": "7.18.9",
"@blackflux/eslint-plugin-rules": "2.2.0",
"@blackflux/eslint-plugin-rules": "2.1.0",
"@blackflux/robo-config-plugin": "7.9.11",
"@tsconfig/node14": "1.0.3",
"@types/chai": "4.3.3",
"@types/mocha": "9.1.1",
"@typescript-eslint/eslint-plugin": "5.37.0",
"@typescript-eslint/parser": "5.37.0",
"@vercel/ncc": "0.34.0",
"c8": "7.12.0",
"chai": "4.3.6",
"eslint": "8.22.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-config-airbnb-typescript": "17.0.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-json": "3.1.0",
"eslint-plugin-markdown": "3.0.0",
"eslint-plugin-mocha": "10.1.0",
"js-gardener": "4.0.1",
"eslint-plugin-yaml": "0.5.0",
"glob": "8.0.3",
"js-gardener": "4.0.2",
"node-tdd": "3.4.3",
"plugin-name-to-package-name": "0.1.1",
"smart-fs": "3.0.1",
"treeify": "1.1.0"
"treeify": "1.1.0",
"ts-loader": "9.3.1",
"ts-node": "10.9.1",
"typescript": "4.7.4"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are all these deps required?

},
"licenses": [
{
Expand Down
5 changes: 0 additions & 5 deletions src/assert.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/assert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default (condition : boolean, message : string) => {
if (!condition) {
throw new Error(message);
}
};
26 changes: 19 additions & 7 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import assert from './assert.js';

const buildCtx = (opts) => {
const buildCtx = (opts : {
joined?: boolean,
spacerNoNeighbour?: string,
spacerNeighbour?: string,
keyNoNeighbour?: string,
keyNeighbour?: string,
separator?: string,
renderFn?: Function,
sortFn?: Function | null,
breakCircularWith?: string | null,
}) => {
const ctx = {
joined: true,
spacerNoNeighbour: ' ',
spacerNeighbour: '│ ',
keyNoNeighbour: '└─ ',
keyNeighbour: '├─ ',
separator: ': ',
renderFn: (node) => (['boolean', 'string', 'number'].includes(typeof node) ? node : undefined),
renderFn: (node: any) => (['boolean', 'string', 'number'].includes(typeof node) ? node : undefined),
sortFn: null,
breakCircularWith: ' (circular ref.)',
...opts
Expand All @@ -29,7 +39,7 @@ const buildCtx = (opts) => {
return ctx;
};

export default (tree, opts = {}) => {
export default (tree: { [key: string]: any }, opts = {}) => {
const ctx = buildCtx(opts);
const result = [];

Expand All @@ -38,17 +48,19 @@ export default (tree, opts = {}) => {
result.push(String(rootRendered));
}

const sort = (input) => (ctx.sortFn === null ? input.reverse() : input.sort((a, b) => ctx.sortFn(b, a)));
const sort = (input: Array<any>) => (
ctx.sortFn === null ? input.reverse() : input.sort((a: any, b: any) => ctx.sortFn!(b, a))
);

const neighbours = [];
const keys = sort(Object.keys(tree)).map((k) => [k]);
const lookup = [tree];
while (keys.length !== 0) {
const key = keys.pop();
const node = lookup[key.length - 1][key[key.length - 1]];
const key = keys.pop()!;
const node = lookup[key.length - 1]![key[key.length - 1]];
const isCircular = ctx.breakCircularWith !== null && lookup.includes(node);

neighbours[key.length - 1] = keys.length !== 0 && keys[keys.length - 1].length === key.length;
neighbours[key.length - 1] = keys.length !== 0 && keys[keys.length - 1]!.length === key.length;
const nodeRendered = ctx.renderFn(node);
result.push([
neighbours.slice(0, key.length - 1).map((n) => (n ? ctx.spacerNeighbour : ctx.spacerNoNeighbour)).join(''),
Expand Down
File renamed without changes.
57 changes: 57 additions & 0 deletions test/eslint.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// @ts-ignore
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Way too many ts-ignore here. How can we get rid of all / most?

import { expect } from 'chai';
// @ts-ignore
import fs from 'smart-fs';
// @ts-ignore
import path from 'path';
// @ts-ignore
import { ESLint } from 'eslint';
import { transform } from 'plugin-name-to-package-name';
// @ts-ignore
import glob from 'glob';
// @ts-ignore
import { describe } from 'node-tdd';

const globSync = glob.sync;

describe('Testing eslint', { timeout: 10000 }, () => {
it('Linting files', async () => {
// @ts-ignore
const dir = path.join(fs.dirname(import.meta.url), '..');
const files = globSync(
'**/*.{ts,tsx,js,cjs,mjs,md,yaml,yml,json}',
{
cwd: dir,
ignore: fs.smartRead(path.join(dir, 'tsconfig.eslint.json')).exclude,
dot: true
}
);
const config = fs.smartRead(path.join(dir, '.eslintrc.json'));
const plugins = Object.fromEntries(await Promise.all((config.plugins || [])
.map((p: string) => transform(p, 'eslint-plugin'))
.map((p: string) => import(p).then(({ default: d }) => [p, d]))));

const eslint = new ESLint({
cwd: dir,
// todo: fix
fix: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Detect cmd arg and set this to true or false here

plugins,
baseConfig: {},
// we use glob on passed in files, due to https://github.com/eslint/eslint/issues/5623
ignore: false,
reportUnusedDisableDirectives: 'error'
});
const results = await eslint.lintFiles(files);
await ESLint.outputFixes(results);
const formatter = await eslint.loadFormatter('stylish');
const resultText = formatter.format(results);

expect(resultText).to.equal('');

const result = results.every(
(e: { warningCount: number, errorCount: number }) => e.warningCount === 0 && e.errorCount === 0
);
expect(result, 'Linter Problem').to.equal(true);
expect(fs.smartWrite(path.join(dir, '.eslint/linted.txt'), files)).to.equal(false);
});
});
7 changes: 4 additions & 3 deletions test/index.spec.js → test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';
// @ts-ignore
import treeifySlow from 'treeify';
import treeify from '../src/index.js';

Expand Down Expand Up @@ -100,7 +101,7 @@ describe('Testing Treeify', () => {
9: null
}
}, {
sortFn: (a, b) => Number(b) - Number(a),
sortFn: (a: string, b: string) => Number(b) - Number(a),
breakCircularWith: null
})).to.deep.equal([
'├─ 7',
Expand All @@ -116,7 +117,7 @@ describe('Testing Treeify', () => {
});

it('Testing Circular Reference', () => {
const x = { a: null, b: {} };
const x = { a: null, b: { c: {} } };
x.b.c = x;
const r1 = treeifySlow.asTree(x).trim();
const r2 = treeify(x);
Expand All @@ -139,7 +140,7 @@ describe('Testing Treeify', () => {
9: null
}
}, {
renderFn: (node) => node
renderFn: (node: any) => node
})).to.deep.equal([
'[object Object]',
'├─ 1: [object Object]',
Expand Down
Loading