Skip to content

Commit 3c9f97c

Browse files
committed
chore: update prettier and ci to format and check ts files
1 parent 24ff0f8 commit 3c9f97c

File tree

6 files changed

+211
-138
lines changed

6 files changed

+211
-138
lines changed

lib/node-utils.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/experimental-utils';
1+
import {
2+
AST_NODE_TYPES,
3+
TSESTree,
4+
} from '@typescript-eslint/experimental-utils';
25
import { RuleContext } from '@typescript-eslint/experimental-utils/dist/ts-eslint';
36

47
export function isCallExpression(
@@ -106,8 +109,10 @@ export function findClosestCallNode(
106109
}
107110
}
108111

109-
export function isObjectExpression(node: TSESTree.Expression): node is TSESTree.ObjectExpression {
110-
return node?.type === AST_NODE_TYPES.ObjectExpression
112+
export function isObjectExpression(
113+
node: TSESTree.Expression
114+
): node is TSESTree.ObjectExpression {
115+
return node?.type === AST_NODE_TYPES.ObjectExpression;
111116
}
112117

113118
export function hasThenProperty(node: TSESTree.Node) {
@@ -124,16 +129,24 @@ export function isAwaitExpression(
124129
return node && node.type === AST_NODE_TYPES.AwaitExpression;
125130
}
126131

127-
export function isArrowFunctionExpression(node: TSESTree.Node): node is TSESTree.ArrowFunctionExpression {
128-
return node && node.type === AST_NODE_TYPES.ArrowFunctionExpression
132+
export function isArrowFunctionExpression(
133+
node: TSESTree.Node
134+
): node is TSESTree.ArrowFunctionExpression {
135+
return node && node.type === AST_NODE_TYPES.ArrowFunctionExpression;
129136
}
130137

131-
export function isReturnStatement(node: TSESTree.Node): node is TSESTree.ReturnStatement {
132-
return node && node.type === AST_NODE_TYPES.ReturnStatement
138+
export function isReturnStatement(
139+
node: TSESTree.Node
140+
): node is TSESTree.ReturnStatement {
141+
return node && node.type === AST_NODE_TYPES.ReturnStatement;
133142
}
134143

135144
export function isAwaited(node: TSESTree.Node) {
136-
return isAwaitExpression(node) || isArrowFunctionExpression(node) || isReturnStatement(node)
145+
return (
146+
isAwaitExpression(node) ||
147+
isArrowFunctionExpression(node) ||
148+
isReturnStatement(node)
149+
);
137150
}
138151

139152
export function isPromiseResolved(node: TSESTree.Node) {
@@ -148,6 +161,13 @@ export function isPromiseResolved(node: TSESTree.Node) {
148161
return hasThenProperty(parent);
149162
}
150163

151-
export function getVariableReferences(context: RuleContext<string, []>, node: TSESTree.Node) {
152-
return (isVariableDeclarator(node) && context.getDeclaredVariables(node)[0].references.slice(1)) || [];
153-
}
164+
export function getVariableReferences(
165+
context: RuleContext<string, []>,
166+
node: TSESTree.Node
167+
) {
168+
return (
169+
(isVariableDeclarator(node) &&
170+
context.getDeclaredVariables(node)[0].references.slice(1)) ||
171+
[]
172+
);
173+
}

lib/rules/prefer-screen-queries.ts

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,23 @@ export const RULE_NAME = 'prefer-screen-queries';
1313
export type MessageIds = 'preferScreenQueries';
1414
type Options = [];
1515

16-
const ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING = ['container', 'baseElement']
16+
const ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING = [
17+
'container',
18+
'baseElement',
19+
];
1720
const ALL_QUERIES_COMBINATIONS_REGEXP = ALL_QUERIES_COMBINATIONS.join('|');
1821

1922
function usesContainerOrBaseElement(node: TSESTree.CallExpression) {
20-
const secondArgument = node.arguments[1]
21-
return isObjectExpression(secondArgument) && secondArgument.properties.some((property) => isProperty(property) && isIdentifier(property.key) && ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING.includes(property.key.name))
23+
const secondArgument = node.arguments[1];
24+
return (
25+
isObjectExpression(secondArgument) &&
26+
secondArgument.properties.some(
27+
property =>
28+
isProperty(property) &&
29+
isIdentifier(property.key) &&
30+
ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING.includes(property.key.name)
31+
)
32+
);
2233
}
2334

2435
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
@@ -53,33 +64,43 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5364
const queriesRegex = new RegExp(ALL_QUERIES_COMBINATIONS_REGEXP);
5465
const queriesDestructuredInWithinDeclaration: string[] = [];
5566
// use an array as within might be used more than once in a test
56-
const withinDeclaredVariables : string[] = []
67+
const withinDeclaredVariables: string[] = [];
5768

5869
return {
5970
VariableDeclarator(node) {
6071
if (!isCallExpression(node.init) || !isIdentifier(node.init.callee)) {
61-
return
72+
return;
6273
}
63-
const isWithinFunction = node.init.callee.name === 'within';
74+
const isWithinFunction = node.init.callee.name === 'within';
6475
// TODO add the custom render option #198
65-
const usesRenderOptions = node.init.callee.name === 'render' && usesContainerOrBaseElement(node.init);
76+
const usesRenderOptions =
77+
node.init.callee.name === 'render' &&
78+
usesContainerOrBaseElement(node.init);
6679

6780
if (!isWithinFunction && !usesRenderOptions) {
68-
return
81+
return;
6982
}
7083

7184
if (isObjectPattern(node.id)) {
7285
// save the destructured query methods
7386
const identifiers = node.id.properties
74-
.filter(property => isProperty(property) && isIdentifier(property.key) && queriesRegex.test(property.key.name))
75-
.map((property: TSESTree.Property) => (property.key as TSESTree.Identifier).name);
87+
.filter(
88+
property =>
89+
isProperty(property) &&
90+
isIdentifier(property.key) &&
91+
queriesRegex.test(property.key.name)
92+
)
93+
.map(
94+
(property: TSESTree.Property) =>
95+
(property.key as TSESTree.Identifier).name
96+
);
7697

7798
queriesDestructuredInWithinDeclaration.push(...identifiers);
78-
return
99+
return;
79100
}
80101

81102
if (isIdentifier(node.id)) {
82-
withinDeclaredVariables.push(node.id.name)
103+
withinDeclaredVariables.push(node.id.name);
83104
}
84105
},
85106
[`CallExpression > Identifier[name=/^${ALL_QUERIES_COMBINATIONS_REGEXP}$/]`](
@@ -96,18 +117,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
96117
[`MemberExpression > Identifier[name=/^${ALL_QUERIES_COMBINATIONS_REGEXP}$/]`](
97118
node: TSESTree.Identifier
98119
) {
99-
100120
function isIdentifierAllowed(name: string) {
101-
return ['screen', ...withinDeclaredVariables].includes(name)
121+
return ['screen', ...withinDeclaredVariables].includes(name);
102122
}
103123

104124
if (
105125
isIdentifier(node) &&
106126
isMemberExpression(node.parent) &&
107127
isCallExpression(node.parent.object) &&
108-
isIdentifier(node.parent.object.callee) &&
109-
node.parent.object.callee.name !== 'within' &&
110-
node.parent.object.callee.name === 'render' && !usesContainerOrBaseElement(node.parent.object)
128+
isIdentifier(node.parent.object.callee) &&
129+
node.parent.object.callee.name !== 'within' &&
130+
node.parent.object.callee.name === 'render' &&
131+
!usesContainerOrBaseElement(node.parent.object)
111132
) {
112133
reportInvalidUsage(node);
113134
return;
@@ -123,4 +144,4 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
123144
},
124145
};
125146
},
126-
});
147+
});

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"postbuild": "cpy README.md ./dist && cpy package.json ./dist && cpy LICENSE ./dist",
3030
"lint": "eslint . --ext .js,.ts",
3131
"lint:fix": "npm run lint -- --fix",
32-
"format": "prettier --write README.md {lib,docs,tests}/**/*.{js,md}",
33-
"format:check": "prettier --check README.md {lib,docs,tests}/**/*.{js,md}",
32+
"format": "prettier --write README.md {lib,docs,tests}/**/*.{js,ts,md}",
33+
"format:check": "prettier --check README.md {lib,docs,tests}/**/*.{js,ts,md}",
3434
"test:local": "jest",
3535
"test:ci": "jest --coverage",
3636
"test:update": "npm run test:local -- --u",
@@ -62,7 +62,7 @@
6262
"is-ci-cli": "^2.1.2",
6363
"jest": "^25.2.6",
6464
"lint-staged": "^10.2.11",
65-
"prettier": "1.18.2",
65+
"prettier": "1.19.1",
6666
"semantic-release": "^15.13.24",
6767
"ts-jest": "^25.3.0",
6868
"typescript": "^3.8.3"

0 commit comments

Comments
 (0)