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

Refactor to turn on noUncheckedIndexedAccess option for TypeScript #5983

Merged
merged 20 commits into from Apr 19, 2022

Conversation

ybiquitous
Copy link
Member

@ybiquitous ybiquitous commented Mar 24, 2022

https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess

Which issue, if any, is this issue related to?

Ref: #5982 (comment)

Is there anything in the PR that needs further explanation?

The noUncheckedIndexedAccess option allows us to detect potential bugs like #5982, e.g.

const firstNoe = node.nodes[0];
firstNode.value; // `firstNode` may be undefined, but TS compiler won't complain unless `noUncheckedIndexedAccess`.

Note: This pull request just helps us to see the errors if noUncheckedIndexedAccess is turned on. We cannot merge it now.

See https://www.typescriptlang.org/tsconfig#noUncheckedIndexedAccess


EDIT: If you want to try it on a branch other than this one, you can use the CLI flag --noUncheckedIndexedAccess, for example:

$ npm run lint:types -- --noUncheckedIndexedAccess

@ybiquitous ybiquitous marked this pull request as ready for review April 19, 2022 00:17
@@ -117,7 +117,7 @@ function longer(hex) {
let hexVariant = '#';

for (let i = 1; i < hex.length; i++) {
hexVariant += hex[i] + hex[i];
hexVariant += hex.charAt(i).repeat(2);
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] .charAt() always returns a string (e.g. an empty string).

for (let i = 0; i < lines.length; i++) {
let line = lines[i];

for (let [i, line] of lines.entries()) {
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] .entries() makes a traditional for loop much readable.

@@ -110,16 +110,20 @@ const rule = (primary, secondaryOptions) => {
* @returns {boolean}
*/
function hasInterpolatingAmpersand(selector) {
for (let i = 0, l = selector.length; i < l; i++) {
if (selector[i] !== '&') {
for (const [i, char] of Array.from(selector).entries()) {
Copy link
Member Author

Choose a reason for hiding this comment

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

[note] Array.from() can make a string to an array of characters.

@ybiquitous
Copy link
Member Author

The CI is now all green. ✅

@jeddy3 jeddy3 changed the title Turn on noUncheckedIndexedAccess option for TypeScript Refactor to turn on noUncheckedIndexedAccess option for TypeScript Apr 19, 2022
Copy link
Member

@jeddy3 jeddy3 left a comment

Choose a reason for hiding this comment

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

This is fantastic work, thank you!

Having this option enabled will likely catch bugs that we've fallen foul to in the past.

@ybiquitous
Copy link
Member Author

@jeddy3 Thank you for reviewing the big pull request! 😄

@ybiquitous ybiquitous merged commit b013ac0 into main Apr 19, 2022
@ybiquitous ybiquitous deleted the tsconfig-noUncheckedIndexedAccess branch April 19, 2022 08:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants