Skip to content

Commit

Permalink
Add support for Flow
Browse files Browse the repository at this point in the history
Fix #1216
  • Loading branch information
Golmote committed Nov 11, 2017
1 parent 9969466 commit d27b70d
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 2 deletions.
5 changes: 5 additions & 0 deletions components.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ var components = {
"require": "clike",
"owner": "simonreynolds7"
},
"flow": {
"title": "Flow",
"require": "javascript",
"owner": "Golmote"
},
"fortran": {
"title": "Fortran",
"owner": "Golmote"
Expand Down
34 changes: 34 additions & 0 deletions components/prism-flow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(function (Prism) {
Prism.languages.flow = Prism.languages.extend('javascript', {});

Prism.languages.insertBefore('flow', 'keyword', {
'type': [
{
pattern: /\b(?:[Nn]umber|[Ss]tring|[Bb]oolean|Function|any|mixed|null|void)\b/,
alias: 'tag'
}
]
});
Prism.languages.flow['function-variable'].pattern = /[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*(?=\s*=\s*(?:function\b|(?:\([^()]*\)(?:\s*:\s*\w+)?|[_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)\s*=>))/i;

Prism.languages.insertBefore('flow', 'operator', {
'flow-punctuation': {
pattern: /\{\||\|\}/,
alias: 'punctuation'
}
});

if (Prism.util.type(Prism.languages.flow.keyword) !== 'Array') {
Prism.languages.flow.keyword = [Prism.languages.flow.keyword];
}
Prism.languages.flow.keyword.unshift(
{
pattern: /(^|[^$]\b)(?:type|opaque|declare|Class)\b(?!\$)/,
lookbehind: true
},
{
pattern: /(^|[^$]\B)\$(?:await|Diff|Exact|Keys|ObjMap|PropertyType|Shape|Record|Supertype|Subtype|Enum)\b(?!\$)/,
lookbehind: true
}
);
}(Prism));
1 change: 1 addition & 0 deletions components/prism-flow.min.js

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

21 changes: 21 additions & 0 deletions examples/prism-flow.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<h1>Flow</h1>
<p>To use this language, use the class "language-flow".</p>

<h2>Primitive types</h2>
<pre><code>function method(x: number, y: string, z: boolean) {}
function stringifyBasicValue(value: string | number) {}
function add(one: any, two: any): number {
return one + two;
}

const bar: number = 2;
var barVar: number = 2;
let barLet: number = 2;
let isOneOf: number | boolean | string = foo;</code></pre>

<h2>Keywords</h2>
<pre><code>type UnionAlias = 1 | 2 | 3;
opaque type ID = string;
declare opaque type PositiveNumber: number;
type Country = $Keys&lt;typeof countries>;
type RequiredProps = $Diff&lt;Props, DefaultProps>;</code></pre>
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
}

// The dependencies map is built automatically with gulp
var lang_dependencies = /*languages_placeholder[*/{"javascript":"clike","actionscript":"javascript","arduino":"cpp","aspnet":"markup","bison":"c","c":"clike","csharp":"clike","cpp":"c","coffeescript":"javascript","crystal":"ruby","css-extras":"css","d":"clike","dart":"clike","django":"markup","fsharp":"clike","glsl":"clike","go":"clike","groovy":"clike","haml":"ruby","handlebars":"markup","haxe":"clike","java":"clike","jolie":"clike","kotlin":"clike","less":"css","markdown":"markup","n4js":"javascript","nginx":"clike","objectivec":"c","opencl":"cpp","parser":"markup","php":"clike","php-extras":"php","processing":"clike","protobuf":"clike","pug":"javascript","qore":"clike","jsx":["markup","javascript"],"reason":"clike","ruby":"clike","sass":"css","scss":"css","scala":"java","smarty":"markup","swift":"clike","textile":"markup","twig":"markup","typescript":"javascript","vbnet":"basic","wiki":"markup"}/*]*/;
var lang_dependencies = /*languages_placeholder[*/{"javascript":"clike","actionscript":"javascript","arduino":"cpp","aspnet":"markup","bison":"c","c":"clike","csharp":"clike","cpp":"c","coffeescript":"javascript","crystal":"ruby","css-extras":"css","d":"clike","dart":"clike","django":"markup","fsharp":"clike","flow":"javascript","glsl":"clike","go":"clike","groovy":"clike","haml":"ruby","handlebars":"markup","haxe":"clike","java":"clike","jolie":"clike","kotlin":"clike","less":"css","markdown":"markup","n4js":"javascript","nginx":"clike","objectivec":"c","opencl":"cpp","parser":"markup","php":"clike","php-extras":"php","processing":"clike","protobuf":"clike","pug":"javascript","qore":"clike","jsx":["markup","javascript"],"reason":"clike","ruby":"clike","sass":"css","scss":"css","scala":"java","smarty":"markup","swift":"clike","textile":"markup","twig":"markup","typescript":"javascript","vbnet":"basic","wiki":"markup"}/*]*/;

var lang_data = {};

Expand Down
2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.min.js

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

12 changes: 12 additions & 0 deletions tests/languages/flow/flow-punctuation_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{| foo : string |}

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

[
["flow-punctuation", "{|"], " foo ", ["punctuation", ":"],
["type", "string"], ["flow-punctuation", "|}"]
]

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

Checks for Flow specific punctuation.
20 changes: 20 additions & 0 deletions tests/languages/flow/function-variable_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
foo = (a: number) : number => {}
bar = () : string => {}

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

[
["function-variable", "foo"], ["operator", "="],
["punctuation", "("], "a", ["punctuation", ":"],
["type", "number"], ["punctuation", ")"],
["punctuation", ":"], ["type", "number"],
["operator", "=>"], ["punctuation", "{"], ["punctuation", "}"],
["function-variable", "bar"], ["operator", "="],
["punctuation", "("], ["punctuation", ")"],
["punctuation", ":"], ["type", "string"],
["operator", "=>"], ["punctuation", "{"], ["punctuation", "}"]
]

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

Checks for function variables containing types.
39 changes: 39 additions & 0 deletions tests/languages/flow/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
type
opaque
declare
Class
$await
$Diff
$Exact
$Keys
$ObjMap
$PropertyType
$Shape
$Record
$Supertype
$Subtype
$Enum

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

[
["keyword", "type"],
["keyword", "opaque"],
["keyword", "declare"],
["keyword", "Class"],
["keyword", "$await"],
["keyword", "$Diff"],
["keyword", "$Exact"],
["keyword", "$Keys"],
["keyword", "$ObjMap"],
["keyword", "$PropertyType"],
["keyword", "$Shape"],
["keyword", "$Record"],
["keyword", "$Supertype"],
["keyword", "$Subtype"],
["keyword", "$Enum"]
]

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

Checks for keywords.
31 changes: 31 additions & 0 deletions tests/languages/flow/type_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Number
number
String
string
Boolean
boolean
Function
any
mixed
null
void

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

[
["type", "Number"],
["type", "number"],
["type", "String"],
["type", "string"],
["type", "Boolean"],
["type", "boolean"],
["type", "Function"],
["type", "any"],
["type", "mixed"],
["type", "null"],
["type", "void"]
]

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

Checks for types.

0 comments on commit d27b70d

Please sign in to comment.