Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
掘金者说
vscode
提交
2ab5b8bd
V
vscode
项目概览
掘金者说
/
vscode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vscode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
2ab5b8bd
编写于
9月 24, 2020
作者:
A
Alex Dima
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Convert `build/lib/eslint/code-no-unused-expressions.js` to `.ts`
上级
9f806960
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
36 addition
and
32 deletion
+36
-32
build/lib/eslint/code-no-unused-expressions.ts
build/lib/eslint/code-no-unused-expressions.ts
+36
-32
未找到文件。
build/lib/eslint/code-no-unused-expressions.
j
s
→
build/lib/eslint/code-no-unused-expressions.
t
s
浏览文件 @
2ab5b8bd
...
...
@@ -13,6 +13,10 @@
'
use strict
'
;
import
*
as
eslint
from
'
eslint
'
;
import
{
TSESTree
}
from
'
@typescript-eslint/experimental-utils
'
;
import
*
as
ESTree
from
'
estree
'
;
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
...
...
@@ -50,29 +54,29 @@ module.exports = {
]
},
create
(
context
)
{
create
(
context
:
eslint
.
Rule
.
RuleContext
)
{
const
config
=
context
.
options
[
0
]
||
{},
allowShortCircuit
=
config
.
allowShortCircuit
||
false
,
allowTernary
=
config
.
allowTernary
||
false
,
allowTaggedTemplates
=
config
.
allowTaggedTemplates
||
false
;
// eslint-disable-next-line jsdoc/require-description
/**
* @param {ASTNode}
node any node
* @returns {boolean}
whether the given node structurally represents a directive
*/
function
looksLikeDirective
(
node
)
{
/**
* @param
node any node
* @returns
whether the given node structurally represents a directive
*/
function
looksLikeDirective
(
node
:
TSESTree
.
Node
):
boolean
{
return
node
.
type
===
'
ExpressionStatement
'
&&
node
.
expression
.
type
===
'
Literal
'
&&
typeof
node
.
expression
.
value
===
'
string
'
;
}
// eslint-disable-next-line jsdoc/require-description
/**
* @param {Function}
predicate ([a] -> Boolean) the function used to make the determination
* @param {a[]}
list the input list
* @returns {a[]}
the leading sequence of members in the given list that pass the given predicate
*/
function
takeWhile
(
predicate
,
list
)
{
/**
* @param
predicate ([a] -> Boolean) the function used to make the determination
* @param
list the input list
* @returns
the leading sequence of members in the given list that pass the given predicate
*/
function
takeWhile
<
T
>
(
predicate
:
(
item
:
T
)
=>
boolean
,
list
:
T
[]):
T
[]
{
for
(
let
i
=
0
;
i
<
list
.
length
;
++
i
)
{
if
(
!
predicate
(
list
[
i
]))
{
return
list
.
slice
(
0
,
i
);
...
...
@@ -82,21 +86,21 @@ module.exports = {
}
// eslint-disable-next-line jsdoc/require-description
/**
* @param {ASTNode}
node a Program or BlockStatement node
* @returns {ASTNode[]}
the leading sequence of directive nodes in the given node's body
*/
function
directives
(
node
)
{
/**
* @param
node a Program or BlockStatement node
* @returns
the leading sequence of directive nodes in the given node's body
*/
function
directives
(
node
:
TSESTree
.
Program
|
TSESTree
.
BlockStatement
):
TSESTree
.
Node
[]
{
return
takeWhile
(
looksLikeDirective
,
node
.
body
);
}
// eslint-disable-next-line jsdoc/require-description
/**
* @param {ASTNode}
node any node
* @param {ASTNode[]}
ancestors the given node's ancestors
* @returns {boolean}
whether the given node is considered a directive in its current position
*/
function
isDirective
(
node
,
ancestors
)
{
/**
* @param
node any node
* @param
ancestors the given node's ancestors
* @returns
whether the given node is considered a directive in its current position
*/
function
isDirective
(
node
:
TSESTree
.
Node
,
ancestors
:
TSESTree
.
Node
[]):
boolean
{
const
parent
=
ancestors
[
ancestors
.
length
-
1
],
grandparent
=
ancestors
[
ancestors
.
length
-
2
];
...
...
@@ -105,12 +109,12 @@ module.exports = {
directives
(
parent
).
indexOf
(
node
)
>=
0
;
}
/**
* Determines whether or not a given node is a valid expression. Recurses on short circuit eval and ternary nodes if enabled by flags.
* @param {ASTNode}
node any node
* @returns {boolean}
whether the given node is a valid expression
*/
function
isValidExpression
(
node
)
{
/**
* Determines whether or not a given node is a valid expression. Recurses on short circuit eval and ternary nodes if enabled by flags.
* @param
node any node
* @returns
whether the given node is a valid expression
*/
function
isValidExpression
(
node
:
TSESTree
.
Node
):
boolean
{
if
(
allowTernary
)
{
// Recursive check for ternary and logical expressions
...
...
@@ -134,9 +138,9 @@ module.exports = {
}
return
{
ExpressionStatement
(
node
)
{
if
(
!
isValidExpression
(
node
.
expression
)
&&
!
isDirective
(
node
,
context
.
getAncestors
()))
{
context
.
report
({
node
,
message
:
'
Expected an assignment or function call and instead saw an expression.
'
});
ExpressionStatement
(
node
:
TSESTree
.
ExpressionStatement
)
{
if
(
!
isValidExpression
(
node
.
expression
)
&&
!
isDirective
(
node
,
<
TSESTree
.
Node
[]
>
context
.
getAncestors
()))
{
context
.
report
({
node
:
<
ESTree
.
Node
>
node
,
message
:
'
Expected an assignment or function call and instead saw an expression.
'
});
}
}
};
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录