Skip to content

Commit

Permalink
Support --fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Feb 2, 2019
1 parent f84da88 commit 18b815e
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 9 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
js/flatbuffers.js
tests/error_syntax.js
tests/badly_formatted.js
5 changes: 5 additions & 0 deletions src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub struct DenoFlags {
pub types: bool,
pub prefetch: bool,
pub info: bool,
pub fmt: bool,
}

pub fn get_usage(opts: &Options) -> String {
Expand Down Expand Up @@ -115,6 +116,9 @@ fn set_recognized_flags(
if matches.opt_present("info") {
flags.info = true;
}
if matches.opt_present("fmt") {
flags.fmt = true;
}

if !matches.free.is_empty() {
rest.extend(matches.free);
Expand Down Expand Up @@ -152,6 +156,7 @@ pub fn set_flags(
opts.optflag("", "types", "Print runtime TypeScript declarations.");
opts.optflag("", "prefetch", "Prefetch the dependencies.");
opts.optflag("", "info", "Show source file related info");
opts.optflag("", "fmt", "Format code.");

let mut flags = DenoFlags::default();

Expand Down
9 changes: 7 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ fn print_err_and_exit(err: errors::RustOrJsError) {
fn main() {
log::set_logger(&LOGGER).unwrap();
let args = env::args().collect();
let (flags, rest_argv, usage_string) =
flags::set_flags(args).unwrap_or_else(|err| {
let (mut flags, mut rest_argv, usage_string) = flags::set_flags(args)
.unwrap_or_else(|err| {
eprintln!("{}", err);
std::process::exit(1)
});
Expand All @@ -80,6 +80,11 @@ fn main() {
LevelFilter::Warn
});

if flags.fmt {
rest_argv.insert(1, "https://deno.land/x/std/prettier/main.ts".to_string());
flags.allow_write = true;
}

let should_prefetch = flags.prefetch;
let should_display_info = flags.info;

Expand Down
4 changes: 4 additions & 0 deletions tests/badly_formatted.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

console.log(
"Hello World"
)
1 change: 1 addition & 0 deletions tests/badly_formatted_fixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Hello World");
34 changes: 34 additions & 0 deletions tools/fmt_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import os
import sys
from util import mkdtemp, root_path, tests_path, run, green_ok
import shutil


def fmt_test(deno_exe):
sys.stdout.write("fmt_test...")
sys.stdout.flush()
d = mkdtemp()
try:
fixed_filename = os.path.join(tests_path, "badly_formatted_fixed.js")
src = os.path.join(tests_path, "badly_formatted.js")
dst = os.path.join(d, "badly_formatted.js")
shutil.copyfile(src, dst)
# Set DENO_DIR to //js/ so we don't have to rely on an intenet
# connection to download https://deno.land/x/std/prettier/main.ts
deno_dir = os.path.join(root_path, "js")
run([deno_exe, dst, "--fmt"], merge_env={"DENO_DIR": deno_dir})
with open(fixed_filename) as f:
expected = f.read()
with open(dst) as f:
actual = f.read()
assert expected == actual
finally:
shutil.rmtree(d)
print green_ok()


if __name__ == "__main__":
fmt_test(sys.argv[1])

9 changes: 2 additions & 7 deletions tools/prefetch_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import os
import sys
from util import tests_path, run_output, build_path, executable_suffix, green_ok
import tempfile
from util import mkdtemp, tests_path, run_output, green_ok
import shutil


def prefetch_test(deno_exe):
sys.stdout.write("prefetch_test...")
sys.stdout.flush()

# On Windows, set the base directory that mkdtemp() uses explicitly. If not,
# it'll use the short (8.3) path to the temp dir, which triggers the error
# 'TS5009: Cannot find the common subdirectory path for the input files.'
temp_dir = os.environ["TEMP"] if os.name == 'nt' else None
deno_dir = tempfile.mkdtemp(dir=temp_dir)
deno_dir = mkdtemp()
try:
t = os.path.join(tests_path, "006_url_imports.ts")
output = run_output([deno_exe, "--prefetch", t],
Expand Down
2 changes: 2 additions & 0 deletions tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from benchmark_test import benchmark_test
from repl_test import repl_tests
from prefetch_test import prefetch_test
from fmt_test import fmt_test
import subprocess
import http_server

Expand Down Expand Up @@ -61,6 +62,7 @@ def main(argv):
unit_tests(deno_exe)

prefetch_test(deno_exe)
fmt_test(deno_exe)

integration_tests(deno_exe)

Expand Down
8 changes: 8 additions & 0 deletions tools/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import stat
import sys
import subprocess
import tempfile

RESET = "\x1b[0m"
FG_RED = "\x1b[31m"
Expand Down Expand Up @@ -381,3 +382,10 @@ def parse_wrk_output(output):

def platform():
return {"linux2": "linux", "darwin": "mac", "win32": "win"}[sys.platform]

def mkdtemp():
# On Windows, set the base directory that mkdtemp() uses explicitly. If not,
# it'll use the short (8.3) path to the temp dir, which triggers the error
# 'TS5009: Cannot find the common subdirectory path for the input files.'
temp_dir = os.environ["TEMP"] if os.name == 'nt' else None
return tempfile.mkdtemp(dir=temp_dir)

0 comments on commit 18b815e

Please sign in to comment.