Skip to content

chore: update dependencies + run prettier on codebase #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Output
# Output
dist

# Logs
Expand Down Expand Up @@ -66,3 +66,8 @@ yarn-error.log
.pnp.js
# Yarn Integrity file
.yarn-integrity

# these cause more harm than good
# when working with contributors
package-lock.json
yarn.lock
4 changes: 3 additions & 1 deletion commitlint.config.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module.exports = { extends: ['@commitlint/config-conventional'] };
module.exports = {
extends: ['@commitlint/config-conventional'],
};
44 changes: 32 additions & 12 deletions lib/node-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/experimental-utils';
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { RuleContext } from '@typescript-eslint/experimental-utils/dist/ts-eslint';

export function isCallExpression(
Expand Down Expand Up @@ -84,7 +87,7 @@ export function findClosestCallExpressionNode(
return node;
}

if(!node.parent) return null
if (!node.parent) return null;

return findClosestCallExpressionNode(node.parent);
}
Expand All @@ -108,8 +111,10 @@ export function findClosestCallNode(
}
}

export function isObjectExpression(node: TSESTree.Expression): node is TSESTree.ObjectExpression {
return node?.type === AST_NODE_TYPES.ObjectExpression
export function isObjectExpression(
node: TSESTree.Expression
): node is TSESTree.ObjectExpression {
return node?.type === AST_NODE_TYPES.ObjectExpression;
}

export function hasThenProperty(node: TSESTree.Node) {
Expand All @@ -126,16 +131,24 @@ export function isAwaitExpression(
return node && node.type === AST_NODE_TYPES.AwaitExpression;
}

export function isArrowFunctionExpression(node: TSESTree.Node): node is TSESTree.ArrowFunctionExpression {
return node && node.type === AST_NODE_TYPES.ArrowFunctionExpression
export function isArrowFunctionExpression(
node: TSESTree.Node
): node is TSESTree.ArrowFunctionExpression {
return node && node.type === AST_NODE_TYPES.ArrowFunctionExpression;
}

export function isReturnStatement(node: TSESTree.Node): node is TSESTree.ReturnStatement {
return node && node.type === AST_NODE_TYPES.ReturnStatement
export function isReturnStatement(
node: TSESTree.Node
): node is TSESTree.ReturnStatement {
return node && node.type === AST_NODE_TYPES.ReturnStatement;
}

export function isAwaited(node: TSESTree.Node) {
return isAwaitExpression(node) || isArrowFunctionExpression(node) || isReturnStatement(node)
return (
isAwaitExpression(node) ||
isArrowFunctionExpression(node) ||
isReturnStatement(node)
);
}

export function isPromiseResolved(node: TSESTree.Node) {
Expand All @@ -150,6 +163,13 @@ export function isPromiseResolved(node: TSESTree.Node) {
return hasThenProperty(parent);
}

export function getVariableReferences(context: RuleContext<string, []>, node: TSESTree.Node) {
return (isVariableDeclarator(node) && context.getDeclaredVariables(node)[0].references.slice(1)) || [];
}
export function getVariableReferences(
context: RuleContext<string, []>,
node: TSESTree.Node
) {
return (
(isVariableDeclarator(node) &&
context.getDeclaredVariables(node)[0].references.slice(1)) ||
[]
);
}
4 changes: 1 addition & 3 deletions lib/rules/no-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import {

export const RULE_NAME = 'no-debug';
export type MessageIds = 'noDebug';
type Options = [
{ renderFunctions?: string[]; }
];
type Options = [{ renderFunctions?: string[] }];

function isRenderVariableDeclarator(
node: TSESTree.VariableDeclarator,
Expand Down
57 changes: 39 additions & 18 deletions lib/rules/prefer-screen-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@ export const RULE_NAME = 'prefer-screen-queries';
export type MessageIds = 'preferScreenQueries';
type Options = [];

const ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING = ['container', 'baseElement']
const ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING = [
'container',
'baseElement',
];
const ALL_QUERIES_COMBINATIONS_REGEXP = ALL_QUERIES_COMBINATIONS.join('|');

function usesContainerOrBaseElement(node: TSESTree.CallExpression) {
const secondArgument = node.arguments[1]
return isObjectExpression(secondArgument) && secondArgument.properties.some((property) => isProperty(property) && isIdentifier(property.key) && ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING.includes(property.key.name))
const secondArgument = node.arguments[1];
return (
isObjectExpression(secondArgument) &&
secondArgument.properties.some(
property =>
isProperty(property) &&
isIdentifier(property.key) &&
ALLOWED_RENDER_PROPERTIES_FOR_DESTRUCTURING.includes(property.key.name)
)
);
}

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

return {
VariableDeclarator(node) {
if (!isCallExpression(node.init) || !isIdentifier(node.init.callee)) {
return
return;
}
const isWithinFunction = node.init.callee.name === 'within';
const isWithinFunction = node.init.callee.name === 'within';
// TODO add the custom render option #198
const usesRenderOptions = node.init.callee.name === 'render' && usesContainerOrBaseElement(node.init);
const usesRenderOptions =
node.init.callee.name === 'render' &&
usesContainerOrBaseElement(node.init);

if (!isWithinFunction && !usesRenderOptions) {
return
return;
}

if (isObjectPattern(node.id)) {
// save the destructured query methods
const identifiers = node.id.properties
.filter(property => isProperty(property) && isIdentifier(property.key) && queriesRegex.test(property.key.name))
.map((property: TSESTree.Property) => (property.key as TSESTree.Identifier).name);
.filter(
property =>
isProperty(property) &&
isIdentifier(property.key) &&
queriesRegex.test(property.key.name)
)
.map(
(property: TSESTree.Property) =>
(property.key as TSESTree.Identifier).name
);

queriesDestructuredInWithinDeclaration.push(...identifiers);
return
return;
}

if (isIdentifier(node.id)) {
withinDeclaredVariables.push(node.id.name)
withinDeclaredVariables.push(node.id.name);
}
},
[`CallExpression > Identifier[name=/^${ALL_QUERIES_COMBINATIONS_REGEXP}$/]`](
Expand All @@ -96,18 +117,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
[`MemberExpression > Identifier[name=/^${ALL_QUERIES_COMBINATIONS_REGEXP}$/]`](
node: TSESTree.Identifier
) {

function isIdentifierAllowed(name: string) {
return ['screen', ...withinDeclaredVariables].includes(name)
return ['screen', ...withinDeclaredVariables].includes(name);
}

if (
isIdentifier(node) &&
isMemberExpression(node.parent) &&
isCallExpression(node.parent.object) &&
isIdentifier(node.parent.object.callee) &&
node.parent.object.callee.name !== 'within' &&
node.parent.object.callee.name === 'render' && !usesContainerOrBaseElement(node.parent.object)
isIdentifier(node.parent.object.callee) &&
node.parent.object.callee.name !== 'within' &&
node.parent.object.callee.name === 'render' &&
!usesContainerOrBaseElement(node.parent.object)
) {
reportInvalidUsage(node);
return;
Expand All @@ -123,4 +144,4 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
},
};
},
});
});
Loading