-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix GitHub syntax highlighting #753
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
Conversation
src/language/ast.js
Outdated
@@ -44,6 +44,30 @@ export type Location = { | |||
}; | |||
|
|||
/** | |||
* Represents the different kinds of tokens in a GraphQL document. | |||
*/ | |||
export type TokenKind = '<SOF>' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
graphql-js/src/language/lexer.js
Lines 84 to 131 in da09c4e
// Each kind of token. | |
const SOF = '<SOF>'; | |
const EOF = '<EOF>'; | |
const BANG = '!'; | |
const DOLLAR = '$'; | |
const PAREN_L = '('; | |
const PAREN_R = ')'; | |
const SPREAD = '...'; | |
const COLON = ':'; | |
const EQUALS = '='; | |
const AT = '@'; | |
const BRACKET_L = '['; | |
const BRACKET_R = ']'; | |
const BRACE_L = '{'; | |
const PIPE = '|'; | |
const BRACE_R = '}'; | |
const NAME = 'Name'; | |
const INT = 'Int'; | |
const FLOAT = 'Float'; | |
const STRING = 'String'; | |
const COMMENT = 'Comment'; | |
/** | |
* An exported enum describing the different kinds of tokens that the | |
* lexer emits. | |
*/ | |
export const TokenKind = { | |
SOF, | |
EOF, | |
BANG, | |
DOLLAR, | |
PAREN_L, | |
PAREN_R, | |
SPREAD, | |
COLON, | |
EQUALS, | |
AT, | |
BRACKET_L, | |
BRACKET_R, | |
BRACE_L, | |
PIPE, | |
BRACE_R, | |
NAME, | |
INT, | |
FLOAT, | |
STRING, | |
COMMENT | |
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn’t know that existed. At a minimum we shouldn’t export TokenKind
then.
Does Flow have a way to get the type of all the values? I know that TypeScript has keyof
which will union all the string literal key types. Something like $PropertyType<T, x>
, but it returns a union of all the property types and not just one?
Assuming the property types are all string literals we could then write:
type Token = {
kind: $PropertyTypes<TokenKind>,
...
};
This would also be a reverse $Keys<T>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if there's a support for that yet - we probably need to do in a verbose way for the time being.
Another thing I noticed is that ast.js
uses Token
object solely to typecheck Location
object - we could potentially move export type Token
to lexer.js
and import the type from there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that better. I moved the Token
type as it is on master into the lexer.js
file.
This reverts commit da09c4e.
Thanks for this @calebmer. I've also found the broken highlighting in GitHub to be bothersome. Travis is complaining about a failure under Node 7 though, with a legit looking error message, although I am not sure why it would only happen in version 7. |
@wincent fixed. It was indeed a legitimate Flow error. It looks like Flow only runs on one of the Travis jobs: Line 29 in 1eb41dd
|
So this doesn't appear to fix the GitHub syntax highlighting, it merely moves the brokenness into a different file. I think this will work instead (at least, it works when I tested it in a Gist): diff --git a/src/language/ast.js b/src/language/ast.js
index 3639ec9..001900d 100644
--- a/src/language/ast.js
+++ b/src/language/ast.js
@@ -44,15 +44,11 @@ export type Location = {
};
/**
- * Represents a range of characters represented by a lexical token
- * within a Source.
+ * Token kinds, defined at the top level to avoid broken syntax
+ * highlighting on GitHub.
*/
-export type Token = {
-
- /**
- * The kind of Token.
- */
- kind: '<SOF>'
+type TokenKind
+ = '<SOF>'
| '<EOF>'
| '!'
| '$'
@@ -73,6 +69,17 @@ export type Token = {
| 'String'
| 'Comment';
+/**
+ * Represents a range of characters represented by a lexical token
+ * within a Source.
+ */
+export type Token = {
+
+ /**
+ * The kind of Token.
+ */
+ kind: Kind;
+
/**
* The character offset at which this Node begins.
*/ |
@wincent this approach was roughly my first commit: da09c4e Moved away from that because @asiandrummer mentioned the existing naming conflict with If you want we can revert back to da09c4e and then not export |
Yeah, the diff was just an example to highlight what kind of change would be need to be made to fix the highlighting problem rather than just moving it.
Yep, and you'll note that in my diff I didn't export it.
Yep, that's what I did in my diff too. I'd be happy with that or an alternative that moves stuff into |
Ok, reverted back to the original changes in order to keep this PR minimal and focused. As I mentioned before, the best solution would probably be to run a Other options for the type name are |
Thanks for this @calebmer. I think what you've done here strikes the right balance. |
I often pull up the
src/language/ast.js
file as a reference when working with a GraphQL query. However, the syntax highlighting on GitHub is broken. This PR should maintain the same behavior while fixing the syntax highlighting on GitHub with the side benefit of exportingTokenKind
separate ofToken
.A before photo is attached below.
graphql-js/src/language/ast.js
Line 76 in 2235563