Skip to content

Commit

Permalink
Added GraphQL improvements and tests (#1788)
Browse files Browse the repository at this point in the history
This makes some improvements to GraphQL and expands the existing tests.
  • Loading branch information
RunDevelopment authored Mar 10, 2019
1 parent 3e00bb9 commit b2298b1
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 23 deletions.
27 changes: 17 additions & 10 deletions components/prism-graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ Prism.languages.graphql = {
pattern: /@[a-z_]\w*/i,
alias: 'function'
},
'attr-name': /[a-z_]\w*(?=\s*(?:\([^()]*\))?:)/i,
'keyword': [
{
pattern: /(fragment\s+(?!on)[a-z_]\w*\s+|\.{3}\s*)on\b/,
lookbehind: true
},
/\b(?:query|fragment|mutation)\b/
],
'operator': /!|=|\.{3}/,
'punctuation': /[!(){}\[\]:=,]/
'attr-name': {
pattern: /[a-z_]\w*(?=\s*(?:\((?:[^()"]|"(?:\\.|[^\\"\r\n])*")*\))?:)/i,
greedy: true
},
'class-name': {
pattern: /(\b(?:enum|implements|interface|on|scalar|type|union)\s+)[a-zA-Z_]\w*/,
lookbehind: true
},
'fragment': {
pattern: /(\bfragment\s+|\.{3}\s*(?!on\b))[a-zA-Z_]\w*/,
lookbehind: true,
alias: 'function'
},
'keyword': /\b(?:enum|fragment|implements|input|interface|mutation|on|query|scalar|schema|type|union)\b/,
'operator': /[!=|]|\.{3}/,
'punctuation': /[!(){}\[\]:=,]/,
'constant': /\b(?!ID\b)[A-Z][A-Z_\d]*\b/
};
2 changes: 1 addition & 1 deletion components/prism-graphql.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/languages/graphql/attr-name_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
zuck: user(id: 4) {
name
}
foo(bar: Int = 0, baz: String = "("): [Int!]!
}

----------------------------------------------------

[
["punctuation", "{"],

["attr-name", "zuck"],
["punctuation", ":"],
" user",
Expand All @@ -19,6 +21,28 @@
["punctuation", "{"],
"\r\n\t\tname\r\n\t",
["punctuation", "}"],

["attr-name", "foo"],
["punctuation", "("],
["attr-name", "bar"],
["punctuation", ":"],
" Int ",
["operator", "="],
["number", "0"],
["punctuation", ","],
["attr-name", "baz"],
["punctuation", ":"],
" String ",
["operator", "="],
["string", "\"(\""],
["punctuation", ")"],
["punctuation", ":"],
["punctuation", "["],
"Int",
["operator", "!"],
["punctuation", "]"],
["operator", "!"],

["punctuation", "}"]
]

Expand Down
48 changes: 48 additions & 0 deletions tests/languages/graphql/class-name_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
interface Foo {}
type Foo {}
type Foo implements Bar {}
enum Foo {}
scalar Foo
union Foo
on Foo {}

----------------------------------------------------

[
["keyword", "interface"],
["class-name", "Foo"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "type"],
["class-name", "Foo"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "type"],
["class-name", "Foo"],
["keyword", "implements"],
["class-name", "Bar"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "enum"],
["class-name", "Foo"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "scalar"],
["class-name", "Foo"],

["keyword", "union"],
["class-name", "Foo"],

["keyword", "on"],
["class-name", "Foo"],
["punctuation", "{"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for class names.
39 changes: 39 additions & 0 deletions tests/languages/graphql/constant_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
enum Color {
RED
GREEN
BLUE
}

{
foo(bar: RED) {
baz
}
}

----------------------------------------------------

[
["keyword", "enum"],
["class-name", "Color"],
["punctuation", "{"],
["constant", "RED"],
["constant", "GREEN"],
["constant", "BLUE"],
["punctuation", "}"],

["punctuation", "{"],
"\n\tfoo",
["punctuation", "("],
["attr-name", "bar"],
["punctuation", ":"],
["constant", "RED"],
["punctuation", ")"],
["punctuation", "{"],
"\n\t\tbaz\n\t",
["punctuation", "}"],
["punctuation", "}"]
]

----------------------------------------------------

Checks for constants.
29 changes: 29 additions & 0 deletions tests/languages/graphql/fragment_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
...frag
}

fragment frag on FooBar {
foo
bar
}

----------------------------------------------------

[
["punctuation", "{"],
["operator", "..."],
["fragment", "frag"],
["punctuation", "}"],

["keyword", "fragment"],
["fragment", "frag"],
["keyword", "on"],
["class-name", "FooBar"],
["punctuation", "{"],
"\r\n\tfoo\r\n\tbar\r\n",
["punctuation", "}"]
]

----------------------------------------------------

Checks for fragments.
33 changes: 21 additions & 12 deletions tests/languages/graphql/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
query
enum
fragment
implements
input
interface
mutation
fragment foo on Bar
... on Foo
on
query
scalar
schema
type
union

----------------------------------------------------

[
["keyword", "query"],
["keyword", "fragment"],
["keyword", "mutation"],
["keyword", "fragment"],
" foo ",
["keyword", "on"],
" Bar\r\n",
["operator", "..."],
["keyword", "enum"],
["class-name", "fragment"],
["keyword", "implements"],
["class-name", "input"],
["keyword", "interface"],
["class-name", "mutation"],
["keyword", "on"],
" Foo"
["class-name", "query"],
["keyword", "scalar"],
["class-name", "schema"],
["keyword", "type"],
["class-name", "union"]
]

----------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions tests/languages/graphql/operator_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
= ! |
...

----------------------------------------------------

[
["operator", "="],
["operator", "!"],
["operator", "|"],
["operator", "..."]
]

----------------------------------------------------

Checks for operators.

0 comments on commit b2298b1

Please sign in to comment.