I would like to apply an ESLint rule that forces newlines before and after /* block comments / but not around /* JSDoc comments */. For those, I’d like a newline before the comment only, but I can’t figure out how to set up a rule to do this. Any help would be appreciated.
My goal for valid comments:
const a = 1;
/*
* This multiline comment has
* newlines above and below it
*/
const b = 2;
/**
* This javadoc describes the function
* direction below it and should have
* a newline above it, but not below
*/
const myFunc = () => {}
I’ve attempted setting up an ignorePatter within the lines-around-comment
setting but it either applies to both types of block comments, neither, or just crashes ESLint.
This attempt ignores both types of block comments for some reason
...
"lines-around-comment": [
"warn",
{
"afterBlockComment": true,
"beforeLineComment": true
"ignorePattern": "\*\*"
}
],
...
This one applies to neither
...
"lines-around-comment": [
"warn",
{
"afterBlockComment": true,
"beforeLineComment": true
"ignorePattern": "\*"
}
],
...
And this one crashes ESLint
...
"lines-around-comment": [
"warn",
{
"afterBlockComment": true,
"beforeLineComment": true
"ignorePattern": "*"
}
],
...
I’ll also add that I am ignoring (via an exception) the requirement for spaced-comment
s when using JSDoc via this config:
...
"spaced-comment": [
"error",
"always",
{ "exceptions": ["*"] }
]
...
Full ESLint file:
{
"root": true,
"plugins": [],
"parser": "@babel/eslint-parser",
"extends": "eslint:recommended",
"rules": {
"array-callback-return": "error",
"no-self-compare": "error",
"no-template-curly-in-string": "error",
"curly": [
"warn",
"all"
],
"default-case": "error",
"dot-notation": "warn",
"eqeqeq": [
"error",
"always"
],
"no-else-return": "warn",
"no-var": "error",
"prefer-const": "error",
"prefer-template": "error",
"radix": "error",
"array-bracket-newline": [
"error",
"consistent"
],
"array-bracket-spacing": [
"warn",
"always",
{
"singleValue": false,
"objectsInArrays": false,
"arraysInArrays": false
}
],
"array-element-newline": [
"warn",
{
"multiline": true,
"minItems": 3
}
],
"arrow-parens": [
"warn",
"as-needed"
],
"arrow-spacing": [
"error",
{
"before": true,
"after": true
}
],
"block-spacing": [
"error",
"always"
],
"brace-style": [
"error",
"1tbs"
],
"comma-dangle": [
"warn",
"always-multiline"
],
"comma-spacing": [
"error",
{
"before": false,
"after": true
}
],
"dot-location": [
"error",
"property"
],
"eol-last": [
"warn",
"always"
],
"func-call-spacing": [
"error",
"never"
],
"function-call-argument-newline": [
"error",
"never"
],
"function-paren-newline": [
"error",
"never"
],
"implicit-arrow-linebreak": [
"error",
"beside"
],
"indent": [
"error",
2,
{
"SwitchCase": 1,
"flatTernaryExpressions": true,
"offsetTernaryExpressions": true
}
],
"keyword-spacing": [
"warn",
{
"before": true,
"after": false
}
],
"lines-around-comment": [
"warn",
{
"afterBlockComment": true,
"beforeLineComment": true,
"ignorePattern": "*"
}
],
"multiline-ternary": [
"error",
"always"
],
"newline-per-chained-call": [
"error"
],
"no-extra-parens": [
"error",
"all"
],
"no-multi-spaces": [
"warn",
{
"ignoreEOLComments": true
}
],
"no-multiple-empty-lines": "warn",
"no-tabs": "error",
"no-trailing-spaces": "warn",
"no-whitespace-before-property": "error",
"object-curly-newline": [
"error",
{
"multiline": true
}
],
"object-curly-spacing": [
"error",
"always"
],
"object-property-newline": "error",
"operator-linebreak": [
"error",
"before"
],
"quotes": [
"error",
"single",
{
"avoidEscape": true,
"allowTemplateLiterals": true
}
],
"rest-spread-spacing": [
"error",
"never"
],
"semi": [
"error",
"always"
],
"semi-spacing": "error",
"semi-style": [
"error",
"last"
],
"space-before-blocks": [
"error",
{
"functions": "never",
"keywords": "always",
"classes": "never"
}
],
"space-before-function-paren": [
"error",
{
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}
],
"space-in-parens": [
"error",
"never"
],
"spaced-comment": [
"error",
"always",
{
"exceptions": [
// Allow lines to start with a second *, which
// basically allows jsdoc to work.
"*"
]
}
]
},
"globals": {
"define": "readonly",
"log": "readonly",
"Promise": "readonly"
}
}