Skip to content

Commit

Permalink
Fixes and optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrondin committed Sep 18, 2022
1 parent 88cfd43 commit d68e2e6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ STRINGS_CID="$(docker create strings:latest)" \
&& docker rm "$STRINGS_CID" \
&& tar czvf strings.linux.tar.gz strings.linux lib

# Trying it on Ubuntu 18.04
# Trying it on Ubuntu 20.04
docker run -it --rm \
-v "$(pwd):/app" \
-v "$(realpath "$(pwd)/../group-income-simple"):/repo" \
Expand Down
7 changes: 2 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ RUN echo '=== Installing QuickJS ===' \
RUN echo '=== Installing Flow ===' \
&& git clone --branch v0.183.1 --depth 1 https://github.com/facebook/flow.git flow

RUN echo '=== Installing TypeScript ===' \
&& npm install --no-save typescript browserify

RUN echo '=== Installing Pug ===' \
&& npm install --no-save pug-lexer pug-parser pug-walk
RUN echo '=== Installing JS dependencies ===' \
&& npm install --no-save typescript browserify pug-lexer pug-parser pug-walk

COPY src src
COPY dune dune
Expand Down
7 changes: 6 additions & 1 deletion src/cli/strings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ let write_english english =
Lwt_io.with_file ~flags:write_flags ~mode:Output path_json (fun oc_json ->
let* () = Lwt.join [ Lwt_io.write oc_strings header; Lwt_io.write_char oc_json '{' ] in
let* () =
String.Table.fold english ~init:Lwt.return_unit ~f:(fun ~key ~data acc ->
(* Switch to a map to preserve order as much as possible and therefore reduce merge conflicts *)
let map =
String.Table.fold english ~init:String.Map.empty ~f:(fun ~key ~data acc ->
String.Map.set acc ~key ~data)
in
String.Map.fold map ~init:Lwt.return_unit ~f:(fun ~key ~data acc ->
let fmt_key = fmt key in
let output_strings = sprintf "/* %s */\n%s = %s;\n\n" data fmt_key fmt_key in
let output_json = json_pair fmt_key fmt_key first in
Expand Down
4 changes: 3 additions & 1 deletion src/cli/vue.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ let debug_template ~path languages template_script target =
collect_from_possible_scripts collector template_script ~on_string:(Queue.enqueue strings)
in
let buf = Buffer.create 256 in
Queue.iter strings ~f:(fun s -> bprintf buf "%s\n" s);
let deduped = Queue.fold strings ~init:String.Set.empty ~f:String.Set.add in
let* () = Lwt_io.printlf "Found %d strings:" (String.Set.length deduped) in
String.Set.iter deduped ~f:(fun s -> bprintf buf !"%{Yojson.Basic}\n" (`String s));
if not (Queue.is_empty file_errors)
then (
bprintf buf "\n❌ %s errors in %s:\n" error_kind path;
Expand Down
4 changes: 2 additions & 2 deletions src/parsing/pug.ml
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ let parser =
lift3
(fun prefix identifier contents -> { prefix; identifier; contents })
(maybe (symbols [ ":$"; ":"; "@"; "#" ]))
(identifier <* blank)
(maybe (char '=' *> blank *> pug_string))
identifier
(maybe (mlblank *> char '=' *> mlblank *> pug_string))
in

let at_least_indent indent = string (String.make indent ' ') in
Expand Down
28 changes: 19 additions & 9 deletions src/quickjs/parsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ const walk = require('pug-walk')

globalThis.extractFromPug = function (code) {
const ast = parse(lex(code))
const strings = []
const possibleScripts = []
const accStrings = {}
const accPossibleScripts = []
const addAttr = function (s) {
if (typeof s !== 'string' || s === '') { return }
const parsed = extractFromAttr(s)
if (parsed !== '') {
possibleScripts.push(parsed)
if (parsed != null && parsed !== '') {
accPossibleScripts.push(parsed)
}
}
walk(ast, function before (node, replace) {
Expand All @@ -58,22 +58,32 @@ globalThis.extractFromPug = function (code) {
if (node.name === 'i18n') {
node.block.nodes.forEach(({ type, val }) => {
if (type === 'Text' && val !== '') {
strings.push(val)
accStrings[val] = true
}
})
}
break
case 'Block':
node.nodes.forEach(({ type, val }) => {
if (type === 'Text' && val !== '') {
accPossibleScripts.push(val)
}
})
break
case 'Code':
possibleScripts.push(node.val)
accPossibleScripts.push(node.val)
break
case 'InterpolatedTag':
possibleScripts.push(node.expr)
accPossibleScripts.push(node.expr)
break
case 'Conditional':
case 'While':
possibleScripts.push(node.test)
accPossibleScripts.push(node.test)
break
}
})
return { strings, possibleScripts }
return {
strings: Object.keys(accStrings),
possibleScripts: accPossibleScripts.filter((s) => accStrings[s] == null)
}
}

0 comments on commit d68e2e6

Please sign in to comment.