Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kusotenpa committed Aug 10, 2020
0 parents commit f41c293
Show file tree
Hide file tree
Showing 8 changed files with 2,121 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const rulesDirPlugin = require('eslint-plugin-rulesdir')

rulesDirPlugin.RULES_DIR = 'lib/rules'

module.exports = {
extends: [
'plugin:@kusotenpa/base',
],
plugins: [ 'rulesdir' ],
env: {
es6: true,
node: true,
},
rules: {
'rulesdir/object-property-newline': 2,
},
}
63 changes: 63 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
### https://raw.github.com/github/gitignore/5d896f6791c4257b74696714c66b2530b8d95a51/Node.gitignore

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env



33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# eslint-plugin-destructuring-newline
Enforce placing destructuring properties on separate lines.

## Installation
```
$ npm install --save-dev eslint eslint-plugin-destructuring-newline
```

## Usage
In your `.eslintrc`
```
{
"plugins": [
"destructuring-newline"
],
"rules": {
"destructuring-newline/object-property-newline": 2
}
}
```

## Rule Details
```js
// bad
const { foo, bar } = obj3

// good
const { foo } = obj1
const {
foo,
bar,
} = obj2
```
7 changes: 7 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = {
rules: {
'object-property-newline': require('./rules/object-property-newline'),
},
}
41 changes: 41 additions & 0 deletions lib/rules/object-property-newline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict'

module.exports = {
meta: {
type: 'layout',
messages: {
propertiesOnNewline: 'Destructuring properties must go on a new line.',
},
fixable: 'whitespace',
},
create: context => {
const sourceCode = context.getSourceCode()

return {
ObjectPattern(node) {
if (node.properties.length <= 1) {
return
}

for (let i = 1; i < node.properties.length; i++) {
const lastPrev = sourceCode.getLastToken(node.properties[ i - 1 ])
const firstCurrent = sourceCode.getFirstToken(node.properties[ i ])

if (lastPrev.loc.end.line === firstCurrent.loc.start.line) {
context.report({
node,
loc: firstCurrent.loc,
messageId: 'propertiesOnNewline',
fix(fixer) {
const comma = sourceCode.getTokenBefore(firstCurrent)
const afterComma = [ comma.range[ 1 ], firstCurrent.range[ 0 ] ]

return fixer.replaceTextRange(afterComma, '\n')
},
})
}
}
},
}
},
}
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "eslint-plugin-destructuring-newline",
"version": "0.0.1",
"description": "Enforce placing destructuring properties on separate lines",
"main": "lib/index.js",
"license": "MIT",
"keywords": [
"eslint",
"eslint-plugin",
"destructuring",
"newline"
],
"scripts": {
"test": "mocha test/**/*.js --reporter spec"
},
"devDependencies": {
"@kusotenpa/eslint-plugin": "1.1.0",
"eslint": "7.6.0",
"eslint-plugin-rulesdir": "0.1.0",
"mocha": "8.1.1"
},
"peerDependencies": {
"eslint": ">=7.0.0"
}
}
67 changes: 67 additions & 0 deletions test/rules/object-property-newline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
'use strict'

const RuleTester = require('eslint').RuleTester

const rule = require('../../lib/rules/object-property-newline')

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
},
})

tester.run('object-property-newline', rule, {
valid: [
{
code: 'var {a,\nb} = obj;',
},
{
code: 'var {a} = obj;',
},
{
code: 'var {\na\n} = obj;',
},
{
code: 'function foo({a,\nb}){};',
},
],
invalid: [
{
code: 'var {a,b} = obj;',
output: 'var {a,\nb} = obj;',
errors: [ {
messageId: 'propertiesOnNewline',
} ],
},
{
code: 'var {a,b,c} = obj;',
output: 'var {a,\nb,\nc} = obj;',
errors: [
{
messageId: 'propertiesOnNewline',
},
{
messageId: 'propertiesOnNewline',
},
],
},
{
code: 'var {\na,b} = obj;',
output: 'var {\na,\nb} = obj;',
errors: [
{
messageId: 'propertiesOnNewline',
},
],
},
{
code: 'function foo({a,b}){};',
output: 'function foo({a,\nb}){};',
errors: [
{
messageId: 'propertiesOnNewline',
},
],
},
],
})
Loading

0 comments on commit f41c293

Please sign in to comment.