Skip to content

Incorrect constant export example #3032

Open
@takrishna

Description

@takrishna

What about all const variables? - This is unnecessary, so uppercasing should not be used for constants within a file. It should be used for exported constants however.

There are two points:

  1. Based on the above description the provided example appears incorrect
  2. Also, Since const variables can't be changed through reassignment, this example (i.e., variable name) feels odd.

Current example:

// bad
export const THING_TO_BE_CHANGED = 'should obviously not be uppercased';

Perhaps it could be:

// bad
export const some_exported_constant = 'should obviously be uppercased';

Activity

ljharb

ljharb commented on Sep 10, 2024

@ljharb
Collaborator

"constants" in this context is different than a const variable.

I'm not sure what your suggested change clears up tbh.

takrishna

takrishna commented on Sep 11, 2024

@takrishna
Author

If we read the highlighted (bold) in the quoted section above, it basically means that exported constants should to be capitalized, but then the example is right opposite (it says "should obviously not be uppercased".)

With respect to the second point around const, as per ECMASCRIPT specification it is NOT possible to change/reassign an exported const variable, but then the variable naming (in this example i.e., const THING_TO_BE_CHANGED) suggests that it is possible :)

hope this was more clear.

ljharb

ljharb commented on Sep 11, 2024

@ljharb
Collaborator

because a string with multiple words in it isn't a conceptual constant.

takrishna

takrishna commented on Sep 12, 2024

@takrishna
Author

because a string with multiple words in it isn't a conceptual constant.

irrespective of being a "conceptual" constant, it would not be possible to mutate/modify a variable declared using the "const" keyword that is assigned with a primitive as its value (in this case a "string"), hence the variable naming "THING_TO_BE_CHANGED" is not justified. It can never be changed, the JS spec simply doesn't allow it.

Here the rule is about "const" variables

What about all const variables?

If it was about "constant" variables (including say "conceptual constants") I would have hoped it to read as:

What about all constant variables?

In my opinion the rule is correct but only the example is misplaced. The rule clearly states that an exported "const" variable should be capitalized and a "const" variable within a file need NOT be capitalized. The example I am referring in this issue is "exporting" a const variable and hence should be capitalized.

In my humble view, constants could very well be made of multiple words, e.g., official country names are perfect examples:
const NETHERLANDS = 'The Netherlands';
const USA = 'United States of America;

takrishna

takrishna commented on Sep 13, 2024

@takrishna
Author

Perhaps a more appropriate example could be
//bad export const MY_EXPORTED_FUNCTION = ()>return 'hello function';

Instead of choosing a string or primitive assignment, coz it confuses.

ljharb

ljharb commented on Sep 13, 2024

@ljharb
Collaborator

True, that would make more sense.

linked a pull request that will close this issue on Dec 16, 2024
Shlokmonster

Shlokmonster commented on Feb 16, 2025

@Shlokmonster

Hey! I’d love to work on this issue. Can you assign it to me?

ljharb

ljharb commented on Feb 17, 2025

@ljharb
Collaborator

@Shlokmonster no, nothing in open source is assigned to people - you just make a PR if you want to work on it. However, note #3072 already exists, so if you have changes to that, instead of opening a PR, please comment on it with a link to your branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @ljharb@takrishna@Shlokmonster

      Issue actions

        Incorrect constant export example · Issue #3032 · airbnb/javascript