Skip to content

Commit a81e1f9

Browse files
committedMay 12, 2020
Add stricter types
1 parent 3e00252 commit a81e1f9

File tree

5 files changed

+23
-24
lines changed

5 files changed

+23
-24
lines changed
 

‎.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
tests/fixtures/**
2+
build
3+
dist
4+
lib

‎package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313
"repository": "git@github.com:infctr/eslint-plugin-typescript-sort-keys.git",
1414
"scripts": {
1515
"build": "yarn rimraf lib && yarn compile",
16-
"build-tests": "yarn rimraf build && yarn compile-tests",
16+
"build-tests": "yarn rimraf build && yarn compile-tests && mv build/rules build/source && cp -r tests/fixtures/rulesdir build/rules",
1717
"compile": "yarn rollup -c",
1818
"compile-tests": "yarn babel --extensions .ts src --out-dir build",
1919
"docs": "eslint-docs",
2020
"docs:check": "eslint-docs check",
2121
"lint": "eslint --ext .js,.ts src/ tests/",
22-
"postlint": "yarn docs:check",
2322
"format": "prettier --write src/**/*.{js,ts} tests/**/*.{js,ts}",
24-
"test": "yarn jest --runInBand",
25-
"test-compiled": "mv build/rules build/source && cp -r tests/fixtures/rulesdir build/rules && USE_COMPILED=1 yarn test",
23+
"test": "yarn jest",
24+
"test-compiled": "USE_COMPILED=1 yarn test",
25+
"test-coverage": "yarn test-compiled --coverage --watchAll=false --coverageReporters=lcov,text-summary",
2626
"verify": "yarn build && yarn lint && yarn build-tests && yarn test-compiled && rimraf build"
2727
},
2828
"dependencies": {

‎src/rules/string-enum.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { JSONSchema4 } from 'json-schema';
2+
import { TSESTree, AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
23

34
import { getObjectBody } from 'utils/ast';
45
import { createReporter } from 'utils/plugin';
@@ -72,11 +73,11 @@ export const rule = createRule<keyof typeof errorMessages, Options>({
7273

7374
return {
7475
TSEnumDeclaration(node) {
75-
const body = getObjectBody(node);
76+
const body = getObjectBody(node) as TSESTree.TSEnumMember[];
7677
const isStringEnum = body.every(
77-
member =>
78+
(member: TSESTree.TSEnumMember) =>
7879
member.initializer &&
79-
member.initializer.type === 'Literal' &&
80+
member.initializer.type === AST_NODE_TYPES.Literal &&
8081
typeof member.initializer.value === 'string',
8182
);
8283

‎src/utils/ast.ts

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ export function getObjectBody(
1414
case AST_NODE_TYPES.TSEnumDeclaration:
1515
case AST_NODE_TYPES.TSTypeLiteral:
1616
return node.members;
17-
default:
18-
return null;
1917
}
2018
}
2119

‎src/utils/plugin.ts

+12-15
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ function createNodeSwapper(context: UtilRuleContext<string, RuleOptions>) {
4343
*/
4444
function getLineRange(node: TSESTree.Comment): TSESTree.Range {
4545
const [start] = getRangeWithIndent(node);
46-
// TODO check SourceCode own methods
4746
const index = sourceCode.lineStartIndices.findIndex(n => start === n);
4847

4948
if (index < 0) {
@@ -63,14 +62,11 @@ function createNodeSwapper(context: UtilRuleContext<string, RuleOptions>) {
6362
return sourceCode.text.slice(...getIndentRange(node));
6463
}
6564

66-
function getNodePunctuator(token: TSESTree.Token) {
67-
const punctuator =
68-
token.type === AST_TOKEN_TYPES.Punctuator
69-
? token
70-
: sourceCode.getTokenAfter(token, {
71-
filter: n => n.type === AST_TOKEN_TYPES.Punctuator && n.value !== ':',
72-
includeComments: false,
73-
});
65+
function getNodePunctuator(node: TSESTree.Node) {
66+
const punctuator = sourceCode.getTokenAfter(node, {
67+
filter: n => n.type === AST_TOKEN_TYPES.Punctuator && n.value !== ':',
68+
includeComments: false,
69+
});
7470

7571
// Check the punctuator value outside of filter because we
7672
// want to stop traversal on any terminating punctuator
@@ -83,17 +79,18 @@ function createNodeSwapper(context: UtilRuleContext<string, RuleOptions>) {
8379
currentNode: TSType,
8480
replaceNode: TSType,
8581
) =>
86-
[currentNode, replaceNode].reduce((acc, node) => {
82+
[currentNode, replaceNode].reduce<RuleFix[]>((acc, node) => {
8783
const otherNode = node === currentNode ? replaceNode : currentNode;
8884
const comments = sourceCode.getCommentsBefore(node);
8985
const nextSibling = sourceCode.getTokenAfter(node);
9086
const isLastReplacingLast =
9187
nodePositions.get(node).final === nodePositions.size - 1 &&
9288
nodePositions.get(node).final === nodePositions.get(otherNode).initial;
9389

94-
let text = `${comments.length ? getIndentText(node) : ''}${sourceCode.getText(
95-
node,
96-
)}`;
90+
let text = [
91+
comments.length ? getIndentText(node) : '',
92+
sourceCode.getText(node),
93+
].join('');
9794

9895
// If nextSibling is the node punctuator, remove it
9996
if (nextSibling === getNodePunctuator(node)) {
@@ -116,7 +113,7 @@ function createNodeSwapper(context: UtilRuleContext<string, RuleOptions>) {
116113
fixer.insertTextBefore(
117114
otherNode,
118115
comments
119-
.map(c => sourceCode.getText(c))
116+
.map(comment => sourceCode.getText(comment as any))
120117
.concat('')
121118
.join('\n'),
122119
),
@@ -133,7 +130,7 @@ function createNodeSwapper(context: UtilRuleContext<string, RuleOptions>) {
133130
);
134131

135132
return acc;
136-
}, [] as RuleFix[]);
133+
}, []);
137134
}
138135

139136
export function createReporter<MessageIds extends string>(

0 commit comments

Comments
 (0)
Please sign in to comment.