Skip to content

chore: updating dev deps. check formatting on travis ci #215

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

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .huskyrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"hooks": {
"pre-commit": "npm run test && lint-staged",
"pre-commit": "lint-staged",
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
8 changes: 6 additions & 2 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{
"*.js": ["eslint --fix", "git add"],
"*.md": ["prettier --write", "git add"]
"*.{js,ts}": [
"eslint --fix",
"prettier --write",
"jest --findRelatedTests",
],
"*.md": ["prettier --write"]
}
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ before_script:

jobs:
include:
- stage: test
node_js: 14
env: ESLINT=6
script:
- npm run format:check
- npm run lint -- --max-warnings 0
- stage: release
if: branch = master AND type != pull_request
node_js: 14
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
[![Tweet][tweet-badge]][tweet-url]

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

[![All Contributors](https://img.shields.io/badge/all_contributors-31-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

## Installation
Expand Down Expand Up @@ -222,6 +224,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
42 changes: 31 additions & 11 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 @@ -106,8 +109,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 @@ -124,16 +129,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 @@ -148,6 +161,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)) ||
[]
);
}
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