Skip to content
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

content script now doesn't rewrite code as refmt errors #92

Merged
merged 2 commits into from
Jun 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name" : "reason-tools",
"version": "0.0.0",
"reason" : {"react-jsx" : 2},
"bs-dependencies": ["reason-react"],
"bs-dependencies": ["reason-react", "bs-result"],
"sources": {
"dir": "src",
"subdirs": [
Expand Down
8 changes: 4 additions & 4 deletions docs/Popup.bundle.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"devDependencies": {
"bs-platform": "^2.2.1",
"bs-result": "^1.1.0",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Much more reliable.

"chrome-store-api": "^1.0.5",
"file-loader": "^1.1.5",
"generate-json-webpack-plugin": "^0.2.1",
Expand Down
17 changes: 7 additions & 10 deletions src/extension/background.re
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
open Core;

module Refmt = {
let refmt = Refmt2.refmtJS;
let refmt = Refmt2.refmt;
let parse =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use Result.map?

let parse = Result.map(((inlang, outLang, outText)) =>
  Protocol.Refmt.{ outText, inLang, outLang };

fun
| ("Failure", error) => Protocol.Error(error)
| (conversion, outText) =>
switch (conversion |> Js.String.split("to")) {
| [|inLang, outLang|] when Protocol.languageOfString(outLang) != RefmtShared.UnknownLang =>
Protocol.Ok(
| Result.Error(error) => Result.Error(error)
| Result.Ok((inLang, outLang, outText)) =>
Result.Ok(
Protocol.Refmt.{
outText,
inLang: Protocol.languageOfString(inLang),
outLang: Protocol.languageOfString(outLang)
inLang,
outLang
}
)
| _ => Protocol.Error(outText)
};
;
};

Protocol.Refmt.listen(
Expand Down
54 changes: 20 additions & 34 deletions src/extension/common/protocol.re
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
open Common;

type result('a, 'e) =
| Ok('a)
| Error('e);
open Result;
Copy link

@scull7 scull7 Jun 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the open since all the Result namespace is used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah IDK why that's there :3

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


type language = RefmtShared.language;

Expand Down Expand Up @@ -30,58 +27,47 @@ module Refmt = {
inType: codeType,
outLang: language
};
type payloadSerialized = {
outText: string,
inLang: string,
outLang: string
};
[@bs.deriving jsConverter]
type payload = {
outText: string,
inLang: language,
outLang: language
};
type response = result(payload, string);
type response = result(string, payload);
let responseToJs = x => {
switch (x) {
| Result.Ok(x) => Obj.magic(("Ok", payloadToJs(x)))
| Result.Error(x) => Obj.magic(("Error", x))
};
};
let responseFromJs:Js.Json.t=>response = x => {
switch(Obj.magic(x)) {
| ("Ok", x) => Result.Ok(payloadFromJs(Obj.magic(x)))
| (_, x) => Result.Error(Obj.magic(x))
};
};
/* Bucklescript's variant tags will be erased when serialized, so we have to manually serialize the response
*/
let serialize: result(payload, string) => (int, payloadSerialized) =
fun
| Error(error) => (0, {outText: error, inLang: "", outLang: ""})
| Ok(payload) => (
1,
{
outText: payload.outText,
inLang: stringOfLanguage(payload.inLang),
outLang: stringOfLanguage(payload.outLang)
}
);
let deserialize: ((int, payloadSerialized)) => result(payload, string) =
fun
| (0, {outText: error}) => Error(error)
| (1, payload) =>
Ok({
outText: payload.outText,
inLang: languageOfString(payload.inLang),
outLang: languageOfString(payload.outLang)
})
| _ => raise(DeserializationFail);
let send =
(
text,
~inLang=RefmtShared.UnknownLang,
~inType=RefmtShared.UnknownType,
~outLang=RefmtShared.UnknownLang,
cb
cb:(response)=>unit
) =>
Message.query(
"refmt:refmt",
{input: text |> normalizeText |> untoplevel, inLang, inType, outLang},
(response) => cb(deserialize(response))
(response) => cb(responseFromJs(response))
);
let listen: ((request, response => unit) => unit) => unit =
(cb) =>
Message.receive(
"refmt:refmt",
(request, _, respond) => cb(request, (r) => r |> serialize |> respond)
(request, _, respond) => cb(request, (r) => {
r |> responseToJs |> respond;
})
);
};

Expand Down
4 changes: 3 additions & 1 deletion src/extension/content/convert/detect.re
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ let getBlacklist = () => [
"mwhittaker.github.io/distributed-systems-ocaml/code_MorePipes.html" /* #49 */
];

let getWhitelist = () => [];
let getWhitelist = () => [
"codebad.com/~hdon/reason-tools-test.html"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this test!

];

let getSignificantUrl = () => Location.hostname ++ Location.pathname;

Expand Down
4 changes: 2 additions & 2 deletions src/extension/content/convertPage.re
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ let doListing = (mode, state, listing) => {
text,
(response) => {
switch response {
| Protocol.Error(_) => () /* TODO */
| Protocol.Ok({outText}) => Replace.replaceListing(els, outText, replace)
| Result.Error(_) => () /* TODO */
| Result.Ok({outText}) => Replace.replaceListing(els, outText, replace)
};
/* we're in an async callback, so keep track of when we're finished by keeping count */
state.remaining = state.remaining - 1;
Expand Down
4 changes: 2 additions & 2 deletions src/extension/content/overlay.re
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ let try_ = (text) =>
text,
(response) =>
switch response {
| Error(message) => showError(message)
| Ok({outText, inLang, outLang}) => showOverlay(inLang, text, outLang, outText)
| Result.Error(message) => showError(message)
| Result.Ok({outText, inLang, outLang}) => showOverlay(inLang, text, outLang, outText)
}
);

Expand Down
4 changes: 2 additions & 2 deletions src/extension/page.re
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ let refmt =
~outLang,
(error) =>
switch error {
| Protocol.Error(error) => cb(error, RefmtShared.UnknownLang, RefmtShared.UnknownLang)
| Protocol.Ok({outText, inLang, outLang}) => cb(outText, inLang, outLang)
| Result.Error(error) => cb(error, RefmtShared.UnknownLang, RefmtShared.UnknownLang)
| Result.Ok({outText, inLang, outLang}) => cb(outText, inLang, outLang)
}
);
Protocol.Storage.setLatestInput(input)
Expand Down
4 changes: 2 additions & 2 deletions src/extension/popup.re
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ let refmt =
~outLang,
(error) =>
switch error {
| Error(error) => cb(error, RefmtShared.UnknownLang, RefmtShared.UnknownLang)
| Ok({outText, inLang, outLang}) => cb(outText, inLang, outLang)
| Result.Error(error) => cb(error, RefmtShared.UnknownLang, RefmtShared.UnknownLang)
| Result.Ok({outText, inLang, outLang}) => cb(outText, inLang, outLang)
}
);
Protocol.Storage.setLatestInput(input)
Expand Down
25 changes: 12 additions & 13 deletions src/refmt/refmt2.re
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ let attempts = (inLang, inType) =>
| (UnknownLang, UnknownType) => [parseRE, parseREI, parseREOld, parseREIOld, parseML, parseMLI]
};

let optionalString = fun | Some(s:string) => s | None => "";

let refmt = (code, inLang, inType, outLang) => {
let parsersToTry = attempts(inLang, inType);
let results = List.map((parser) => parser(code), parsersToTry);
Expand Down Expand Up @@ -220,12 +222,12 @@ let refmt = (code, inLang, inType, outLang) => {
| (UnknownLang, Ast(REI(ast))) => (ML, printMLI(ast))
| (_, Error(error)) =>
switch error {
| REI(thing) => (RE, Js.Exn.message(thing) |? "")
| RE(thing) => (RE, Js.Exn.message(thing) |? "")
| ML(thing) => (ML, Js.Exn.message(thing) |? "")
| MLI(thing) => (ML, Js.Exn.message(thing) |? "")
| REO(thing) => (REO, Js.Exn.message(thing) |? "")
| REOI(thing) => (REO, Js.Exn.message(thing) |? "")
| REI(thing) => raise(Failure(Js.Exn.message(thing)|>optionalString))
| RE(thing) => raise(Failure(Js.Exn.message(thing)|>optionalString))
| ML(thing) => raise(Failure(Js.Exn.message(thing)|>optionalString))
| MLI(thing) => raise(Failure(Js.Exn.message(thing)|>optionalString))
| REO(thing) => raise(Failure(Js.Exn.message(thing)|>optionalString))
| REOI(thing) => raise(Failure(Js.Exn.message(thing)|>optionalString))
}
};
let trueIn =
Expand All @@ -238,13 +240,10 @@ let refmt = (code, inLang, inType, outLang) => {
| Ast(REOI(_)) => REO
| Error(_) => UnknownLang
};
(trueIn, trueOut, printedResult)
Result.Ok((trueIn, trueOut, printedResult))
} {
| a => (UnknownLang, UnknownLang, Obj.magic(a))
| a => {
Result.Error(a |> Printexc.to_string)
}
}
};

let refmtJS = (jsString, inLang, inType, outLang) => {
let (trueIn, trueOut, s) = refmt(jsString, inLang, inType, outLang);
(stringOfLanguage(trueIn) ++ "to" ++ stringOfLanguage(trueOut), s)
};
8 changes: 6 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,12 @@ browserify-zlib@^0.2.0:
pako "~1.0.5"

bs-platform@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/bs-platform/-/bs-platform-2.2.1.tgz#e22b82143a43a10630d7b2040aab87648b12190c"
version "2.2.3"
resolved "https://registry.yarnpkg.com/bs-platform/-/bs-platform-2.2.3.tgz#d905ae10a5f3621e6a739041dfa0b58483a2174f"

bs-result@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/bs-result/-/bs-result-1.1.0.tgz#dcc1e3ca4692ec5fa31df83bf288b5ca2d123e6f"

buffer-crc32@~0.2.3:
version "0.2.13"
Expand Down