Skip to content

Commit

Permalink
Lint and tests (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash-Singh1 authored Sep 4, 2021
1 parent b61d5e7 commit 633da81
Show file tree
Hide file tree
Showing 13 changed files with 307 additions and 174 deletions.
116 changes: 10 additions & 106 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
commonjs: true,
es2021: true
},
extends: ['eslint:recommended', 'plugin:unicorn/recommended', 'prettier'],
extends: ['eslint:recommended', 'plugin:unicorn/recommended', 'plugin:jest/recommended', 'plugin:jest/style', 'prettier'],
parserOptions: {
ecmaVersion: 12
},
Expand Down Expand Up @@ -39,132 +39,60 @@ module.exports = {
rules: {
'prettier/prettier': 'error',
'accessor-pairs': 'error',
'array-bracket-newline': ['error', {multiline: true}],
'array-bracket-spacing': ['error', 'never'],
'array-callback-return': 'error',
'arrow-body-style': ['error', 'as-needed'],
'arrow-parens': ['error', 'always'],
'arrow-spacing': 'error',
'block-scoped-var': 'error',
'block-spacing': 'error',
'brace-style': ['error', '1tbs'],
'callback-return': 'error',
'camelcase': ['error', {ignoreImports: true}],
'comma-dangle': ['error', 'never'],
'comma-spacing': 'error',
'comma-style': ['error', 'last'],
'computed-property-spacing': 'error',
'consistent-this': 'error',
'curly': ['error', 'all'],
'default-case-last': 'error',
'default-param-last': 'error',
'dot-location': ['error', 'property'],
'dot-notation': ['error', {allowKeywords: true}],
'eol-last': 'error',
'dot-notation': 'error',
'eqeqeq': 'error',
'func-call-spacing': 'error',
'func-style': ['error', 'declaration', {allowArrowFunctions: true}],
'function-call-argument-newline': ['error', 'consistent'],
'generator-star-spacing': [
'error',
{
before: false,
after: true,
method: 'neither'
}
],
'global-require': 'error',
'grouped-accessor-pairs': 'error',
'guard-for-in': 'error',
'handle-callback-err': 'error',
'id-denylist': 'error',
'id-length': ['error', {exceptions: ['i']}],
'indent': ['error', 2, {SwitchCase: 1}],
'init-declarations': ['error', 'always'],
'jsx-quotes': ['error', 'prefer-single'],
'key-spacing': 'error',
'keyword-spacing': 'error',
'line-comment-position': 'error',
'linebreak-style': ['error', 'unix'],
'lines-around-directive': 'error',
'init-declarations': 'error',
'lines-between-class-members': 'error',
'max-nested-callbacks': 'error',
'max-params': 'error',
'max-statements-per-line': 'error',
'multiline-comment-style': ['error', 'separate-lines'],
'multiline-ternary': ['error', 'always-multiline'],
'new-cap': 'error',
'new-parens': 'error',
'no-array-constructor': 'error',
'no-bitwise': 'error',
'no-buffer-constructor': 'error',
'no-caller': 'error',
'no-case-declarations': 'off',
'no-catch-shadow': 'error',
'no-confusing-arrow': 'error',
'no-constant-condition': ['error', {checkLoops: false}],
'no-constructor-return': 'error',
'no-div-regex': 'error',
'no-duplicate-imports': 'error',
'no-duplicate-imports': ['error', {includeExports: true}],
'no-else-return': 'error',
'no-empty': ['error', {allowEmptyCatch: true}],
'no-eval': 'error',
'no-extend-native': 'error',
'no-extra-bind': 'error',
'no-extra-label': 'error',
'no-floating-decimal': 'error',
'no-global-assign': 'error',
'no-implicit-coercion': 'error',
'no-implied-eval': 'error',
'no-inner-declarations': 'off',
'no-iterator': 'error',
'no-label-var': 'error',
'no-labels': 'error',
'no-lone-blocks': 'error',
'no-lonely-if': 'error',
'no-loss-of-precision': 'error',
'no-mixed-requires': 'error',
'no-multi-assign': 'error',
'no-multi-spaces': 'error',
'no-multi-str': 'error',
'no-multiple-empty-lines': 'error',
'no-native-reassign': 'error',
'no-negated-condition': 'error',
'no-negated-in-lhs': 'error',
'no-nested-ternary': 'error',
'no-new-func': 'error',
'no-new-object': 'error',
'no-new-require': 'error',
'no-new-wrappers': 'error',
'no-nonoctal-decimal-escape': 'error',
'no-octal-escape': 'error',
'no-path-concat': 'error',
'no-plusplus': ['error', {allowForLoopAfterthoughts: true}],
'no-process-exit': 'error',
'no-promise-executor-return': 'error',
'no-proto': 'error',
'no-restricted-exports': 'error',
'no-restricted-globals': 'error',
'no-restricted-imports': 'error',
'no-restricted-modules': 'error',
'no-restricted-properties': 'error',
'no-restricted-syntax': 'error',
'no-return-assign': 'error',
'no-return-assign': ['error', 'always'],
'no-return-await': 'error',
'no-script-url': 'error',
'no-self-compare': 'error',
'no-sequences': 'error',
'no-spaced-func': 'error',
'no-tabs': ['error', {allowIndentationTabs: true}],
'no-template-curly-in-string': 'error',
'no-throw-literal': 'error',
'no-trailing-spaces': 'error',
'no-undef-init': 'error',
'no-undef': 'off',
'no-unneeded-ternary': 'error',
'no-unreachable-loop': 'error',
'no-unsafe-optional-chaining': 'error',
'no-unused-expressions': 'error',
'no-unused-vars': 'off',
'no-useless-backreference': 'error',
'no-useless-call': 'error',
'no-useless-computed-key': 'error',
Expand All @@ -174,45 +102,21 @@ module.exports = {
'no-useless-return': 'error',
'no-var': 'error',
'no-void': 'error',
'no-warning-comments': 'error',
'no-whitespace-before-property': 'error',
'object-curly-newline': 'error',
'object-curly-spacing': ['error', 'never'],
'object-shorthand': 'error',
'one-var': ['error', 'never'],
'one-var-declaration-per-line': 'error',
'operator-assignment': ['error', 'always'],
'operator-linebreak': ['error', null],
'padding-line-between-statements': 'error',
'prefer-arrow-callback': 'error',
'prefer-destructuring': ['error', {object: true, array: false}],
'operator-assignment': 'error',
'prefer-destructuring': ['error', {VariableDeclarator: {object: true, array: true}, AssignmentExpression: {object: true, array: false}}],
'prefer-exponentiation-operator': 'error',
'prefer-numeric-literals': 'error',
'prefer-object-spread': 'error',
'prefer-regex-literals': 'error',
'prefer-regex-literals': ['error', {disallowRedundantWrapping: true}],
'prefer-rest-params': 'error',
'prefer-spread': 'error',
'radix': 'error',
'require-atomic-updates': 'error',
'require-await': 'error',
'rest-spread-spacing': ['error', 'never'],
'semi': 'error',
'semi-spacing': 'error',
'semi-style': ['error', 'last'],
'sort-imports': 'error',
'sort-vars': 'error',
'space-before-blocks': 'error',
'space-in-parens': ['error', 'never'],
'space-infix-ops': 'error',
'space-unary-ops': 'error',
'spaced-comment': ['error', 'always'],
'spaced-comment': 'error',
'strict': ['error', 'never'],
'switch-colon-spacing': 'error',
'symbol-description': 'error',
'template-curly-spacing': ['error', 'never'],
'template-tag-spacing': 'error',
'unicode-bom': ['error', 'never'],
'yield-star-spacing': 'error',
'yoda': ['error', 'never'],

// Unicorn rules
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ dist.zip
# Cache
.eslintcache
.stylelintcache

# Code coverage
coverage/
2 changes: 1 addition & 1 deletion docs/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ window.$docsify = {
plugins: [
function (hook, vm) {
hook.beforeEach((html) => {
const editHtml = `<p align="right">:memo: <a href="${url}">Edit this Page</a></p>\n\n`;
const url =
'https://github.com/Yash-Singh1/' +
(vm.route.file.startsWith('MONKEYIDE') ? 'monkeyide' : 'epack') +
'/blob/main/' +
(/(README|CONTRIBUTING|LICENSE|ROADMAP|CODE_OF_CONDUCT|ARCHITECTURE)/.test(vm.route.file) ? '' : 'docs/') +
(vm.route.file.startsWith('MONKEYIDE') ? vm.route.file.slice(10) : vm.route.file);
const editHtml = `<p align="right">:memo: <a href="${url}">Edit this Page</a></p>\n\n`;
if (html.includes('404')) {
return html;
}
Expand Down
6 changes: 2 additions & 4 deletions ext/valid.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ function validPanelLst(panelsGiven) {
(Array.isArray(panel.styles) || typeof panel.styles === 'undefined') &&
(Array.isArray(panel.scripts) || typeof panel.scripts === 'undefined') &&
typeof panel.settings === 'object' &&
panel.settings !== null &&
((panel.settings.inject === true && (Array.isArray(panel.settings.matches) || typeof panel.settings.matches === 'string')) ||
panel.settings.inject !== true)
panel.settings !== null
);
}

// Export for tests... Implementing later
// istanbul ignore else
if (typeof module !== 'undefined') {
module.exports = validPanelLst;
}
2 changes: 2 additions & 0 deletions ext/welcome.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable no-unused-vars */

/**
* Returns the <p> tag for the welcome tag.
* @param {boolean} bool console.logs the element
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"heroku-postbuild": "",
"dev": "bash package.sh --no-zip",
"code-style": "markdownlint ./ --fix && stylelint ./www/style/*.css ./ext/*.css --fix --cache && eslint ./ --cache --fix && prettier --check ./ > /dev/null || prettier --write .",
"lint": "markdownlint ./ && stylelint ./www/style/*.css ./ext/*.css --cache && eslint ./ --cache && prettier --check ."
"lint": "markdownlint ./ && stylelint ./www/style/*.css ./ext/*.css --cache && eslint ./ --cache && prettier --check .",
"test": "jest --coverage"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -44,8 +45,10 @@
"devDependencies": {
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-jest": "^24.4.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-unicorn": "^35.0.0",
"jest": "^27.1.0",
"markdownlint-cli": "^0.28.1",
"prettier": "^2.2.1",
"stylelint": "^13.11.0",
Expand Down
3 changes: 1 addition & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ app.get('/*', (request, response) => {
}

// Determine the root from which to send the file
let root = rootDocs.has(request.path.slice(1)) || request.path.startsWith('/img/') ? './' : './docs/';
if (request.path.startsWith('/node_modules/')) {
root = './';
} else if (request.path.startsWith('/playground/')) {
root = './www/';
} else {
root = rootDocs.has(request.path.slice(1)) || request.path.startsWith('/img/') ? './' : './docs/';
}

// Prevent loads of 404 logs
Expand Down
68 changes: 68 additions & 0 deletions test/ext/valid.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const valid = require('../../ext/valid');

describe('valid function', () => {
test('succeeds on empty array', () => {
expect(valid([])).toBe(true);
});

test('false on non-array', () => {
expect(valid('abc')).toBe(false);
expect(valid(123)).toBe(false);
});

test('should have valid title', () => {
expect(valid([{settings: {}}])).toBe(false);
expect(valid([{title: '', settings: {}}])).toBe(false);
expect(valid([{title: 'abc', settings: {}}])).toBe(true);
});

test('allow multiple', () => {
expect(
valid([
{title: 'abc', settings: {}},
{title: 'bca', settings: {}},
{title: 'blah blah blah', settings: {}}
])
).toBe(true);
});

test('valid content', () => {
expect(valid([{title: 'abc', settings: {}, content: {}}])).toBe(false);
expect(valid([{title: 'abc', settings: {}, content: 156}])).toBe(false);
expect(valid([{title: 'abc', settings: {}, content: {nestedProp: 'abcd'}}])).toBe(false);
expect(valid([{title: 'abc', settings: {}, content: "{nestedProp: 'abcd'}"}])).toBe(true);
expect(valid([{title: 'abc', settings: {}}])).toBe(true);
});

test('valid styles', () => {
expect(valid([{title: 'abc', settings: {}, styles: {}}])).toBe(false);
expect(valid([{title: 'abc', settings: {}, styles: 'blah'}])).toBe(false);
expect(valid([{title: 'abc', settings: {}, styles: ['blah']}])).toBe(true);
});

test('valid scripts', () => {
expect(valid([{title: 'hey', settings: {}, scripts: {}}])).toBe(false);
expect(valid([{title: 'hey', settings: {}, scripts: 'blah'}])).toBe(false);
expect(valid([{title: 'hey', settings: {}, scripts: ['blah']}])).toBe(true);
});

test('valid settings', () => {
expect(valid([{title: 'my title', settings: 'blah'}])).toBe(false);
expect(valid([{title: 'my title', settings: 57_485}])).toBe(false);
expect(valid([{title: 'my title', settings: null}])).toBe(false);
expect(valid([{title: 'my title', settings: {}}])).toBe(true);
});

test('everything', () => {
expect(
valid([
{title: 'my title', settings: {}},
{title: 'hey', settings: {}, scripts: ['blah']},
{title: 'abc', settings: {}, styles: ['blah']},
{title: 'abc', settings: {}},
{title: 'abc', settings: {}, content: "{nestedProp: 'abcd'}"},
{title: 'abc', settings: {}}
])
).toBe(true);
});
});
Loading

0 comments on commit 633da81

Please sign in to comment.