From c18a23640c00f32fca39112381b5cabdaa6a9a55 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 27 Apr 2023 11:40:11 -0700 Subject: [PATCH] [eslint] cleanup --- .eslintrc | 18 +++++++ .eslintrc.json | 130 ---------------------------------------------- README.md | 14 ++--- index.js | 44 ++++++++-------- package.json | 138 +++++++++++++++++++++++++------------------------ test.js | 71 ------------------------- test/index.js | 71 +++++++++++++++++++++++++ 7 files changed, 190 insertions(+), 296 deletions(-) create mode 100644 .eslintrc delete mode 100644 .eslintrc.json delete mode 100644 test.js create mode 100644 test/index.js diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..cdf73a2 --- /dev/null +++ b/.eslintrc @@ -0,0 +1,18 @@ +{ + "root": true, + + "extends": "@ljharb/eslint-config/node/6", + + "overrides": [ + { + "files": "test/**/*.js", + "env": { + "mocha": true, + }, + "rules": { + "max-lines-per-function": "warn", + "max-nested-callbacks": "warn", + }, + }, + ], +} diff --git a/.eslintrc.json b/.eslintrc.json deleted file mode 100644 index 24b8984..0000000 --- a/.eslintrc.json +++ /dev/null @@ -1,130 +0,0 @@ -{ - "extends": [ - "eslint:recommended" - ], - - "env": { - "browser": false, - "es6": true, - "node": true, - "mocha": true - }, - - "parserOptions":{ - "ecmaVersion": 9, - "sourceType": "module", - "ecmaFeatures": { - "modules": true, - "experimentalObjectRestSpread": true - } - }, - - "globals": { - "document": false, - "navigator": false, - "window": false - }, - - "rules": { - "accessor-pairs": 2, - "arrow-spacing": [2, { "before": true, "after": true }], - "block-spacing": [2, "always"], - "brace-style": [2, "1tbs", { "allowSingleLine": true }], - "comma-dangle": [2, "never"], - "comma-spacing": [2, { "before": false, "after": true }], - "comma-style": [2, "last"], - "constructor-super": 2, - "curly": [2, "multi-line"], - "dot-location": [2, "property"], - "eol-last": 2, - "eqeqeq": [2, "allow-null"], - "generator-star-spacing": [2, { "before": true, "after": true }], - "handle-callback-err": [2, "^(err|error)$" ], - "indent": [2, 2, { "SwitchCase": 1 }], - "key-spacing": [2, { "beforeColon": false, "afterColon": true }], - "keyword-spacing": [2, { "before": true, "after": true }], - "new-cap": [2, { "newIsCap": true, "capIsNew": false }], - "new-parens": 2, - "no-array-constructor": 2, - "no-caller": 2, - "no-class-assign": 2, - "no-cond-assign": 2, - "no-const-assign": 2, - "no-control-regex": 2, - "no-debugger": 2, - "no-delete-var": 2, - "no-dupe-args": 2, - "no-dupe-class-members": 2, - "no-dupe-keys": 2, - "no-duplicate-case": 2, - "no-empty-character-class": 2, - "no-eval": 2, - "no-ex-assign": 2, - "no-extend-native": 2, - "no-extra-bind": 2, - "no-extra-boolean-cast": 2, - "no-extra-parens": [2, "functions"], - "no-fallthrough": 2, - "no-floating-decimal": 2, - "no-func-assign": 2, - "no-implied-eval": 2, - "no-inner-declarations": [2, "functions"], - "no-invalid-regexp": 2, - "no-irregular-whitespace": 2, - "no-iterator": 2, - "no-label-var": 2, - "no-labels": 2, - "no-lone-blocks": 2, - "no-mixed-spaces-and-tabs": 2, - "no-multi-spaces": 2, - "no-multi-str": 2, - "no-multiple-empty-lines": [2, { "max": 1 }], - "no-native-reassign": 0, - "no-negated-in-lhs": 2, - "no-new": 2, - "no-new-func": 2, - "no-new-object": 2, - "no-new-require": 2, - "no-new-wrappers": 2, - "no-obj-calls": 2, - "no-octal": 2, - "no-octal-escape": 2, - "no-proto": 0, - "no-redeclare": 2, - "no-regex-spaces": 2, - "no-return-assign": 2, - "no-self-compare": 2, - "no-sequences": 2, - "no-shadow-restricted-names": 2, - "no-spaced-func": 2, - "no-sparse-arrays": 2, - "no-this-before-super": 2, - "no-throw-literal": 2, - "no-trailing-spaces": 0, - "no-undef": 2, - "no-undef-init": 2, - "no-unexpected-multiline": 2, - "no-unneeded-ternary": [2, { "defaultAssignment": false }], - "no-unreachable": 2, - "no-unused-vars": [2, { "vars": "all", "args": "none" }], - "no-useless-call": 0, - "no-with": 2, - "one-var": [0, { "initialized": "never" }], - "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], - "padded-blocks": [0, "never"], - "quotes": [2, "single", "avoid-escape"], - "radix": 2, - "semi": [2, "always"], - "semi-spacing": [2, { "before": false, "after": true }], - "space-before-blocks": [2, "always"], - "space-before-function-paren": [2, "never"], - "space-in-parens": [2, "never"], - "space-infix-ops": 2, - "space-unary-ops": [2, { "words": true, "nonwords": false }], - "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], - "use-isnan": 2, - "valid-typeof": 2, - "wrap-iife": [2, "any"], - "yoda": [2, "never"] - } -} diff --git a/README.md b/README.md index 42b0714..ef08758 100644 --- a/README.md +++ b/README.md @@ -84,11 +84,11 @@ A descriptor may have additional _invalid_ properties (an error will **not** be var foo = {}; Object.defineProperty(foo, 'bar', { - enumerable: true, - whatever: 'blah', // invalid, but doesn't cause an error - get: function() { - return 'baz'; - } + enumerable: true, + whatever: 'blah', // invalid, but doesn't cause an error + get: function() { + return 'baz'; + } }); console.log(foo.bar); @@ -139,7 +139,7 @@ You might also be interested in these projects: ### Contributors -| **Commits** | **Contributor** | +| **Commits** | **Contributor** | | --- | --- | | 21 | [jonschlinkert](https://github.com/jonschlinkert) | | 2 | [realityking](https://github.com/realityking) | @@ -158,4 +158,4 @@ Released under the [MIT License](LICENSE). *** -_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on November 01, 2017._ \ No newline at end of file +_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on November 01, 2017._ diff --git a/index.js b/index.js index 361884c..54f0f67 100644 --- a/index.js +++ b/index.js @@ -1,29 +1,31 @@ 'use strict'; const hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key); -const isObject = val => { - return val !== null && typeof val === 'object' && !Array.isArray(val); -}; +const isObject = (val) => val !== null && typeof val === 'object' && !Array.isArray(val); const isDescriptor = (obj, key) => { - if (!isObject(obj)) return false; - let desc = key ? Object.getOwnPropertyDescriptor(obj, key) : obj; - if (isObject(desc)) { - let booleans = ['configurable', 'enumerable', 'writable']; - if (!hasOwn(desc, 'value') || hasOwn(desc, 'get') || hasOwn(desc, 'set')) { - return false; - } - for (let key of Object.keys(desc)) { - if (booleans.includes(key) && typeof desc[key] !== 'boolean') { - return false; - } - if (!booleans.includes(key) && key !== 'value') { - return false; - } - } - return true; - } - return false; + if (!isObject(obj)) { + return false; + } + const desc = key ? Object.getOwnPropertyDescriptor(obj, key) : obj; + if (isObject(desc)) { + const booleans = [ + 'configurable', 'enumerable', 'writable', + ]; + if (!hasOwn(desc, 'value') || hasOwn(desc, 'get') || hasOwn(desc, 'set')) { + return false; + } + for (const descKey of Object.keys(desc)) { + if (booleans.includes(descKey) && typeof desc[descKey] !== 'boolean') { + return false; + } + if (!booleans.includes(descKey) && descKey !== 'value') { + return false; + } + } + return true; + } + return false; }; module.exports = isDescriptor; diff --git a/package.json b/package.json index 9a9dc7b..3b9f7c7 100644 --- a/package.json +++ b/package.json @@ -1,69 +1,73 @@ { - "name": "is-data-descriptor", - "description": "Returns true if a value has the characteristics of a valid JavaScript data descriptor.", - "version": "2.0.0", - "homepage": "https://github.com/jonschlinkert/is-data-descriptor", - "author": "Jon Schlinkert (https://github.com/jonschlinkert)", - "contributors": [ - "Jon Schlinkert (http://twitter.com/jonschlinkert)", - "Rouven Weßling (www.rouvenwessling.de)" - ], - "repository": "jonschlinkert/is-data-descriptor", - "bugs": { - "url": "https://github.com/jonschlinkert/is-data-descriptor/issues" - }, - "license": "MIT", - "files": [ - "index.js" - ], - "main": "index.js", - "engines": { - "node": ">=6" - }, - "scripts": { - "test": "mocha" - }, - "devDependencies": { - "gulp-format-md": "^1.0.0", - "mocha": "^3.5.3" - }, - "keywords": [ - "accessor", - "check", - "data", - "descriptor", - "get", - "getter", - "is", - "keys", - "object", - "properties", - "property", - "set", - "setter", - "type", - "valid", - "value" - ], - "verb": { - "toc": false, - "layout": "default", - "tasks": [ - "readme" - ], - "plugins": [ - "gulp-format-md" - ], - "related": { - "list": [ - "is-accessor-descriptor", - "is-data-descriptor", - "is-descriptor", - "isobject" - ] - }, - "lint": { - "reflinks": true - } - } + "name": "is-data-descriptor", + "description": "Returns true if a value has the characteristics of a valid JavaScript data descriptor.", + "version": "2.0.0", + "homepage": "https://github.com/jonschlinkert/is-data-descriptor", + "author": "Jon Schlinkert (https://github.com/jonschlinkert)", + "contributors": [ + "Jon Schlinkert (http://twitter.com/jonschlinkert)", + "Rouven Weßling (www.rouvenwessling.de)" + ], + "repository": "jonschlinkert/is-data-descriptor", + "bugs": { + "url": "https://github.com/jonschlinkert/is-data-descriptor/issues" + }, + "license": "MIT", + "files": [ + "index.js" + ], + "main": "index.js", + "engines": { + "node": ">=6" + }, + "scripts": { + "lint": "eslint --ext=js,mjs .", + "pretest": "npm run lint", + "test": "mocha" + }, + "devDependencies": { + "@ljharb/eslint-config": "^21.0.1", + "eslint": "=8.8.0", + "gulp-format-md": "^1.0.0", + "mocha": "^3.5.3" + }, + "keywords": [ + "accessor", + "check", + "data", + "descriptor", + "get", + "getter", + "is", + "keys", + "object", + "properties", + "property", + "set", + "setter", + "type", + "valid", + "value" + ], + "verb": { + "toc": false, + "layout": "default", + "tasks": [ + "readme" + ], + "plugins": [ + "gulp-format-md" + ], + "related": { + "list": [ + "is-accessor-descriptor", + "is-data-descriptor", + "is-descriptor", + "isobject" + ] + }, + "lint": { + "reflinks": true + } + } } diff --git a/test.js b/test.js deleted file mode 100644 index cbb15ea..0000000 --- a/test.js +++ /dev/null @@ -1,71 +0,0 @@ -'use strict'; - -require('mocha'); -const assert = require('assert'); -const isDescriptor = require('./'); -const noop = () => {}; - -describe('isDescriptor', () => { - describe('value type', () => { - it('should be false when not an object:', () => { - assert(!isDescriptor('a')); - assert(!isDescriptor(null)); - assert(!isDescriptor([])); - }); - }); - - describe('data descriptor:', () => { - it('should be false when the object has invalid properties:', () => { - assert(!isDescriptor({ value: 'foo', bar: 'baz' })); - assert(!isDescriptor({ value: 'foo', bar: 'baz' })); - }); - - it('should be false when the object has get or set properties', () => { - assert(!isDescriptor({ value: 'foo', get: noop })); - assert(!isDescriptor({ get: noop, value: noop })); - }); - - it('should be false when the object has invalid properties and strict is true', () => { - assert(!isDescriptor({ value: 'foo', bar: 'baz' })); - assert(!isDescriptor({ value: 'foo', bar: 'baz' })); - assert(!isDescriptor({ value: 'foo', get: noop })); - assert(!isDescriptor({ get: noop, value: noop })); - }); - - it('should be true when the object has valid data-descriptor properties', () => { - assert(isDescriptor({ value: 'foo' })); - assert(isDescriptor({ value: noop })); - }); - - it('should be false when valid properties are invalid types', () => { - assert(!isDescriptor({ value: 'foo', enumerable: 'foo' })); - assert(!isDescriptor({ value: 'foo', configurable: 'foo' })); - assert(!isDescriptor({ value: 'foo', writable: 'foo' })); - }); - - it('should be true when a value is a valid data descriptor', () => { - assert(isDescriptor({ value: 'foo' })); - assert(!isDescriptor({ writable: true })); - assert(!isDescriptor({ value: 'foo', get: 'foo' })); - }); - - it('should be false when descriptor has an in-valid propery and "strict" is true', () => { - assert(isDescriptor({ value: 'foo' })); - assert(!isDescriptor({ writable: true }, void 0, true)); - assert(!isDescriptor({ value: 'foo', get: 'foo' }, void 0, true)); - }); - - it('should be false when the value is not a valid descriptor', () => { - assert(!isDescriptor('foo')); - assert(!isDescriptor({})); - assert(!isDescriptor({ configurable: true })); - assert(!isDescriptor({ enumerable: true })); - assert(!isDescriptor({ - get: undefined, - set: undefined, - enumerable: true, - configurable: true - })); - }); - }); -}); diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..25bcefa --- /dev/null +++ b/test/index.js @@ -0,0 +1,71 @@ +'use strict'; + +require('mocha'); +const assert = require('assert'); +const isDescriptor = require('../'); +const noop = () => {}; + +describe('isDescriptor', () => { + describe('value type', () => { + it('should be false when not an object:', () => { + assert(!isDescriptor('a')); + assert(!isDescriptor(null)); + assert(!isDescriptor([])); + }); + }); + + describe('data descriptor:', () => { + it('should be false when the object has invalid properties:', () => { + assert(!isDescriptor({ value: 'foo', bar: 'baz' })); + assert(!isDescriptor({ value: 'foo', bar: 'baz' })); + }); + + it('should be false when the object has get or set properties', () => { + assert(!isDescriptor({ value: 'foo', get: noop })); + assert(!isDescriptor({ get: noop, value: noop })); + }); + + it('should be false when the object has invalid properties and strict is true', () => { + assert(!isDescriptor({ value: 'foo', bar: 'baz' })); + assert(!isDescriptor({ value: 'foo', bar: 'baz' })); + assert(!isDescriptor({ value: 'foo', get: noop })); + assert(!isDescriptor({ get: noop, value: noop })); + }); + + it('should be true when the object has valid data-descriptor properties', () => { + assert(isDescriptor({ value: 'foo' })); + assert(isDescriptor({ value: noop })); + }); + + it('should be false when valid properties are invalid types', () => { + assert(!isDescriptor({ value: 'foo', enumerable: 'foo' })); + assert(!isDescriptor({ value: 'foo', configurable: 'foo' })); + assert(!isDescriptor({ value: 'foo', writable: 'foo' })); + }); + + it('should be true when a value is a valid data descriptor', () => { + assert(isDescriptor({ value: 'foo' })); + assert(!isDescriptor({ writable: true })); + assert(!isDescriptor({ value: 'foo', get: 'foo' })); + }); + + it('should be false when descriptor has an in-valid propery and "strict" is true', () => { + assert(isDescriptor({ value: 'foo' })); + assert(!isDescriptor({ writable: true }, void 0, true)); + assert(!isDescriptor({ value: 'foo', get: 'foo' }, void 0, true)); + }); + + it('should be false when the value is not a valid descriptor', () => { + assert(!isDescriptor('foo')); + assert(!isDescriptor({})); + assert(!isDescriptor({ configurable: true })); + assert(!isDescriptor({ enumerable: true })); + assert(!isDescriptor({ + get: undefined, + set: undefined, + enumerable: true, + configurable: true, + })); + }); + }); +});