Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

[no-magic-numbers] option "allow-element-access" and ignore nums w/ comments #3605

Closed
wants to merge 7 commits into from

Conversation

aervin
Copy link
Contributor

@aervin aervin commented Dec 23, 2017

PR checklist

  • Addresses an existing issue: #2721 #1883
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

Overview of change:

  • By default, the rule ignores "magic" numbers if they are on the same line as a comment.
  • Adds support for config object
  • Adds new config option allow-element-access
  • Moves allowed-numbers config option into config object

Is there anything you'd like reviewers to focus on?

The way this rule gathers the source file's comments--is it sufficient? Are there edge cases I'm forgetting? See: the generateCommentPositions method.

Copy link

@giladgray giladgray left a comment

Choose a reason for hiding this comment

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

@aervin let's make this just about "allow-element-access" and kill the implicit-comment-disabling feature because we already have explicit support for disabling rules with comments.

},
optionExamples: [true, [true, 1, 2, 3]],
optionExamples: [true, [true, 1, 2, 3], [true, {

Choose a reason for hiding this comment

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

this second example no longer matches the schema, which makes this PR a breaking change

Choose a reason for hiding this comment

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

though i see you've preserved support for the old usage below.

@@ -62,15 +89,55 @@ export class Rule extends Lint.Rules.AbstractRule {

public static DEFAULT_ALLOWED = [ -1, 0, 1 ];

/* tslint:disable no-unsafe-any */

Choose a reason for hiding this comment

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

wait why... can we not disable this rule here?

);
}

private parseOptions(args: any): Options {

Choose a reason for hiding this comment

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

from code, looks like this is args?: number[] | { [key: string]: boolean }

import * as Lint from "../index";
import { isNegativeNumberLiteral } from "../language/utils";

interface Options {
allowedNumbers: number[] | Set<string>;

Choose a reason for hiding this comment

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

Set<string> does not make sense for numbers. why is that here and can it be just number[] (or Set<number>)?

public walk(sourceFile: ts.SourceFile) {
const cb = (node: ts.Node): void => {
if (
commentPositions(this.sourceFile)

Choose a reason for hiding this comment

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

❌ strongly against this feature. instead of implicitly disabling the rule if a comment appears, users should explicitly use the existing // tslint:disable-next-line feature, which is always a comment itself!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see a big difference here:

foo = bar[1]; // some helpful info about "1"

and

// tslint:disable-next-line no-magic-numbers
foo = bar[1];

Please keep in mind that this PR is in response to issues about the strictness of no-magic-numbers.

* Used for checking when a magic number is on the same line as
* a comment. In such a case, the magic number should be ignored.
*/
function commentPositions(sourceFile: ts.SourceFile): number[] {

Choose a reason for hiding this comment

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

❌ kill this feature please.

!(
node.parent !== undefined &&
isElementAccessExpression(node.parent) &&
this.options.allowElementAccess

Choose a reason for hiding this comment

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

please pull this () out to a local variable so it's easier to follow this logic.

@@ -28,3 +34,18 @@ class A {
enum Test {
key = 1337,
}

it('should provide a link to the landing page', () => {

Choose a reason for hiding this comment

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

while it's cool to put real code here, it's also very confusing when the only thing we care about is that little index operator inside the next line.

please remove the it() wrapper and just test the array access.

@aervin
Copy link
Contributor Author

aervin commented May 1, 2018

@giladgray We can nix this PR if you like. To me, the main benefit here is the ability to add inline comments so this rule ignores the magic number. Devs may use a disable comment, but this doesn't provide any info about the number itself (and they could've added the disable comment w/ or w/o this PR...). The hope is that devs provide a comment about the magic number in question.

@giladgray
Copy link

ok cool will close. thanks for the info @aervin!

fwiw, you can totally add a comment after the disable:

// tslint:disable-next-line:no-magic-numbers the number is magic because Snape said so

@giladgray giladgray closed this May 1, 2018
@aervin
Copy link
Contributor Author

aervin commented May 1, 2018

Alright thanks @giladgray

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants