Skip to content
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

feat(eslint-plugin): [require-array-sort-compare] add ignoreStringArrays option #1972

Merged
merged 3 commits into from Jun 6, 2020

Conversation

sosukesuzuki
Copy link
Contributor

Fixes #1963

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @sosukesuzuki!

typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community.

The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately.

Thanks again!


🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. As a thank you, your profile/company logo will be added to our main README which receives thousands of unique visitors per day.

@codecov
Copy link

codecov bot commented May 4, 2020

Codecov Report

Merging #1972 into master will decrease coverage by 0.01%.
The diff coverage is 77.77%.

@@            Coverage Diff             @@
##           master    #1972      +/-   ##
==========================================
- Coverage   93.45%   93.44%   -0.02%     
==========================================
  Files         171      171              
  Lines        7813     7821       +8     
  Branches     2227     2229       +2     
==========================================
+ Hits         7302     7308       +6     
- Misses        244      245       +1     
- Partials      267      268       +1     
Flag Coverage Δ
#unittest 93.44% <77.77%> (-0.02%) ⬇️
Impacted Files Coverage Δ
...int-plugin/src/rules/require-array-sort-compare.ts 88.23% <77.77%> (-11.77%) ⬇️

@bradzacher bradzacher added the enhancement: plugin rule option New rule option for an existing eslint-plugin rule label May 11, 2020
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your work!
a few comments.

Comment on lines 54 to 61
function isStringArrayNode(node: TSESTree.ArrayExpression): boolean {
return node.elements.every(element => {
const type = checker.getTypeAtLocation(
service.esTreeNodeToTSNodeMap.get(element),
);
return util.getTypeName(checker, type) === 'string';
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this only works for array expressions that have sort directly called on them: ['a'].sort()

A better solution would be to check the type of the node instead, and extract the type of the generic parameters.

All array types (string[] and Array<string>) are treated as Array<string> in code.
All tuple types ([a, b, c]) are treated as Tuple<T1, T2, ...> (a special variadic type).

const type = checker.getTypeAtLocation(
  service.esTreeNodeToTSNodeMap.get(node),
);
if (checker.isArrayType(type) || checker.isTupleType(type)) {
  const typeArgs = checker.getTypeArguments(type);
  return checker.getTypeArguments(type).every(
    arg => util.getTypeName(checker, arg) === 'string',
  );
}
return false;

@bradzacher bradzacher added the awaiting response Issues waiting for a reply from the OP or another party label May 17, 2020
@sosukesuzuki
Copy link
Contributor Author

Thank you for your review. I fixed all reviewed points!

@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label May 19, 2020
Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - thanks for this!

@bradzacher bradzacher merged commit 6dee784 into typescript-eslint:master Jun 6, 2020
@finnigantime
Copy link

@sosukesuzuki Thank you for adding this!

@sosukesuzuki sosukesuzuki deleted the 1963 branch June 8, 2020 08:02
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[require-array-sort-compare] add option to ignore string arrays
3 participants