diff --git a/lib/linguist/heuristics.rb b/lib/linguist/heuristics.rb index c78a4e15..b14d841b 100644 --- a/lib/linguist/heuristics.rb +++ b/lib/linguist/heuristics.rb @@ -127,6 +127,14 @@ module Linguist Language["ECL"] end end + + disambiguate ".es" do |data| + if /^\s*(?:%%|main\s*\(.*?\)\s*->)/.match(data) + Language["Erlang"] + elsif /(?:\/\/|("|')use strict\1|export\s+default\s|\/\*.*?\*\/)/m.match(data) + Language["JavaScript"] + end + end disambiguate ".for", ".f" do |data| if /^: /.match(data) diff --git a/lib/linguist/languages.yml b/lib/linguist/languages.yml index e84e16f7..d2f2259a 100755 --- a/lib/linguist/languages.yml +++ b/lib/linguist/languages.yml @@ -1694,6 +1694,7 @@ JavaScript: - .js - ._js - .bones + - .es - .es6 - .frag - .gs diff --git a/samples/Erlang/170-os-daemons.es b/samples/Erlang/170-os-daemons.es new file mode 100755 index 00000000..87dbbc24 --- /dev/null +++ b/samples/Erlang/170-os-daemons.es @@ -0,0 +1,26 @@ +#! /usr/bin/env escript + +% Licensed under the Apache License, Version 2.0 (the "License"); you may not +% use this file except in compliance with the License. You may obtain a copy of +% the License at +% +% http://www.apache.org/licenses/LICENSE-2.0 +% +% Unless required by applicable law or agreed to in writing, software +% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +% License for the specific language governing permissions and limitations under +% the License. + +loop() -> + loop(io:read("")). + +loop({ok, _}) -> + loop(io:read("")); +loop(eof) -> + stop; +loop({error, Reason}) -> + throw({error, Reason}). + +main([]) -> + loop(). diff --git a/samples/Erlang/release b/samples/Erlang/release old mode 100644 new mode 100755 index 9b06a6dd..3f44c3fe --- a/samples/Erlang/release +++ b/samples/Erlang/release @@ -119,4 +119,3 @@ execute_overlay([{copy, In, Out} | Rest], Vars, BaseDir, TargetDir) -> exit_code(ExitCode) -> erlang:halt(ExitCode, [{flush, true}]). - diff --git a/samples/Erlang/single-context.es b/samples/Erlang/single-context.es new file mode 100755 index 00000000..72c73f2f --- /dev/null +++ b/samples/Erlang/single-context.es @@ -0,0 +1,104 @@ +#! /usr/bin/env escript +% This file is part of Emonk released under the MIT license. +% See the LICENSE file for more information. + +main([]) -> + start(64, 1000); +main([N]) -> + start(list_to_integer(N), 1000); +main([N, M]) -> + start(list_to_integer(N), list_to_integer(M)). + + +start(N, M) -> + code:add_pathz("test"), + code:add_pathz("ebin"), + {ok, Ctx} = emonk:create_ctx(), + {ok, undefined} = emonk:eval(Ctx, js()), + run(Ctx, N, M), + wait(N). + +run(_, 0, _) -> + ok; +run(Ctx, N, M) -> + Self = self(), + Pid = spawn(fun() -> do_js(Self, Ctx, M) end), + io:format("Spawned: ~p~n", [Pid]), + run(Ctx, N-1, M). + +wait(0) -> + ok; +wait(N) -> + receive + {finished, Pid} -> ok + end, + io:format("Finished: ~p~n", [Pid]), + wait(N-1). + +do_js(Parent, _, 0) -> + Parent ! {finished, self()}; +do_js(Parent, Ctx, M) -> + io:format("Running: ~p~n", [M]), + Test = random_test(), + {ok, [Resp]} = emonk:call(Ctx, <<"f">>, [Test]), + Sorted = sort(Resp), + true = Test == Sorted, + do_js(Parent, Ctx, M-1). + +js() -> + <<"var f = function(x) {return [x];};">>. + +random_test() -> + Tests = [ + null, + true, + false, + 1, + -1, + 3.1416, + -3.1416, + 12.0e10, + 1.234E+10, + -1.234E-10, + 10.0, + 123.456, + 10.0, + <<"foo">>, + <<"foo", 5, "bar">>, + <<"">>, + <<"\n\n\n">>, + <<"\" \b\f\r\n\t\"">>, + {[]}, + {[{<<"foo">>, <<"bar">>}]}, + {[{<<"foo">>, <<"bar">>}, {<<"baz">>, 123}]}, + [], + [[]], + [1, <<"foo">>], + {[{<<"foo">>, [123]}]}, + {[{<<"foo">>, [1, 2, 3]}]}, + {[{<<"foo">>, {[{<<"bar">>, true}]}}]}, + {[ + {<<"foo">>, []}, + {<<"bar">>, {[{<<"baz">>, true}]}}, {<<"alice">>, <<"bob">>} + ]}, + [-123, <<"foo">>, {[{<<"bar">>, []}]}, null] + ], + {_, [Test | _]} = lists:split(random:uniform(length(Tests)) - 1, Tests), + sort(Test). + +sort({Props}) -> + objsort(Props, []); +sort(List) when is_list(List) -> + lstsort(List, []); +sort(Other) -> + Other. + +objsort([], Acc) -> + {lists:sort(Acc)}; +objsort([{K,V} | Rest], Acc) -> + objsort(Rest, [{K, sort(V)} | Acc]). + +lstsort([], Acc) -> + lists:reverse(Acc); +lstsort([Val | Rest], Acc) -> + lstsort(Rest, [sort(Val) | Acc]). diff --git a/samples/JavaScript/axios.es b/samples/JavaScript/axios.es new file mode 100644 index 00000000..57f1f82d --- /dev/null +++ b/samples/JavaScript/axios.es @@ -0,0 +1,13 @@ +import axios from "axios"; + +export default { + async getIndex(prefix) { + const {data} = await axios.get((prefix || "") + "/index.json"); + return data; + }, + + async getContent(path, prefix) { + const {data} = await axios.get((prefix || "") + "/" + path + ".json"); + return data; + } +} diff --git a/samples/JavaScript/index.es b/samples/JavaScript/index.es new file mode 100644 index 00000000..45396918 --- /dev/null +++ b/samples/JavaScript/index.es @@ -0,0 +1,35 @@ +import config from "../webpack.config"; +import webpackDevMiddleware from "webpack-dev-middleware"; +import webpackHot from "webpack-hot-middleware"; +import webpack from "webpack"; +import express from "express"; + +app.use(webpackDevMiddleware(compiler, { + noInfo: false, + quiet: false, + publicPath: config.output.publicPath, + hot: true, + historyApiFallback: true +})); + +app.get("/(:root).json", (req, resp) => { + resp.send(indexer.index(req.params.root)); +}); + +export default function(){ + const server = http.createServer(app); + + server.listen(3000); + + const wss = new WebSocketServer({server}); + + let id = 1; + wss.on("connection", (ws) => { + console.log("Hello", " world"); + let wsId = id++; + sessions[wsId] = ws; + ws.on("close", () => { + delete sessions[wsId] + }); + }); +}; diff --git a/test/test_heuristics.rb b/test/test_heuristics.rb index 301ca9d7..e022ce6b 100644 --- a/test/test_heuristics.rb +++ b/test/test_heuristics.rb @@ -83,6 +83,13 @@ class TestHeuristcs < Minitest::Test "ECLiPSe" => all_fixtures("ECLiPSe", "*.ecl") }) end + + def test_es_by_heuristics + assert_heuristics({ + "Erlang" => all_fixtures("Erlang", "*.es"), + "JavaScript" => all_fixtures("JavaScript", "*.es") + }) + end def test_f_by_heuristics assert_heuristics({