Skip to content

Instantly share code, notes, and snippets.

@leggsimon
Created January 27, 2018 18:33
Show Gist options
  • Save leggsimon/70726168a7058fb7d538943d8544fd5e to your computer and use it in GitHub Desktop.
Save leggsimon/70726168a7058fb7d538943d8544fd5e to your computer and use it in GitHub Desktop.
ECMAScript Proposal - Question Mark character to be allowed in IdentifierNames

ECMAScript Proposal - Question Mark character to be allowed in IdentifierNames

Outline

My proposal is for the Question Mark (?) character to be allowed in an IdentifierName.

Acording to the current ES5 spec

This standard specifies specific character additions: The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.

Currently I can't think of any reason why this couldn't be ammended in a future version.

The reason I think this should be included in the spec is inspired by a convention in the Ruby language where variables or methods which end in a ? denote that the value of a variable is a boolean or that the return value of a method is a boolean.

Example:

// As a variable name
const userIsAdmin? = getUserStatus(user) === 'admin';

if (userIsAdmin?) {
	// do something
} else {
	// do other thing
}
// As a function name
function isCat?(animal) {
	return animal.type === 'cat';
}
@shicks
Copy link

shicks commented Jan 28, 2018

Here are a few issues you may want to consider:

  1. Ambiguity between ternary operator and labels:
    foo?bar:baz();
    Currently this is unambiguously a condition on foo, but if foo?bar could be an identifier, then we have a problem.
  2. Closure and TypeScript both use ? as a modifier in type annotations, which are based on identifiers. So if you see the type Foo? it becomes ambiguous whether this is an optional Foo or the identifier Foo?. This isn't exactly a dealbreaker, but it does mean you're likely to have a very difficult time getting buy-in from Microsoft or Google.

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