[英]How to configure prettier or eslint to put a newline after function definitions and other blocks?
Right now prettier is giving me this:现在 prettier 给我这个:
function _project(x, k, dim: number = -1, eps: Float = -1.0) {
if (eps < 0) {
if (x.dtype == torch.float32) {
eps = 4e-3
} else {
eps = 1e-5
}
}
maxnorm = (1 - eps) / sabs(k) ** 0.5
maxnorm = torch.where(k.lt(0), maxnorm, k.newFull([], 1e15))
norm = x.norm({ dim: dim, keepdim: true, p: 2 }).clampMin(1e-15)
cond = norm > maxnorm
projected = (x / norm) * maxnorm
return torch.where(cond, projected, x)
}
function lambdaX(
x: TorchTensor,
k: TorchTensor,
keepdim = false,
dim = -1,
) {
return _lambdaX(x, k, { keepdim: keepdim, dim: dim })
}
function _lambdaX(
x: TorchTensor,
k: TorchTensor,
keepdim: Bool = false,
dim: number = -1,
) {
return (
2 /
(1 + k * x.pow(2).sum({ dim: dim, keepdim: keepdim })).clampMin(
1e-15,
)
)
}
function inner(
x: TorchTensor,
u: TorchTensor,
v: TorchTensor,
k,
keepdim = false,
dim = -1,
) {
Is there a way to configure either prettier or eslint to put a space after some of the blocks, so it is more like this:有没有办法配置 prettier 或 eslint 在某些块之后放置一个空格,所以它更像这样:
function _project(x, k, dim: number = -1, eps: Float = -1.0) {
if (eps < 0) {
if (x.dtype == torch.float32) {
eps = 4e-3
} else {
eps = 1e-5
}
}
maxnorm = (1 - eps) / sabs(k) ** 0.5
maxnorm = torch.where(k.lt(0), maxnorm, k.newFull([], 1e15))
norm = x.norm({ dim: dim, keepdim: true, p: 2 }).clampMin(1e-15)
cond = norm > maxnorm
projected = (x / norm) * maxnorm
return torch.where(cond, projected, x)
}
function lambdaX(
x: TorchTensor,
k: TorchTensor,
keepdim = false,
dim = -1,
) {
return _lambdaX(x, k, { keepdim: keepdim, dim: dim })
}
function _lambdaX(
x: TorchTensor,
k: TorchTensor,
keepdim: Bool = false,
dim: number = -1,
) {
return (
2 /
(1 + k * x.pow(2).sum({ dim: dim, keepdim: keepdim })).clampMin(
1e-15,
)
)
}
function inner(
x: TorchTensor,
u: TorchTensor,
v: TorchTensor,
k,
keepdim = false,
dim = -1,
) {
My prettier config currently is:我目前更漂亮的配置是:
{
semi: false,
parser: 'typescript',
trailingComma: 'all',
singleQuote: true,
printWidth: 72,
tabWidth: 2,
useTabs: false,
arrowParens: 'avoid',
quoteProps: 'as-needed',
bracketSpacing: true,
proseWrap: 'always',
endOfLine: 'lf',
singleAttributePerLine: true,
importOrder: [
'^\\w(.*)$',
'^@(.*)$',
'~(.*)$',
'\\..(.*)$',
'\\.(.*)$',
],
importOrderSeparation: true,
importOrderSortSpecifiers: true,
}
And I am programmatically running it like this:我像这样以编程方式运行它:
const prettier = require('prettier')
function pretty(string) {
return prettier.format(string, config)
}
What is as close as you can get to this "space between blocks" style?您最接近这种“块之间的空间”风格的是什么?
I think you're looking for the padding-line-between-statements
rule for ESlint.我认为您正在寻找 ESlint
padding-line-between-statements
规则。 You can even define multiple rules;您甚至可以定义多个规则;
{
"padding-line-between-statements": [
"error",
{ "blankLine": "always", "prev": "function", "next": "function" },
{ "blankLine": "always", "prev": "function", "next": "block" },
{ "blankLine": "always", "prev": "block", "next": "function" },
...
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.