#!/usr/bin/env bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
NUM=$(unzip -l "$BASEDIR"/android/build/outputs/aar/android-debug.aar | grep -c libevent_core)

if (( NUM >= 4 )); then
    echo "Found $NUM instances of libevent_core. Looks good."
    exit 0
else
    unzip -l "$BASEDIR"/android/build/outputs/aar/android-debug.aar | grep libevent_core
    echo "Expected 4 but found $NUM libevent_core.so files. Expecting one for each architecture. See #3395 for details."
    exit 1
fi


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

if [ "$#" -ne 4 ] && [ "$#" -ne 2 ]; then
    echo "Utility for manipulating the sonar directory where certificates and connection config are stored on Android devices."
    echo "Usage: $0 pull APP_PACKAGE SOURCE DEST"
    echo "E.g:   $0 pull com.facebook.flipper.sample device.crt /tmp/device.crt"
    echo " or"
    echo "Usage: $0 ls APP_PACKAGE"
    exit 1;
fi
PACKAGE=$2
SOURCE=$3
DEST=$4
case "$1" in
  "pull")
    # run-as opens a subshell on the device, so we echo the command we want to
    # execute into that subshell.
    echo "cat files/sonar/$SOURCE" | adb shell run-as "$PACKAGE" > "$DEST"
    ;;
  "ls")
    echo "ls -la files/sonar" | adb shell run-as "$PACKAGE"
    ;;
  *)
    echo "Unrecognised command: $1"
    exit 1
    ;;
esac


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

show_help() {
  printf "Usage: runtests.sh [-hlqrv] [-d DIR] [-t TEST] [-b] FLOW_BINARY [[-f] TEST_FILTER]\n\n"
  printf "Runs Flow's tests.\n\n"
  echo "    [-b] FLOW_BINARY"
  echo "        path to Flow binary (the -b is optional)"
  echo "    [-f] TEST_FILTER"
  echo "        optional regular expression to choose test directories to run"
  echo "    -l"
  echo "        lists the tests that will be run"
  echo "    -d DIR"
  echo "        run tests in DIR/tests/"
  echo "    -t TEST"
  echo "        run the test DIR/tests/TEST, equivalent to a filter of \"^TEST$\""
  echo "    -c"
  echo "        only run check tests"
  echo "    -r"
  echo "        re-record failing tests to update expected output"
  echo "    -q"
  echo "        quiet output (hides status, just prints results)"
  echo "    -s"
  echo "        test saved state"
  echo "    -L"
  echo "        test long-lived workers"
  echo "    -v"
  echo "        verbose output (shows skipped tests)"
  echo "    -h"
  echo "        display this help and exit"
}

export IN_FLOW_TEST=1
export FLOW_LOG_LEVEL=debug
export FLOW_NODE_BINARY=${FLOW_NODE_BINARY:-${NODE_BINARY:-$(which node)}}

# find git. to skip git tests even when git is available,
# set FLOW_GIT_BINARY to empty string
if [ -n "${FLOW_GIT_BINARY-unset}" ]; then
  export FLOW_GIT_BINARY=${FLOW_GIT_BINARY:-$(which git)}
fi

OPTIND=1
record=0
saved_state=0
verbose=0
quiet=0
relative="."
check_only=0
long_lived_workers=0
export saved_state filter check_only long_lived_workers
while getopts "b:d:f:clqxrsLt:vh?" opt; do
  case "$opt" in
  b)
    FLOW="$OPTARG"
    ;;
  d)
    relative="$OPTARG"
    ;;
  f)
    filter="$OPTARG"
    ;;
  c)
    check_only=1
    ;;
  l)
    list_tests=1
    ;;
  t)
    specific_test="$OPTARG"
    ;;
  q)
    quiet=1
    ;;
  r)
    record=1
    ;;
  s)
    saved_state=1
    printf "Testing saved state by running all tests using saved state\\n"
    ;;
  L)
    long_lived_workers=1
    ;;
  v)
    verbose=1
    ;;
  h|\?)
    show_help
    exit 0
    ;;
  esac
done

shift $((OPTIND-1))

[ "$1" = "--" ] && shift

if [ -n "$specific_test" ]; then
  if [[ "$saved_state" -eq 1 ]]; then
    specific_test=$(echo $specific_test | sed 's/\(.*\)-saved-state$/\1/')
  fi
  if [[ "$long_lived_workers" -eq 1 ]]; then
    specific_test=$(echo $specific_test | sed 's/\(.*\)-long-lived-workers$/\1/')
  fi

  filter="^$specific_test$"
fi

FLOW="${FLOW:-$1}"
filter="${filter:-$2}"

if [[ "$OSTYPE" == "darwin"* ]]; then
  _canonicalize_file_path() {
    local dir file
    dir=$(dirname -- "$1")
    file=$(basename -- "$1")
    (cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file")
  }
  FLOW=$(_canonicalize_file_path "$FLOW")
else
  FLOW=$(readlink -f "$FLOW")
fi

VERSION=$("$FLOW" version --semver)

export FLOW VERSION

if [ -t 1 ]; then
  COLOR_RESET="\x1b[0m"
  COLOR_DEFAULT="\x1b[39;49;0m"
  COLOR_DEFAULT_BOLD="\x1b[39;49;1m"
  COLOR_RED_BOLD="\x1b[31;1m"
  COLOR_GREEN_BOLD="\x1b[32;1m"
  COLOR_YELLOW_BOLD="\x1b[33;1m"
  COLOR_MAGENTA_BOLD="\x1b[35;1m"
  COLOR_WHITE_ON_RED_BOLD="\x1b[37;41;1m"
else
  COLOR_RESET=""
  COLOR_DEFAULT=""
  COLOR_DEFAULT_BOLD=""
  COLOR_RED_BOLD=""
  COLOR_GREEN_BOLD=""
  COLOR_YELLOW_BOLD=""
  COLOR_MAGENTA_BOLD=""
  COLOR_WHITE_ON_RED_BOLD=""
fi
print_failure() {
    dir=$1
    name=${dir%*/}
    name=${name##*/}

    if [[ "$record" -eq 1 ]]; then
      printf "%b[âœ—] UPDATED:%b %s%b\n" \
        "$COLOR_MAGENTA_BOLD" "$COLOR_DEFAULT" "$name" "$COLOR_RESET"
    else
      printf "%b[âœ—] FAILED:%b  %s%b\n" \
        "$COLOR_RED_BOLD" "$COLOR_DEFAULT" "$name" "$COLOR_RESET"
    fi

    diff_file="${dir}${name}.diff"
    err_file="${dir}${name}.err"
    [ -f "$err_file" ] && cat "$err_file"
    if [ -f "$diff_file" ]; then
      if [ -t 1 ] ; then
        esc=$(echo -e "\x1b")
        sed \
            "s/^-/${esc}[31m-/;s/^+/${esc}[32m+/;s/^@/${esc}[35m@/;s/$/${esc}[0m/" \
            < "$diff_file"
      else
        cat "$diff_file"
      fi
    fi
    # Default expected file extension is .exp
    ext=".exp"

    if [[ "$record" -eq 1 ]]; then
      # Copy .out to .exp, replacing the current version, if present, with
      # <VERSION>, so that the .exp doesn't have to be updated on each release.
      sed 's/'"${VERSION//./\\.}"'/<VERSION>/g' "${dir}${name}.out" > "${dir}${name}${ext}"
      rm "${dir}${name}.out"
      rm -f "$err_file"
      rm "$diff_file"
    fi
}
print_error() {
    dir=$1
    name=${dir%*/}
    name=${name##*/}

    printf "%b[âœ—] ERRORED:%b %s%b\n" \
      "$COLOR_RED_BOLD" "$COLOR_DEFAULT" "$name" "$COLOR_RESET"

    out_file="${dir}${name}.out"
    [ -f "$out_file" ] && cat "$out_file"

    err_file="${dir}${name}.err"
    [ -f "$err_file" ] && printf "\n\nStderr:\n" && cat "$err_file"

    monitor_log_file="${dir}${name}.monitor_log"
    [ -f "$monitor_log_file" ] && printf "\n\nServer monitor log:\n" && cat "$monitor_log_file"

    log_file="${dir}${name}.log"
    [ -f "$log_file" ] && printf "\n\nServer log:\n" && cat "$log_file"
}
print_skip() {
    name=$1
    name=${name%*/}
    name=${name##*/}
    verbose=$2
    if [[ "$verbose" -eq 1 ]]; then
      printf "%b[-] SKIPPED:%b %s%b\n" \
        "$COLOR_YELLOW_BOLD" "$COLOR_DEFAULT" "$name" "$COLOR_RESET"
    elif [[ -t 1 ]]; then
      printf "             %*s\r" ${#name} " "
    fi
}
print_success() {
    name=$1
    name=${name%*/}
    name=${name##*/}
    printf "%b[âœ“] PASSED:%b  %s%b\n" \
      "$COLOR_GREEN_BOLD" "$COLOR_DEFAULT" "$name" "$COLOR_RESET"
}
print_run() {
    if [[ -t 1 && "$quiet" -eq 0 ]]; then
      name=$1
      name=${name%*/}
      name=${name##*/}
      printf "%b[ ] RUNNING:%b %s%b\r" \
        "$COLOR_DEFAULT_BOLD" "$COLOR_DEFAULT" "$name" "$COLOR_RESET"
    fi
}

cd "$relative" || exit 1
passed=0
failed=0
skipped=0
errored=0

# shellcheck source=fbcode/flow/scripts/lib/runtests-common.sh
. "./scripts/lib/runtests-common.sh"

if [ -z "$FLOW_MAX_WORKERS" ]; then
  export FLOW_MAX_WORKERS=2
fi

num_to_run_in_parallel=${FLOW_RUNTESTS_PARALLELISM-16}
if [[ "$quiet" -eq 0 ]]; then
  printf "Running up to %d test(s) in parallel\n" "$num_to_run_in_parallel"
fi

# Index N of results should correspond to the test at index N of dirs
dirs=(tests/*/)
results=()

if [[ "$list_tests" -eq 1 ]]; then
  for dir in "${dirs[@]}"; do
    dir=${dir%*/}
    name=${dir##*/}

    if [[ -z $filter || $name =~ $filter ]]; then

      if [[ "$saved_state" -eq 1 ]]; then
        echo "$name-saved-state"
      elif [[ "$long_lived_workers" -eq 1 ]]; then
        echo "$name-long-lived-workers"
      else
        echo "$name"
      fi
    fi
  done
  exit 0
fi

# Run all tests, num_to_run_in_parallel at a time
parallel_run_tests() {
    (
        i=0
        for dir in "${dirs[@]}"; do
            echo "$(( i++ )):$dir"
        done
    ) | xargs -P"$num_to_run_in_parallel" -n1 \
          "./scripts/run-one-test"
}

# Kick off all tests, reporting results over a pipe to our fd 3
exec 3< <(parallel_run_tests)

# Print a test's result, given its index in dirs and results
print_result() {
    local test_to_print=$1
    code=${results[$test_to_print]}
    testname=${dirs[$test_to_print]}
    case $code in
      $RUNTEST_SUCCESS )
        (( passed++ ))
        print_success "$testname" ;;
      $RUNTEST_FAILURE )
        (( failed++ ))
        print_failure "$testname" ;;
      $RUNTEST_SKIP )
        (( skipped++ ))
        print_skip "$testname" "$verbose" ;;
      $RUNTEST_MISSING_FILES )
        (( errored++ ))
        print_error "$testname"
        name=${testname%*/}
        name=${name##*/}
        printf "Missing %s.exp file or .flowconfig file\n" "$name" ;;
      $RUNTEST_MISSING_ALL_OPTION )
        (( errored++ ))
        print_error "$testname"
        echo 'You are required to set either `all=true` or `all=false` in your test `.flowconfig`.' ;;
      $RUNTEST_ERROR | '')
        # '' means the parallel runner stopped before giving a result
        (( errored++ ))
        print_error "$testname" ;;
    esac
}

# Collect the results in the order the tests finish
next_test_to_print=0
print_run "${dirs[$next_test_to_print]}"
while read -ru 3 index code; do
    results[$index]=$code

    # Print the results in order in a pretty way
    while [ -n "${results[$next_test_to_print]}" ]; do
        print_result "$next_test_to_print"
        (( next_test_to_print++ ))
    done

    if (( next_test_to_print < ${#dirs[@]} )); then
        print_run "${dirs[$next_test_to_print]}"
    fi
done

while (( next_test_to_print < ${#dirs[@]} )); do
    # If the parallel runner stopped early, print the remaining tests
    # (as errors, except any where we already have a result)
    print_result "$next_test_to_print"
    (( next_test_to_print++ ))
done

if [ "$failed" -eq 0 ]; then
  FAILED_COLOR="$COLOR_DEFAULT_BOLD"
else
  FAILED_COLOR="$COLOR_WHITE_ON_RED_BOLD"
fi
if [ "$errored" -eq 0 ]; then
  ERRORED_COLOR="$COLOR_DEFAULT_BOLD"
else
  ERRORED_COLOR="$COLOR_WHITE_ON_RED_BOLD"
fi

if [[ "$quiet" -eq 0 ]]; then
  echo
  printf "%bPassed: %d, %bFailed: %d%b, Skipped: %d, %bErrored: %d%b\n" \
    "$COLOR_DEFAULT_BOLD" "$passed" \
    "$FAILED_COLOR" "$failed" \
    "$COLOR_DEFAULT_BOLD" "$skipped" \
    "$ERRORED_COLOR" "$errored" \
    "$COLOR_RESET"
fi

if [[ "$failed" -gt 0 || "$errored" -gt 0 ]]; then
  exit 1
fi


#!/bin/sh
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# Generate expected output
./flow-remove-types test/source.js > test/expected.js;

# Generate expected output with --ignore-uninitialized-fields flag
./flow-remove-types --ignore-uninitialized-fields test/source.js > test/expected-uninitialized-fields.js;

# Generate expected output with --pretty flag
./flow-remove-types --pretty test/source.js > test/expected-pretty.js;

# Test expected source maps with --pretty --sourcemaps
./flow-remove-types --pretty --sourcemaps test/source.js -d test/expected-with-maps;

# Test expected source maps with --pretty --sourcemaps inline
./flow-remove-types --pretty --sourcemaps inline test/source.js > test/expected-pretty-inlinemap.js;

# Test expected source maps with --pretty --sourcemaps inline, from stdin
./flow-remove-types --pretty --sourcemaps inline < test/source.js > test/expected-pretty-inlinemap-stdin.js;


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# Test expected output
echo "Test: flow-remove-types test/source.js"
DIFF=$(./flow-remove-types test/source.js | diff test/expected.js -);
if [ -n "$DIFF" ]; then echo "$DIFF"; exit 1; fi;

# Test expected output with --ignore-uninitialized-fields flag
echo "Test: flow-remove-types --ignore-uninitialized-fields test/source.js"
DIFF=$(./flow-remove-types --ignore-uninitialized-fields test/source.js | diff test/expected-uninitialized-fields.js -);
if [ -n "$DIFF" ]; then echo "$DIFF"; exit 1; fi;

# Test expected output with --pretty flag
echo "Test: flow-remove-types --pretty test/source.js"
DIFF=$(./flow-remove-types --pretty test/source.js | diff test/expected-pretty.js -);
if [ -n "$DIFF" ]; then echo "$DIFF"; exit 1; fi;

# Test expected source maps with --pretty --sourcemaps
echo "Test: flow-remove-types --pretty --sourcemaps test/source.js -d test/expected-with-maps"
TEST_DIR=$(
  DIR=$(dirname "${BASH_SOURCE[0]}");
  cd "$DIR" && pwd
)
DIR=$(mktemp -d)
cp -r test "$DIR"
pushd "$DIR" || exit 1 > /dev/null
"$TEST_DIR/flow-remove-types" --pretty --sourcemaps test/source.js -d test/expected-with-maps;
popd || exit 1 > /dev/null
DIFF_SOURCE=$(diff test/expected-with-maps/test/source.js "$DIR/test/expected-with-maps/test/source.js");
DIFF_MAP=$(diff test/expected-with-maps/test/source.js.map "$DIR/test/expected-with-maps/test/source.js.map");
rm -rf "$DIR"
if [ -n "$DIFF_SOURCE" ]; then echo "$DIFF_SOURCE"; exit 1; fi;
if [ -n "$DIFF_MAP" ]; then echo "$DIFF_MAP"; exit 1; fi;

# Test expected source maps with --pretty --sourcemaps inline
echo "Test: flow-remove-types --pretty --sourcemaps inline test/source.js"
DIFF=$(./flow-remove-types --pretty --sourcemaps inline test/source.js | diff test/expected-pretty-inlinemap.js -);
if [ -n "$DIFF" ]; then echo "$DIFF"; exit 1; fi;

# Test expected source maps with --pretty --sourcemaps inline, from stdin
echo "Test: flow-remove-types --pretty --sourcemaps inline < test/source.js"
DIFF=$(./flow-remove-types --pretty --sourcemaps inline < test/source.js | diff test/expected-pretty-inlinemap-stdin.js -);
if [ -n "$DIFF" ]; then echo "$DIFF"; exit 1; fi;

# Test expected output with @flow outside of comments
echo "Test: flow-remove-types test/without-flow.js"
DIFF=$(./flow-remove-types test/without-flow.js | diff test/without-flow.js -);
if [ -n "$DIFF" ]; then echo "$DIFF"; exit 1; fi;

# Test node require hook
echo "Test: node require hook"
RES=$(node -e 'require("./register");require("./test/test-node-module.js")');
if [ "$RES" != 42 ]; then echo 'Node require hook failed'; exit 1; fi;

# Test flow-node
echo "Test: flow-node"
FLOW_NODE=$(./flow-node ./test/test-node-module.js);
if [ "$FLOW_NODE" != 42 ]; then echo 'flow-node failed'; exit 1; fi;

# Test flow-node with options
echo "Test: flow-node with options"
# --no-warnings is a node flag. node doesn't count its own flags in process.argv, so we're left with
# ["node", "path/to/flow-node", "-p", "'process.argv.length'"]
FLOW_NODE_OPTS=$(./flow-node --no-warnings -p 'process.argv.length');
if [ "$FLOW_NODE_OPTS" != 4 ]; then echo 'flow-node with options failed'; exit 1; fi;
# --bogus-flag isn't a node flag, so node will error. this test ensures we're not just dropping flags
FLOW_NODE_OPTS=$(./flow-node --bogus-flag 2>&1);
if [[ "$FLOW_NODE_OPTS" != *"bad option: --bogus-flag"* ]]; then echo 'flow-node with options failed'; exit 1; fi;


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

if [ $# -ne 1 ]; then
  echo "version number is required"
  exit 1
fi

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ROOT="$DIR/.."

VERSION="${1#v}" # strip leading v if present
OPAM_VERSION="${VERSION//-/'~'}"
REMOVE_TYPES_VERSION="2${VERSION#0}" # replace leading 0 with 2: 0.123.0 -> 2.123.0

pushd "$ROOT" || exit > /dev/null

set -x
sed -i 's/^\(let version = "\)[^"]*\("\)/\1'"$VERSION"'\2/' src/common/flow_version.ml
sed -i 's/^\(version: "\)[^"]*\("\)/\1'"$OPAM_VERSION"'\2/' flowtype.opam
sed -i 's/^\(version: "\)[^"]*\("\)/\1'"$OPAM_VERSION"'\2/' flow_parser.opam
sed -i 's/\("version": "\)[^"]*\("\)/\1'"$VERSION"'\2/' packages/flow-parser-bin/package.json
sed -i 's/\("version": "\)[^"]*\("\)/\1'"$VERSION"'\2/' packages/flow-parser/package.json
sed -i 's/\("version": "\)[^"]*\("\)/\1'"$REMOVE_TYPES_VERSION"'\2/' packages/flow-remove-types/package.json
sed -i 's/\("flow-parser": "\)[^"]*\("\)/\1^'"$VERSION"'\2/' packages/flow-remove-types/package.json

popd || exit > /dev/null


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

set -e

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
WEBSITE=$(realpath "$DIR/../website")

# We need flow.js to exist, so let's build it
(cd "$DIR/../" && make deps && make && make deps-js && make js)

FLOW_OUT_DIR=$(realpath "$DIR/../bin")

pushd "$WEBSITE" > /dev/null

# Symlink flow.js and the flowlibs into place directly in the static folder
ln -sf "$FLOW_OUT_DIR/flow.js" "static/flow/master/flow.js"
ln -sfn "$DIR/../lib" "static/flow/master/flowlib"

yarn install
PATH="$FLOW_OUT_DIR:$PATH" yarn start --host ::


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# shellcheck disable=SC2034

RUNTEST_SUCCESS=0
RUNTEST_FAILURE=1
RUNTEST_SKIP=2
RUNTEST_MISSING_FILES=3
RUNTEST_ERROR=4
RUNTEST_MISSING_ALL_OPTION=5


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

[ -d ~/.opam ] || opam init default 'https://github.com/fdopen/opam-repository-mingw.git#opam2' --bare --disable-sandboxing --no-setup


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# All these functions assume the $FLOW env variable is set to the Flow binary.
# Globs should be expanded when you call these functions, so things like
#
#     remove *.js
#
# should work.

remove() {
  set -e
  # shellcheck disable=SC2068
  rm $@;
  # shellcheck disable=SC2068
  assert_ok "$FLOW" force-recheck $@;
}

# This only supports the mv a.js b.js form. It doesn't support mv *.js dir/
move() {
  set -e
  # shellcheck disable=SC2068
  mv $@;
  # shellcheck disable=SC2068
  assert_ok "$FLOW" force-recheck $@;
}

# This only supports the cp a.js b.js form. It doesn't support cp *.js dir/
copy() {
    set -e
    # shellcheck disable=SC2068
    cp $@;
    # shellcheck disable=SC2068
    assert_ok "$FLOW" force-recheck $@;
}


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "Get-def imported class:\n"
assert_ok "$FLOW" get-def bar.js 5 23 --strip-root --pretty

printf "Get-def class member:\n"
assert_ok "$FLOW" get-def bar.js 7 6 --strip-root --pretty

printf "Autocomplete class property:\n"
assert_ok "$FLOW" autocomplete bar.js 7 5 --strip-root --pretty < bar.js.txt

printf "dump types:\n"
assert_ok "$FLOW" dump-types bar.js --strip-root --pretty

printf "type-at-pos:\n"
assert_ok "$FLOW" type-at-pos bar.js 7 6 --strip-root --pretty


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" status --strip-root

# We should drive merge based on the sig dependency graph, not the implementation dependency graph.
# Therefore, only `foo.js` and `sig-dependent.js` should be considered for merge. At the time that
# this test is being written, we consider all six files for merge. This results in unnecessary work.

printf "\\nChanging the exported type should produce downstream errors.\\n"
printf "We should recheck all files, but should not remerge files that are not recursive sig "
printf "dependents.\\n"
sed -i -e 's/string/number/' foo.js
assert_ok "$FLOW" force-recheck foo.js
assert_errors "$FLOW" status --strip-root
show_skipping_stats "$FLOW_LOG_FILE"

assert_ok "$FLOW" stop


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

show_obj_obj_fast_path() {
  grep "ObjT ~> ObjT fast path" $1 | tail -n 1
}

printf "\\nServer should start in verbose mode\\n"
start_flow . --verbose
assert_ok "$FLOW" status --strip-root
show_obj_obj_fast_path "$FLOW_LOG_FILE"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

mkdir tmp
cp test1.js tmp/

printf "\\nServer should start in types-first mode\\n"
start_flow .
assert_errors "$FLOW" status --strip-root

printf "\\nAdding a line should cause minimal rechecking (but preserve errors)\\n"
cp tmp1/test1.js test1.js
assert_ok "$FLOW" force-recheck --focus test1.js
assert_errors "$FLOW" status --strip-root
show_skipping_stats "$FLOW_LOG_FILE"

printf "\\nAdding a comment should cause minimal rechecking (but preserve errors)\\n"
cp tmp2/test1.js test1.js
assert_ok "$FLOW" force-recheck --focus test1.js
assert_errors "$FLOW" status --strip-root
show_skipping_stats "$FLOW_LOG_FILE"

assert_ok "$FLOW" stop

cp tmp/test1.js test1.js
rm -rf tmp


#! /bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" stop

echo
echo "exact_by_default=false (should print: {|f: number|} for exact)"
echo
assert_ok "$FLOW" codemod annotate-exports --strip-root .

echo
echo "exact_by_default=true (should print: {f: number, ...} for inexact)"
echo
mv flowconfig_exact_by_default_true .flowconfig
assert_ok "$FLOW" codemod annotate-exports --strip-root .


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" stop

echo
echo '=== Okay to print any type'
echo
assert_ok "$FLOW" codemod annotate-exports --strip-root .

echo
echo "=== Should print \$MyPreferedSuppression instead of the any type"
echo
assert_ok "$FLOW" codemod annotate-exports --strip-root --lints unclear-type=error .


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "Reading JS from stdin:\n"
assert_ok "$FLOW" ast --pretty < foo.js

printf "\nReading JSON from stdin:\n"
assert_ok "$FLOW" ast --pretty --type json < bar.json

printf "\nReading JS from a file:\n"
assert_ok "$FLOW" ast --pretty foo.js

printf "\nReading JSON from a file:\n"
assert_ok "$FLOW" ast --pretty bar.json

printf "\nUsing absolute --path with stdin:\n"
assert_ok "$FLOW" ast --pretty --path /some/path.js < foo.js

printf "\nUsing relative --path with stdin:\n"
assert_ok "$FLOW" ast --pretty --path my/relative/path.js < foo.js

printf "\nUsing --path is ignore without stdin:\n"
assert_ok "$FLOW" ast --pretty --path my/relative/path.js foo.js

printf "\nNon-ASCII characters without specifying offset style:\n"
assert_ok "$FLOW" ast --pretty nonascii.js

printf "\nNon-ASCII characters with utf8-bytes offset style:\n"
assert_ok "$FLOW" ast --pretty --offset-style utf8-bytes nonascii.js

printf "\nNon-ASCII characters with js-indices offset style:\n"
assert_ok "$FLOW" ast --pretty --offset-style js-indices nonascii.js


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# shellcheck disable=SC2094

queries_in_file autocomplete "foo_parse_fail.js" --pretty
queries_in_file autocomplete "foo.js" --pretty
queries_in_file autocomplete "bar.js" --pretty
queries_in_file autocomplete "str.js" --pretty
queries_in_file autocomplete "num.js" --pretty
queries_in_file autocomplete "bool.js" --pretty
queries_in_file autocomplete "union.js" --pretty
queries_in_file autocomplete "object_builtins.js" --pretty
queries_in_file autocomplete "function_builtins.js" --pretty
queries_in_file autocomplete "fun.js" --pretty
queries_in_file autocomplete "this.js" --pretty
queries_in_file autocomplete "this2.js" --pretty
queries_in_file autocomplete "typeparams.js" --pretty
queries_in_file autocomplete "typeparams_function.js" --pretty
queries_in_file autocomplete "generics.js" --pretty
queries_in_file autocomplete "optional.js" --pretty
queries_in_file autocomplete "jsx1.js" --pretty
queries_in_file autocomplete "jsx2.js" --pretty
queries_in_file autocomplete "jsx3.js" --pretty
queries_in_file autocomplete "jsx4.js" --pretty
queries_in_file autocomplete "jsx-attribute-member.js" --pretty
queries_in_file autocomplete "jsx-closing-tag.js" --pretty
queries_in_file autocomplete "jsx-closing-tag-unclosed.js" --pretty
queries_in_file autocomplete "jsx-function-component.js" --pretty
queries_in_file autocomplete "jsx-function-component-2.js" --pretty
queries_in_file autocomplete "jsx-function-component-3.js" --pretty
queries_in_file autocomplete "jsx-abstract-component.js" --pretty
queries_in_file autocomplete "jsx-with-children.js" --pretty
queries_in_file autocomplete "jsx-text.js" --pretty
queries_in_file autocomplete "customfun.js" --pretty
queries_in_file autocomplete "issue-1368.js" --pretty
queries_in_file autocomplete "if.js" --pretty
queries_in_file autocomplete "override.js" --pretty
queries_in_file autocomplete "member_class_property.js" --lsp
queries_in_file autocomplete "member_class_static.js" --pretty
queries_in_file autocomplete "member_interface_property.js" --lsp
queries_in_file autocomplete "member_middle.js" --pretty
queries_in_file autocomplete "opaque_types.js" --pretty
queries_in_file autocomplete "optional_chaining_middle.js" --pretty
queries_in_file autocomplete "optional_chaining_new.js" --pretty
queries_in_file autocomplete "optional_chaining_continue.js" --pretty
queries_in_file autocomplete "idx.js" --pretty
queries_in_file autocomplete "generic_alias.js" --pretty
queries_in_file autocomplete "object_literal.js" --pretty
queries_in_file autocomplete "optional_object.js" --pretty
queries_in_file autocomplete "indirect_array.js" --pretty
queries_in_file autocomplete "eval_predicate.js" --pretty
queries_in_file autocomplete "eval_destructor.js" --pretty
queries_in_file autocomplete "poly.js" --pretty
queries_in_file autocomplete "poly_no_args.js" --pretty
queries_in_file autocomplete "identifier.js" --pretty
queries_in_file autocomplete "identifier_middle.js" --pretty
queries_in_file autocomplete "identifier_import_type.js" --pretty
queries_in_file autocomplete "identifier_type.js" --pretty
queries_in_file autocomplete "super.js" --pretty
queries_in_file autocomplete "this-2.js" --pretty
queries_in_file autocomplete "pattern.js" --pretty
queries_in_file autocomplete "normalize-1.js" --pretty
queries_in_file autocomplete "normalize-2.js" --pretty
queries_in_file autocomplete "unqualified-type-annotation.js" --lsp
queries_in_file autocomplete "qualified-type-annotation.js" --pretty
queries_in_file autocomplete "qualified-type-annotation-require.js" --pretty
queries_in_file autocomplete "inherited-class-properties.js" --pretty
queries_in_file autocomplete "function-added-properties.js" --pretty
queries_in_file autocomplete "comments.js" --pretty
queries_in_file autocomplete "literal.js" --pretty
queries_in_file autocomplete "import_source.js" --pretty
queries_in_file autocomplete "import_specifiers_1.js" --pretty
queries_in_file autocomplete "import_specifiers_2.js" --pretty
queries_in_file autocomplete "object-spread-1.js" --pretty
queries_in_file autocomplete "object-spread-2.js" --pretty
queries_in_file autocomplete "object_keys_spread.js" --pretty
queries_in_file autocomplete "union-2.js" --pretty
queries_in_file autocomplete "type-alias.js" --pretty
queries_in_file autocomplete "intersection.js" --pretty
queries_in_file autocomplete "rank.js" --lsp
queries_in_file autocomplete "rank_union.js" --lsp
queries_in_file autocomplete "rank_intersection.js" --lsp
queries_in_file autocomplete "suggest_optional_chaining_1.js" --lsp
queries_in_file autocomplete "suggest_optional_chaining_2.js" --lsp
queries_in_file autocomplete "suggest_optional_chaining_3.js" --lsp
queries_in_file autocomplete "enums.js" --lsp
queries_in_file autocomplete "enum-key.js" --lsp
queries_in_file autocomplete "enum-id.js" --pretty
queries_in_file autocomplete "class_declaration_name.js" --pretty
queries_in_file autocomplete "pattern_object_key_middle.js" --pretty
queries_in_file autocomplete "jsdoc.js" --lsp
queries_in_file autocomplete "jsdoc-members-1.js" --lsp
queries_in_file autocomplete "jsdoc-members-2.js" --lsp
queries_in_file autocomplete "jsdoc-members-3.js" --lsp
queries_in_file autocomplete "jsdoc-members-4.js" --lsp
queries_in_file autocomplete "jsdoc-members-5.js" --lsp
queries_in_file autocomplete "jsdoc-members-6.js" --lsp
queries_in_file autocomplete "jsdoc-members-7.js" --lsp
queries_in_file autocomplete "jsdoc-members-8.js" --lsp
queries_in_file autocomplete "types-jsdoc.js" --lsp
queries_in_file autocomplete "qualified-types-jsdoc.js" --lsp
queries_in_file autocomplete "bracket_syntax_1.js" --lsp
queries_in_file autocomplete "bracket_syntax_2.js" --lsp
queries_in_file autocomplete "bracket_syntax_3.js" --lsp
queries_in_file autocomplete "bracket_syntax_4.js" --lsp
queries_in_file autocomplete "bracket_syntax_5.js" --lsp
queries_in_file autocomplete "bracket_syntax_6.js" --lsp
queries_in_file autocomplete "iterator.js" --lsp
queries_in_file autocomplete "bracket_syntax_needed.js" --lsp
queries_in_file autocomplete "bracket_syntax_needed_2.js" --lsp
queries_in_file autocomplete "upper_bounds_1.js" --lsp
queries_in_file autocomplete "upper_bounds_2.js" --lsp
queries_in_file autocomplete "upper_bounds_6.js" --pretty
queries_in_file autocomplete "upper_bounds_7.js" --pretty
queries_in_file autocomplete "upper_bounds_8.js" --pretty
queries_in_file autocomplete "upper_bounds_9.js" --pretty
queries_in_file autocomplete "upper_bounds_10.js" --pretty
queries_in_file autocomplete "upper_bounds_11.js" --pretty
queries_in_file autocomplete "upper_bounds_12.js" --pretty
queries_in_file autocomplete "upper_bounds_13.js" --pretty
queries_in_file autocomplete "upper_bounds_14.js" --pretty
queries_in_file autocomplete "upper_bounds_15.js" --pretty
queries_in_file autocomplete "upper_bounds_16.js" --pretty
queries_in_file autocomplete "upper_bounds_17.js" --pretty
queries_in_file autocomplete "upper_bounds_18.js" --pretty
queries_in_file autocomplete "indexed-access-1.js" --lsp
queries_in_file autocomplete "indexed-access-2.js" --lsp
queries_in_file autocomplete "indexed-access-optional-1.js" --lsp
queries_in_file autocomplete "indexed-access-optional-2.js" --lsp
queries_in_file autocomplete "typeof.js" --lsp
queries_in_file autocomplete "typeof_2.js" --lsp
queries_in_file autocomplete "recursive.js" --pretty
queries_in_file autocomplete "class_members.js" --pretty
queries_in_file autocomplete "method_signature.js" --lsp
query_at_pos autocomplete "last_empty_line.js" 5 0 --pretty
queries_in_file autocomplete "string_literal.js" --lsp
queries_in_file autocomplete "type_binding_id.js" --pretty
query_at_pos autocomplete "fixme.js" 5 4 --lsp
query_at_pos autocomplete "fixme.js" 8 5 --lsp
query_at_pos autocomplete "fixme.js" 14 8 --lsp
query_at_pos autocomplete "fixme.js" 18 12 --pretty
query_at_pos autocomplete "fixme.js" 21 10 --lsp
query_at_pos autocomplete "jsdoc_comment.js" 3 4 --lsp
query_at_pos autocomplete "jsdoc_comment.js" 8 4 --lsp
query_at_pos autocomplete "jsdoc_comment.js" 11 4 --lsp
queries_in_file autocomplete "super_member.js" --pretty
queries_in_file autocomplete "switch.js" --pretty
queries_in_file autocomplete "keywords_c.js" --pretty
queries_in_file autocomplete "keywords_f.js" --pretty


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

test_file(){
  FILE=$1
  echo "> insert-type" "$@"
  assert_ok "$FLOW" autofix insert-type --in-place "$@"
  assert_ok "$FLOW" force-recheck "$FILE"
  echo "> cat $FILE"
  cat "$FILE"
}

test_file a.js 6 15
test_file b.js 6 15 --strategy=generalize
test_file c.js 6 15 --strategy=specialize

echo "> flow status"
assert_errors "$FLOW" status --strip-root


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

mkdir tmp || rm tmp/*
cp .flowconfig tmp/.flowconfig
start_flow tmp

test_file () {
  local FILE=$1
  cp "$FILE" "tmp/$FILE"

  echo "> autofix insert-type" "$@"
  assert_ok "$FLOW" autofix insert-type --in-place "$@"
  assert_ok "$FLOW" force-recheck "$FILE"
  echo "> cat $FILE"
  cat "$FILE"
}

test_file a.js 5 20 --strategy=generalize
test_file b.js 5 20 --strategy=specialize

echo "> flow status"
assert_ok "$FLOW" status

echo "> flow status tmp"
assert_ok "$FLOW" status tmp

assert_ok "$FLOW" stop tmp


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

mkdir tmp || rm tmp/*
cp .flowconfig tmp/.flowconfig
start_flow tmp

fix_missing_local_annot() {
  FILE=$1; shift

  cp "$FILE" "tmp/$FILE"

  echo "> flow autofix missing-local-annot $FILE"
  assert_ok "$FLOW" autofix missing-local-annot --in-place "$FILE"
  assert_ok "$FLOW" force-recheck "$FILE"
  echo "> cat $FILE"
  cat "$FILE"
}

insert_type () {
  FILE=$1; shift

  cp "$FILE" "tmp/$FILE"

  while [[ $# -ge 4 ]]; do
    local a1=$1; shift;
    local a2=$1; shift;
    local a3=$1; shift;
    local a4=$1; shift;
    echo "> flow autofix insert-type $FILE $a1 $a2 $a3 $a4"
    assert_ok "$FLOW" autofix insert-type --in-place "$FILE" "$a1" "$a2" "$a3" "$a4"
  done
  assert_ok "$FLOW" force-recheck "$FILE"
  echo "> cat $FILE"
  cat "$FILE"
}

fix_missing_local_annot a.js
insert_type a.js 7 3 7 11
fix_missing_local_annot b.js
fix_missing_local_annot c.js
insert_type c.js 4 3 4 32    9 17 9 17

echo "> flow status"
assert_ok "$FLOW" status

echo "> flow status tmp"
assert_ok "$FLOW" status tmp

assert_ok "$FLOW" stop tmp


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" autofix insert-type --in-place a.js 3 20
echo "> cat a.js"
cat a.js

assert_ok "$FLOW" autofix insert-type --in-place a.js.flow 3 20
echo "> cat a.js.flow"
cat a.js.flow

assert_ok "$FLOW" force-recheck a.js a.js.flow
echo "> flow status"
assert_ok "$FLOW" status


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

mkdir tmp || rm -r tmp/*
cp -r .flowconfig lib tmp
start_flow tmp

cp a.js tmp/a.js
assert_ok "$FLOW" autofix insert-type --in-place a.js 6 18 6 23
assert_ok "$FLOW" force-recheck a.js
echo "> cat a.js"
cat a.js

echo "> flow status"
assert_ok "$FLOW" status

echo "> flow status tmp"
assert_ok "$FLOW" status tmp

assert_ok "$FLOW" stop tmp


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

mkdir tmp || rm tmp/*
cp .flowconfig tmp/.flowconfig
start_flow tmp

do_file () {
  FILE=$1; shift

  cp "$FILE" "tmp/$FILE"

  while [[ $# -ge 2 ]]; do
    local a1=$1; shift;
    local a2=$1; shift;
    echo "> flow autofix insert-type $FILE $a1 $a2"
    assert_ok "$FLOW" autofix insert-type --in-place "$FILE" "$a1" "$a2"
  done
  assert_ok "$FLOW" force-recheck "$FILE"
  echo "> cat $FILE"
  cat "$FILE"
}

do_file a.js 3 20
do_file b.js 3 25

echo "> flow status"
assert_ok "$FLOW" status
echo "> flow status tmp"
assert_ok "$FLOW" status tmp

assert_ok "$FLOW" stop tmp


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# autofix doesn't try to control the order of unions
update_in_place(){
  local FILE=$1;
  assert_ok "$FLOW" autofix insert-type --in-place "$@"
  assert_ok "$FLOW" force-recheck "$FILE"
  echo "> cat $FILE"
  cat "$FILE"
}

update_in_place any-in-union.js 12 5
update_in_place any-in-union.js 13 5
update_in_place any-in-union.js 14 5
update_in_place any-in-union.js 15 5
update_in_place any-in-union.js 16 5
update_in_place any-to-flowfixme-no-strict.js 4 20
update_in_place any-to-flowfixme-strict-local.js 4 20
update_in_place any-to-flowfixme-strict.js 4 20

echo "> flow status"
assert_ok "$FLOW" status


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" autofix insert-type --in-place a.js 5 18 5 21
echo "> cat a.js"
cat a.js
assert_ok "$FLOW" force-recheck a.js
echo "> flow status"
assert_ok "$FLOW" status --strip-root


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" autofix insert-type --in-place a.js 6 9
assert_ok "$FLOW" autofix insert-type --in-place a.js 7 9
assert_ok "$FLOW" autofix insert-type --in-place a.js 8 9
assert_ok "$FLOW" autofix insert-type --in-place a.js 9 9
echo "> cat a.js"
cat a.js
assert_ok "$FLOW" force-recheck a.js
echo "> flow status"
assert_ok "$FLOW" status


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

spec_it () {
  assert_ok "$FLOW" autofix insert-type --in-place --strategy=specialize "$@"
}

spec_it a.js 8 9
spec_it a.js 10 9
spec_it a.js 12 9
spec_it a.js 14 9
spec_it a.js 16 19
spec_it a.js 18 19
spec_it a.js 20 17
spec_it a.js 22 17
spec_it a.js 24 15
spec_it a.js 26 9
spec_it a.js 28 9
spec_it a.js 30 19
spec_it a.js 32 9
spec_it a.js 34 9
spec_it a.js 36 19
spec_it a.js 38 14
spec_it a.js 40 31
spec_it a.js 44 9

echo "> cat a.js"
cat a.js
assert_ok "$FLOW" force-recheck a.js

echo "> flow status"
assert_ok "$FLOW" status


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" autofix insert-type --in-place simplify-empty.js 3 28
echo "> cat simplify-empty.js"
cat simplify-empty.js
assert_ok "$FLOW" force-recheck simplify-empty.js
echo "> flow status"
assert_ok "$FLOW" status


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" stop

printf "\\nInit cannot be canceled\\n"
# Add a file which sleeps for 100s
cp sleep.js.ignored sleep.js

# Don't wait for init - this server is going to hang
assert_ok "$FLOW" start . \
  --all --no-flowlib \
  --file-watcher "none" \
  --log-file "$FLOW_LOG_FILE" \
  --monitor-log-file "$FLOW_MONITOR_LOG_FILE"

assert_ok "$FLOW" force-recheck sleep.js

# This will timeout
assert_exit 3 "$FLOW" status --timeout 5 --no-auto-start

rm sleep.js

assert_ok "$FLOW" stop

printf "\\n\\nRecheck can be canceled\\n"
# Wait for init
start_flow .

# Kick off a recheck which will hang for 100s
cp sleep.js.ignored sleep.js
assert_ok "$FLOW" force-recheck --no-auto-start sleep.js

# This will timeout
assert_exit 3 "$FLOW" status --timeout 5 --no-auto-start

# If we change sleep_dependent.js, the recheck will cancel. If we for some
# reason fail to rollback the transaction, sleep.js will be missing from the
# shared memory, so typechecking sleep_dependent.js will produce
#
# Internal error: uncaught exception: Utils_js.Key_not_found("LeaderHeap", ".../cancelable_rechecks/sleep.js")
#
# So this is intended to test that we ARE rolling back the transaction properly
echo " " >> sleep_dependent.js
assert_ok "$FLOW" force-recheck --no-auto-start sleep_dependent.js

# This will timeout
assert_exit 3 "$FLOW" status --timeout 5 --no-auto-start

# Removing the file and canceling the recheck will unhang the server
rm sleep.js sleep_dependent.js
assert_ok "$FLOW" force-recheck --no-auto-start sleep.js
assert_errors "$FLOW" status --no-auto-start --timeout 5


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# shellcheck disable=SC2094

printf "syntax_error.js\n"
assert_errors \
  $FLOW check-contents --strip-root syntax_error.js < syntax_error.js

printf "docblock_error.js\n"
assert_errors \
  $FLOW check-contents --strip-root docblock_error.js < docblock_error.js

printf "docblock_error_with_syntax_error.js\n"
assert_errors \
  $FLOW check-contents --strip-root docblock_error_with_syntax_error.js < docblock_error_with_syntax_error.js

printf "docblock_error_with_module_type_conflict.js\n"
assert_errors \
  $FLOW check-contents --strip-root docblock_error_with_module_type_conflict.js < docblock_error_with_module_type_conflict.js

printf "module_type_conflict.js\n"
assert_errors \
  $FLOW check-contents --strip-root module_type_conflict.js < module_type_conflict.js

printf "syntax_error.js (no filename)\n"
assert_errors \
  $FLOW check-contents --strip-root < syntax_error.js

printf "\n\nnot_flow.js\n\n"
assert_ok "$FLOW" check-contents --strip-root not_flow.js < not_flow.js

printf "\n\nnot_flow.js with --all\n\n"
assert_errors "$FLOW" check-contents --strip-root --all not_flow.js < not_flow.js

printf "\n\nunsaved_foo.js depends on bar.js which is still checked against foo.js\n\n"
assert_ok \
  $FLOW check-contents --strip-root foo.js < unsaved_foo.js

printf "\n\nbad_exports.js\n\n"
assert_errors \
  $FLOW check-contents --strip-root bad_exports.js < bad_exports.js

printf "\n\njson.json\n\n"
assert_ok \
  $FLOW check-contents --strip-root json.json < json.json


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_errors "$1" check . --all --no-flowlib --show-all-errors --include-warnings --color=always


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_errors "$1" check . --all --no-flowlib --show-all-errors --include-warnings --color=always --unicode=always


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_errors "$1" check . --all --no-flowlib --include-warnings


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_errors "$1" check . --all --no-flowlib --show-all-errors --unicode=always


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_errors "$1" check . --all --no-flowlib --show-all-errors --message-width=120


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_errors "$1" check . --all --no-flowlib --show-all-errors --message-width=40


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_errors "$FLOW" check . --strip-root --show-all-errors


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" check . --strip-root --show-all-errors


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

echo "flow config check:"
assert_exit 8 "$FLOW" config check --pretty

echo
echo "flow config check --ignore-version:"
assert_ok "$FLOW" config check --ignore-version --pretty


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

echo "Invalid flowconfig should fail"
assert_exit 8 "$FLOW" check 2>&1

echo
echo "Invalid flowconfig should pass"
assert_ok "$FLOW" check --quiet --ignore-version 2>&1

echo
echo "flow config check:"
assert_exit 8 "$FLOW" config check --pretty

echo
echo "flow config check --ignore-version:"
assert_ok "$FLOW" config check --ignore-version --pretty


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_exit 8 "$FLOW" config check 2>&1


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "\nFlowconfig should not be valid:\n";
assert_exit 8 "$FLOW" check --strip-root --no-flowlib . 2>&1


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" stop 2> /dev/null > /dev/null
for i in $(seq 1 20); do
  assert_ok "$FLOW" status 2> /dev/null > /dev/null &
done
assert_ok "$FLOW" status


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" get-def --strip-root --json ignore/test.js 3 2
assert_ok "$FLOW" type-at-pos --strip-root --json ignore/test.js 3 2
assert_ok "$FLOW" get-def --strip-root --json no_flow/test.js 3 2
assert_ok "$FLOW" type-at-pos --strip-root --json no_flow/test.js 3 2


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "coverage of declare module:\n"
assert_ok "$FLOW" coverage --color declare_module.js

printf "\nshould not crash:\n"
assert_ok "$FLOW" coverage --color crash.js

printf "\nshould terminate:\n"
assert_ok "$FLOW" coverage --color non-termination.js

printf "\nerrors because not @flow:\n"
assert_ok "$FLOW" coverage --quiet no_pragma.js 2>&1

printf "\nforces @flow:\n"
assert_ok "$FLOW" coverage --quiet --all no_pragma.js 2>&1

#
# some more detailed tests:
#

echo "-----------------------------"
echo "call.js"
echo "-----------------------------"
echo
assert_ok "$FLOW" coverage --strip-root --pretty call.js

echo "-----------------------------"
echo "coverage.js"
echo "-----------------------------"
echo
assert_ok "$FLOW" coverage --strip-root --pretty coverage.js

echo "-----------------------------"
echo "any-refinement.js"
echo "-----------------------------"
echo
assert_ok "$FLOW" coverage --strip-root --pretty any-refinement.js

echo "-----------------------------"
echo "unicode.js"
echo "-----------------------------"
echo
assert_ok "$FLOW" coverage --strip-root --pretty unicode.js
assert_ok "$FLOW" coverage --color unicode.js

"$FLOW" stop
"$FLOW" start --trust-mode=check
echo "-----------------------------"
echo "trust.js"
echo "-----------------------------"
echo
assert_ok "$FLOW" coverage --show-trust --strip-root --pretty trust.js
assert_ok "$FLOW" coverage --show-trust trust.js

"$FLOW" stop
"$FLOW" start --trust-mode=check
echo "-----------------------------"
echo "tparam.js"
echo "-----------------------------"
echo
assert_ok "$FLOW" coverage --show-trust --strip-root --pretty tparam.js
assert_ok "$FLOW" coverage --show-trust tparam.js

"$FLOW" stop
"$FLOW" start --trust-mode=check
echo "-----------------------------"
echo "tparam_import.js"
echo "-----------------------------"
echo
assert_ok "$FLOW" coverage --show-trust --strip-root --pretty tparam_import.js
assert_ok "$FLOW" coverage --show-trust tparam_import.js


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" stop

printf "\\nStart server in types-first mode\\n"
start_flow .

printf "\\nValue cycle should include both files\\n"
assert_ok "$FLOW" cycle --strip-root fileA.js

printf "\\nType cycle should be empty\\n"
assert_ok "$FLOW" cycle --strip-root --types fileA.js

assert_ok "$FLOW" stop

printf "\\n"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

OVERLAPPING_FILES="
  A.js
  ExplicitProvidesModuleSameName.js
  ImplicitProvidesModule.js
  external/_d3/min.js
  node_modules/qux/corge/lib/index.js
  node_modules/qux/docblock.js
"
DECL_FILES="$(echo "$OVERLAPPING_FILES" | sed -e "s/\.js/.js.flow/g") ExplicitProvidesModuleDifferentNameDefinitions.js.flow"
IMPL_FILES="$OVERLAPPING_FILES ExplicitProvidesModuleDifferentName.js md5.js ws/index.js ws/test/client.js"

ignore_files() {
  for file in $1; do
    mv "$file" "$file.ignored" 2>&1
  done
}

use_files() {
  for file in $1; do
    mv "$file.ignored" "$file" 2>&1
  done
}

printf "======Start off with the .js files but without the .flow file======\n"
assert_errors "$FLOW" status .
use_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck $DECL_FILES
assert_errors "$FLOW" status .
ignore_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck $DECL_FILES
assert_errors "$FLOW" status .

printf "\n\n======Start off with the .js files and the .flow file======\n"
assert_ok "$FLOW" stop .
use_files "$DECL_FILES"
start_flow .

assert_errors "$FLOW" status .
ignore_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck $DECL_FILES
assert_errors "$FLOW" status .
use_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck $DECL_FILES
assert_errors "$FLOW" status .

printf "\n\n======Start off without the .js files and with the .flow file======\n"
assert_ok "$FLOW" stop .
ignore_files "$IMPL_FILES"
start_flow .

assert_errors "$FLOW" status .
use_files "$IMPL_FILES"
assert_ok "$FLOW" force-recheck $IMPL_FILES
assert_errors "$FLOW" status .
ignore_files "$IMPL_FILES"
assert_ok "$FLOW" force-recheck $IMPL_FILES
assert_errors "$FLOW" status .

# reset
use_files "$IMPL_FILES"
ignore_files "$DECL_FILES"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

IMPL_FILES="
  A.js
  node_modules/test/Test.js
"
DECL_FILES="$(echo "$IMPL_FILES" | sed -e "s/\.js/.js.flow/g")"

ignore_files() {
  for file in $1; do
    mv "$file" "$file.ignored" 2>&1
  done
}

use_files() {
  for file in $1; do
    mv "$file.ignored" "$file" 2>&1
  done
}

printf "======Start off with the .js files but without the .flow file======\n"
assert_errors "$FLOW" status --no-auto-start --strip-root .
use_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck --no-auto-start $IMPL_FILES $DECL_FILES
assert_errors "$FLOW" status --no-auto-start --strip-root .
ignore_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck --no-auto-start $IMPL_FILES $DECL_FILES
assert_errors "$FLOW" status --no-auto-start --strip-root .

printf "\n\n======Start off with the .js files and the .flow file======\n"
assert_ok "$FLOW" stop .
use_files "$DECL_FILES"
start_flow .

assert_errors "$FLOW" status --no-auto-start --strip-root .
ignore_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck --no-auto-start $IMPL_FILES $DECL_FILES
assert_errors "$FLOW" status --no-auto-start --strip-root .
use_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck --no-auto-start $IMPL_FILES $DECL_FILES
assert_errors "$FLOW" status --no-auto-start --strip-root .

printf "\n\n======Start off without the .js files and with the .flow file======\n"
assert_ok "$FLOW" stop .
ignore_files "$IMPL_FILES"
start_flow .

assert_errors "$FLOW" status --no-auto-start --strip-root .
use_files "$IMPL_FILES"
assert_ok "$FLOW" force-recheck --no-auto-start $IMPL_FILES $DECL_FILES
assert_errors "$FLOW" status --no-auto-start --strip-root .
ignore_files "$IMPL_FILES"
assert_ok "$FLOW" force-recheck --no-auto-start $IMPL_FILES $DECL_FILES
assert_errors "$FLOW" status --no-auto-start --strip-root .

# reset
use_files "$IMPL_FILES"
ignore_files "$DECL_FILES"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

IMPL_FILES="
  A.js
  node_modules/B.js
  node_modules/package_with_dir_main/dir/index.js
  node_modules/package_with_full_main/code.js
  node_modules/package_with_no_package_json/index.js
  node_modules/package_with_partial_main/code.js
"
DECL_FILES=$(echo "$IMPL_FILES" | sed -e "s/\.js/.js.flow/g")

ignore_files() {
  for file in $1; do
    mv "$file" "$file.ignored"
  done
}

use_files() {
  for file in $1; do
    mv "$file.ignored" "$file"
  done
}

printf "======Start off with the .js files but without the .flow file======\n"
assert_errors "$FLOW" status .
use_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck $DECL_FILES
assert_errors "$FLOW" status .
ignore_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck $DECL_FILES
assert_errors "$FLOW" status .

printf "\n\n======Start off with the .js files and the .flow file======\n"
assert_ok "$FLOW" stop .
use_files "$DECL_FILES"
start_flow .

assert_errors "$FLOW" status .
ignore_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck $DECL_FILES
assert_errors "$FLOW" status .
use_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck $DECL_FILES
assert_errors "$FLOW" status .

printf "\n\n======Start off without the .js files and with the .flow file======\n"
assert_ok "$FLOW" stop .
ignore_files "$IMPL_FILES"
start_flow .

assert_errors "$FLOW" status .
use_files "$IMPL_FILES"
assert_ok "$FLOW" force-recheck $IMPL_FILES
assert_errors "$FLOW" status .
ignore_files "$IMPL_FILES"
assert_ok "$FLOW" force-recheck $IMPL_FILES
assert_errors "$FLOW" status .

printf "\n\n======Start off with neither .js files nor .flow files======\n"
assert_ok "$FLOW" stop .
ignore_files "$DECL_FILES"
start_flow .

assert_errors "$FLOW" status .
use_files "$DECL_FILES"
assert_ok "$FLOW" force-recheck $DECL_FILES
assert_errors "$FLOW" status .

# reset
use_files "$IMPL_FILES"
ignore_files "$DECL_FILES"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" stop

printf "\\n\\nStart server in types-first mode\\n"
start_flow .

printf "\\nValue dep-graph should include both edges\\n"
assert_ok "$FLOW" graph dep-graph --strip-root --out types-first-values.log
cat types-first-values.log

printf "\\n\\nType dep-graph should include one edge\\n"
assert_ok "$FLOW" graph dep-graph --strip-root --types --out types-first-types.log
cat types-first-types.log

assert_ok "$FLOW" stop

printf "\\n"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" status

# make a change to dependency.js, to populate its direct dependents cache
printf "\n(123: number);\n" >> dependency.js
assert_ok "$FLOW" force-recheck dependency.js

# delete dependent.js
rm dependent.js
assert_ok "$FLOW" force-recheck dependent.js

# make another change to dependency.js. if this hits the direct dependents cache
# it will think dependent is still there, and blow up.
printf "\n(123: number);\n" >> dependency.js
assert_ok "$FLOW" force-recheck dependency.js

assert_ok "$FLOW" status --no-auto-start


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

log_file="$FLOW_TEMP_DIR/direct_dependent_files_cache.log"

start_flow src

printf "== Initial there should be 2 errors ==\n"
assert_errors "$FLOW" status --no-auto-start src

printf "\n== Delete unrelated.js and now there is 1 error ==\n"
# Unchanged during `ResolvedRequires`
assert_ok mv src/unrelated{.js,.js.ignored}
assert_ok "$FLOW" force-recheck src/unrelated.js

assert_errors "$FLOW" status --no-auto-start src
printf "\n"

printf "\n== Restore unrelated.js and back to 2 errors ==\n"
# Unchanged during `ResolvedRequires`
assert_ok mv src/unrelated{.js.ignored,.js}
assert_ok "$FLOW" force-recheck src/unrelated.js

assert_errors "$FLOW" status --no-auto-start src
printf "\n"

printf "\n== Delete src/node_modules/dependency.js changes an error ==\n"
# Changed during `ResolvedRequires`
assert_ok mv src/node_modules/dependency{.js,.js.ignored}
assert_ok "$FLOW" force-recheck src/node_modules/dependency.js

assert_errors "$FLOW" status --no-auto-start src
printf "\n"

printf "\n== Restore src/node_modules/dependency.js change it back ==\n"
# Changed during `ResolvedRequires`
assert_ok mv src/node_modules/dependency{.js.ignored,.js}
assert_ok "$FLOW" force-recheck src/node_modules/dependency.js

assert_errors "$FLOW" status --no-auto-start src
printf "\n"

printf "\n== Remove the import from dependent.js ==\n"
# Changed during `ResolvedRequires`
assert_ok mv src/dependent{.js,.js.ignored}
assert_ok echo "// @flow" > src/dependent.js
assert_ok "$FLOW" force-recheck src/dependent.js

assert_errors "$FLOW" status --no-auto-start src
printf "\n"

printf "\n== Add the import back to dependent.js ==\n"
# Changed during `ResolvedRequires`
assert_ok mv src/dependent{.js.ignored,.js}
assert_ok "$FLOW" force-recheck src/dependent.js

assert_errors "$FLOW" status --no-auto-start src
printf "\n"

printf "\n== Adding code that doesn't import has no effect on dep graph ==\n"
# Unchanged during `ResolvedRequires`
assert_ok echo "export var foo: boolean = 123" >> src/node_modules/dependency.js
assert_ok "$FLOW" force-recheck src/node_modules/dependency.js

assert_errors "$FLOW" status --no-auto-start src
printf "\n"

"$FLOW" stop src 1> /dev/null 2>&1


#! /bin/sh
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "\nFocus a file in node_modules with a dependent also in node_modules.\n"
assert_ok "$FLOW" force-recheck --focus node_modules/a.js

printf "\nBy focusing on a.js, we should force-check main.js, and only report\n"
printf "errors there, not in node_modules.\n"
assert_errors "$FLOW" status --no-auto-start


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" status

# make a change to dependency.js, to populate its direct dependents cache
printf "\n(123: number);\n" >> dependency.js
assert_ok "$FLOW" force-recheck dependency.js

# add a parse error to dependent.js, making it untyped
printf "\nparse error" >> dependent.js
assert_ok "$FLOW" force-recheck dependent.js

# make another change to dependency.js. if this hits the direct dependents cache
# it will think dependent is still typed, and blow up.
printf "\n(123: number);\n" >> dependency.js
assert_ok "$FLOW" force-recheck dependency.js

assert_errors "$FLOW" status --no-auto-start


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

coverage() {
  echo "-----------------------------"
  echo "restart server"
  echo "-----------------------------"
  assert_ok "$FLOW" stop
  assert_ok "$FLOW" start
  echo "-----------------------------"
  echo "root"
  echo "-----------------------------"
  assert_ok "$FLOW" batch-coverage --strip-root .
  echo "-----------------------------"
  echo "folder"
  echo "-----------------------------"
  assert_ok "$FLOW" batch-coverage --strip-root folder
  echo "-----------------------------"
  echo "cycle"
  echo "-----------------------------"
  assert_ok "$FLOW" batch-coverage --strip-root cycle
  echo "-----------------------------"
  echo "match_coverage"
  echo "-----------------------------"
  assert_ok "$FLOW" batch-coverage --strip-root match_coverage
  echo "-----------------------------"
  echo "other_folder"
  echo "-----------------------------"
  assert_ok "$FLOW" batch-coverage --strip-root other_folder
  echo "-----------------------------"
  echo "folder/subfolder"
  echo "-----------------------------"
  assert_ok "$FLOW" batch-coverage --strip-root folder/subfolder
  echo "-----------------------------"
  echo "file list"
  echo "-----------------------------"
  assert_ok "$FLOW" batch-coverage --strip-root a.js folder/d.js folder/subfolder/j.js
  echo "-----------------------------"
  echo "file and dir list"
  echo "-----------------------------"
  assert_ok "$FLOW" batch-coverage --strip-root a.js folder folder/d.js
  echo "-----------------------------"
  echo "files"
  echo "-----------------------------"
  assert_ok "$FLOW" batch-coverage --strip-root --input-file files.txt
  echo "-----------------------------"
  echo "json"
  echo "-----------------------------"
  assert_ok "$FLOW" batch-coverage --strip-root --json --pretty --input-file files.txt
  echo "-----------------------------"
  echo "root info survives recheck"
  echo "-----------------------------"
  assert_ok cp a.js.ignored a.js
  assert_ok "$FLOW" force-recheck a.js
  assert_ok "$FLOW" batch-coverage --strip-root --wait-for-recheck true .
  echo "-----------------------------"
  echo "delete file"
  echo "-----------------------------"
  assert_ok rm a.js
  assert_ok "$FLOW" force-recheck a.js
  assert_ok "$FLOW" batch-coverage --strip-root --wait-for-recheck true .
  echo "-----------------------------"
  echo "parsed -> unparsed file"
  echo "-----------------------------"
  assert_ok cp non_flow.js b.js
  assert_ok "$FLOW" force-recheck b.js
  assert_ok "$FLOW" batch-coverage --strip-root --wait-for-recheck true .
}

coverage > coverage.log

cat coverage.log


#!/bin/bash -e
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "/**\r\n * @providesModule crlf\r\n */\r\n" > crlf.js
printf "/**\r\n * @providesModule foo\r\n */\r\nrequire('crlf');\r\n" > foo.js
"$FLOW" force-recheck crlf.js foo.js
"$FLOW" status
rm crlf.js foo.js


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" dump-types --strip-root --json --pretty test.js

echo "=== predicates.js ==="
assert_ok "$FLOW" dump-types --strip-root --json --pretty predicates.js

echo "=== type-destructors.js ==="
assert_ok "$FLOW" dump-types --strip-root type-destructors.js | grep '^type-destructors.js:7'

echo "=== type-destructors.js (--evaluate-type-destructors) ==="
assert_ok "$FLOW" dump-types --strip-root --evaluate-type-destructors type-destructors.js | grep '^type-destructors.js:7'


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" dump-types --strip-root --json --pretty test.js


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# This test is primarily intended to test that Types_js.ensure_parsed properly
# parses things which were not originally parsed due to saved_state

printf "==== No errors at start====\\n"
assert_ok "$FLOW" status --no-auto-start

# focusing file.js means its dependency, dependency.js, needs to be parsed
# when using lazy mode + saved state.
assert_ok "$FLOW" force-recheck --focus file.js

printf "\\n\\n==== ensure_parsed does not recheck unrelated dependent ====\\n"
# ensure_parsed included dependency.js, but does not fan out to dependency.js's
# dependents (dependent.js).
assert_ok "$FLOW" status --no-auto-start

assert_ok "$FLOW" stop
start_flow .

# modify dependency.js from what's in saved state, but don't tell the server
# about it (an unexpected change). this simulates a race condition between
# the file watcher and the server.
printf "\nexport const bar = 123;\n" >> dependency.js

printf "==== No errors after restart====\\n"
assert_ok "$FLOW" status --no-auto-start

# focusing file.js means its dependency, dependency.js, needs to be parsed
# when using lazy mode + saved state.
assert_ok "$FLOW" force-recheck --focus file.js

printf "\\n\\n==== ensure_parsed skips dependent ====\\n"
# ensure_parsed included dependency.js as a dependency update, which means
# it does not fan out to dependency.js's dependents (dependent.js)
assert_ok "$FLOW" status --no-auto-start
show_skipping_stats "$FLOW_LOG_FILE"

# now simulate the file system event for dependency.js
assert_ok "$FLOW" force-recheck dependency.js

printf "\\n\\n==== force-recheck checks dependent ====\\n"
# even though we've already partially processed the change to dependency.js
# (updated the parsing heaps and the dep graph), its signature changed so we
# need to recheck its dependents now.
assert_errors "$FLOW" status --no-auto-start
show_skipping_stats "$FLOW_LOG_FILE"

# now focus dependent.js (similar to opening it in an IDE)
assert_ok "$FLOW" force-recheck --focus dependent.js

printf "\\n\\n==== force-recheck on dependent finds errors ====\\n"
assert_errors "$FLOW" status --no-auto-start
# should check dependent.js (0 of 1 skipped)
show_skipping_stats "$FLOW_LOG_FILE"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# This test is intended to complement the ensure_parsed_saved_state test and
# demonstrate the different behavior of ensure_parsed when saved state is not
# used
printf "==== No errors at start====\\n"
assert_ok "$FLOW" status --no-auto-start

cp B.js B.js.orig
cp C.js C.js.orig

# Simulate a file watcher race condition. Let's say it notices A.js changed
# but not that B.js changed
cp B.js.with_parse_error B.js
assert_ok "$FLOW" force-recheck --focus A.js

printf "\\n\\n==== Still don't notice the error ====\\n"
# The parse error should not be noticed yet. The server does recheck B.js, but
# it will use the AST stored in shared memory. If we started up with saved state
# we wouldn't have the AST in memory and would notice the error when we try to
# read B.js from disk and parse it
assert_ok "$FLOW" status --no-auto-start

assert_ok "$FLOW" force-recheck --focus B.js

printf "\\n\\n==== Now we see the parse error ====\\n"
# The parse error should now be noticed
assert_errors "$FLOW" status --no-auto-start

# Reset the parse error
cp B.js.orig B.js
cp C.js.with_new_dependency C.js
assert_ok "$FLOW" force-recheck --focus A.js B.js # But not C.js

printf "\\n\\n==== We don't see the unknown module exception yet ====\\n"
# The error should not be noticed yet. The server does recheck C.js, but
# it will use the AST stored in shared memory. If we started up with saved state
# we wouldn't have the AST in memory and would notice the error when we try to
# read C.js from disk, parse it, and recheck the changed file
assert_ok "$FLOW" status --no-auto-start

assert_ok "$FLOW" force-recheck --focus C.js

printf "\\n\\n==== Now we see the unknown module error ====\\n"
# The error should now be noticed
assert_errors "$FLOW" status --no-auto-start


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" status --no-auto-start

# rename Dependency and make it slow, so we can interrupt the recheck
mv dependency.js dependency.js.ignored
mv dependency_slow.js.ignored dependency_slow.js

# start a very slow recheck
assert_ok "$FLOW" force-recheck dependency.js dependency_slow.js

sleep 2

# modify Dependency to cancel the recheck. we overwrite it with
# the fast version, so the test doesn't take forever.
mv dependency.js.ignored dependency_slow.js

assert_ok "$FLOW" force-recheck dependency_slow.js

assert_ok "$FLOW" status --no-auto-start


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# This test is primarily intended to test that Types_js.ensure_parsed properly
# parses things which were not originally parsed due to saved_state

printf "==== No errors at start====\\n"
assert_ok "$FLOW" status --no-auto-start

cp B.js B.js.orig
cp C.js C.js.orig

# Simulate a file watcher race condition. Let's say it notices A.js changed
# but not that B.js changed
cp B.js.with_parse_error B.js
assert_ok "$FLOW" force-recheck --focus A.js

printf "\\n\\n==== ensure_parsed notices that B has changed ====\\n"
# Focusing on A.js requires B.js to be checked, so ensure_parsed tries to
# parse it. But it notices it has changed and triggers a recheck, which finds
# the parse errors
assert_errors "$FLOW" status --no-auto-start

# Reset the parse error
cp B.js.orig B.js
assert_ok "$FLOW" stop
start_flow . --lazy-mode fs --file-watcher none

printf "==== No errors after restart====\\n"
assert_ok "$FLOW" status --no-auto-start

cp C.js.with_new_dependency C.js
cp A.js.modified A.js
# Tell the server that A.js has changed but don't tell it about C
assert_ok "$FLOW" force-recheck --focus A.js

printf "\\n\\n==== ensure_parsed notices that C has changed ====\\n"
# Focusing on A.js requires C.js to be checked, so ensure_parsed tries to
# parse it. But it notices it has changed and triggers a recheck, which finds
# the error
assert_errors "$FLOW" status --no-auto-start
show_skipping_stats "$FLOW_LOG_FILE"

# simulate the file watcher event for C
assert_ok "$FLOW" force-recheck C.js

printf "\\n\\n==== after file watcher update ====\\n"
assert_errors "$FLOW" status --no-auto-start
show_skipping_stats "$FLOW_LOG_FILE"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "==== No errors at start====\\n"
assert_ok "$FLOW" status --no-auto-start

rm B.js

# Simulate a file watcher race condition. Let's say A.js becomes focused, but the
# file watcher has not yet noticed that B.js was removed.
assert_ok "$FLOW" force-recheck --focus A.js

printf "\\n\\n==== ensure_parsed notices that B was removed ====\\n"
# Focusing on A.js requires B.js to be checked, so ensure_parsed tries to parse
# it. But it notices it has been removed and triggers a recheck, which now knows
# B is gone. With B gone, C should error.
assert_errors "$FLOW" status --no-auto-start


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "==== No errors at start====\\n"
assert_ok "$FLOW" status --no-auto-start

rm A.js

# Simulate a file watcher race condition. Let's say B.js becomes focused, but the file watcher has not yet noticed that A.js was removed.
assert_ok "$FLOW" force-recheck --focus B.js

printf "\\n\\n==== ensure_parsed notices that A was removed ====\\n"
# Focusing on A.js requires B.js to be checked, so ensure_parsed tries to parse
# it. But it notices it has been removed and triggers a recheck, which finds the
# error
assert_errors "$FLOW" status --no-auto-start --strip-root


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "\nInvoking status with no offset-style argument:\n"
assert_errors "$FLOW" status --pretty --strip-root

printf "\nInvoking status with utf8-bytes offset-style:\n"
assert_errors "$FLOW" status --pretty --strip-root --offset-style utf8-bytes

printf "\nInvoking status with js-indices offset-style:\n"
assert_errors "$FLOW" status --pretty --strip-root --offset-style js-indices

printf "\nInvoking check with no offset-style argument:\n"
assert_errors "$FLOW" check --pretty --strip-root

printf "\nInvoking check with utf8-bytes offset-style:\n"
assert_errors "$FLOW" check --pretty --strip-root --offset-style utf8-bytes

printf "\nInvoking check with js-indices offset-style:\n"
assert_errors "$FLOW" check --pretty --strip-root --offset-style js-indices

printf "\nInvoking check-contents with no offset-style argument:\n"
# check-contents returns 0 even when errors are present
assert_ok "$FLOW" check-contents --pretty < foo.js

printf "\nInvoking check-contents with utf8-bytes offset-style:\n"
assert_ok "$FLOW" check-contents --pretty --offset-style utf8-bytes < foo.js

printf "\nInvoking check-contents with js-indices offset-style:\n"
assert_ok "$FLOW" check-contents --pretty --offset-style js-indices < foo.js

# Make sure there's a newline at the end of the file
echo


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

show_eval_eval_fast_path() {
  grep "EvalT ~> EvalT fast path" $1 | tail -n 1
}

printf "\\nServer should start in verbose mode\\n"
start_flow . --verbose
assert_ok "$FLOW" status --strip-root
show_eval_eval_fast_path "$FLOW_LOG_FILE"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

show_union_union_slow_path() {
  grep "UnionT ~> UnionT slow case" $1 | tail -n 7
}

printf "\\nServer should start in verbose mode\\n"
start_flow . --verbose
assert_ok "$FLOW" status --strip-root
show_union_union_slow_path "$FLOW_LOG_FILE"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" find-module --strip-root --json ./req test.js
assert_ok "$FLOW" get-def --strip-root --json test.js 3 2
assert_ok "$FLOW" get-def --strip-root --json test.js 2 10
assert_ok "$FLOW" get-def --strip-root --json test.js 5 18


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "flow fix\n"
assert_ok "$FLOW" fix --strip-root
printf "flow fix --error-codes method-unbinding\n"
assert_ok "$FLOW" fix --error-codes method-unbinding --strip-root


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" stop
assert_ok "$FLOW" stop --flowconfig-name configure-flow

printf "\\nWe should use the all flag from configure-flow to find an error:\\n"
assert_errors "$FLOW" check --flowconfig-name configure-flow

printf "\\nWe can start a server using the configure-flow file:\\n"
start_flow . --flowconfig-name configure-flow

printf "\\nAnd still find the error with flow status!:\\n"
assert_errors "$FLOW" status --flowconfig-name configure-flow

printf "\\nWe can simultaneously start a server with the regular .flowconfig:\\n"
start_flow .

printf "\\nAnd still find the error with the original configure-flow config:\\n"
assert_errors "$FLOW" status --flowconfig-name configure-flow

printf "\\nAnd not check any non @flow files using the .flowconfig config:\\n"
assert_ok "$FLOW" status

assert_ok "$FLOW" stop
assert_ok "$FLOW" stop --flowconfig-name configure-flow

printf "\\nWe should be able to run status without start:\\n"
assert_errors "$FLOW" status --flowconfig-name configure-flow

assert_ok "$FLOW" stop --flowconfig-name configure-flow

printf "\\nWe should be able to run without any command specified:\\n"
assert_errors "$FLOW" --flowconfig-name configure-flow

assert_ok "$FLOW" stop --flowconfig-name configure-flow


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "\nShould detect that rollout groups sum to less than 100%%:\n";
assert_exit 8 "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.pct_sum_too_low" . 2>&1

printf "\nShould detect that rollout groups sum to more than 100%%:\n";
assert_exit 8 "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.pct_sum_too_high" . 2>&1

printf "\nDuplicate rollout names are banned:\n";
assert_exit 8 "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.duplicate_rollout_names" . 2>&1

printf "\nDuplicate group names are banned:\n";
assert_exit 8 "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.duplicate_group_names" . 2>&1

printf "\nRollout names may only contain [a-zA-Z0-9._]:\n";
assert_exit 8 "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.bad_rollout_name" . 2>&1

printf "\nGroup names may only contain [a-zA-Z0-9._]:\n";
assert_exit 8 "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.bad_group_name" . 2>&1

printf "\n100%% on should always be on:\n";
assert_errors "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.100_pct_short_literals" .

printf "\n0%% on should always be off:\n";
assert_ok "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.0_pct_short_literals" .

printf "\nUnknown rollout:\n"
assert_exit 8 "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.unknown_rollout" . 2>&1

printf "\nUnknown group:\n"
assert_exit 8 "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.unknown_group" . 2>&1

printf "\nDisable a .*.js ignore via rollout:\n"
assert_errors "$FLOW" check \
    --strip-root --no-flowlib --flowconfig-name ".flowconfig.0_pct_ignore" .


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "\nStop any already-running server."
assert_ok "$FLOW" stop .

printf "\nFull check:\n"
assert_errors "$FLOW" check .

printf "\nFull focus-check:\n"
assert_errors "$FLOW" check .

printf "\nFocused check on a file with local errors but no dependency or reverse dependency:\n"
assert_errors "$FLOW" focus-check test.js

printf "\nFocused check on a file with no local errors but a dependency:\n"
assert_errors "$FLOW" focus-check a.js

printf "\nFocused check on a file with no local errors but a reverse dependency:\n"
assert_errors "$FLOW" focus-check b.js

printf "\nFocused check on two files with overlapping errors:\n"
assert_errors "$FLOW" focus-check {a,b}.js

printf "\nFocused check on multiple files with disjoint sets of errors:\n"
assert_errors "$FLOW" focus-check {a,b,test}.js

printf "\nFocused check on a file and a directory:\n"
assert_errors "$FLOW" focus-check test.js dir

printf "\nFocused check on files specified in list.txt:\n"
assert_errors "$FLOW" focus-check --input-file=list.txt

printf "\nFocused check guessing root by looking at first file:\n"
assert_errors "$FLOW" focus-check other_root/other_root_file.js

printf "\nFocused check with explicit root:\n"
assert_errors "$FLOW" focus-check --root . other_root/other_root_file.js

printf "\nFocused check with syntax error:\n"
assert_errors "$FLOW" focus-check syntax.js

printf "\nDone!\n"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "\nNo errors:\n"
assert_ok "$FLOW" status --no-auto-start --strip-root

printf "\nFocusing the unparsed file shouldn't blow up:\n"
assert_ok "$FLOW" force-recheck --focus --no-auto-start unparsed.js

printf "\nNo errors:\n"
assert_ok "$FLOW" status --no-auto-start --strip-root


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "===== force-recheck of a unchanged file does nothing: =====\\n"
assert_ok "$FLOW" force-recheck a.js
assert_ok "$FLOW" status --no-auto-start

printf "\\n===== force-recheck --focus of a unchanged file does a recheck: =====\\n"
assert_ok "$FLOW" force-recheck --focus a.js
assert_errors "$FLOW" status --no-auto-start


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# shellcheck disable=SC2094

# Classes
assert_errors "$FLOW" check-contents setter_class_missing_param.js < setter_class_missing_param.js
queries_in_file type-at-pos setter_class_missing_param.js --pretty
assert_errors "$FLOW" check-contents setter_class_multiple_params.js < setter_class_multiple_params.js
queries_in_file type-at-pos setter_class_multiple_params.js --pretty

# Interfaces
assert_errors "$FLOW" check-contents setter_interface_missing_param.js < setter_interface_missing_param.js
queries_in_file type-at-pos setter_interface_missing_param.js --pretty
assert_errors "$FLOW" check-contents setter_interface_multiple_params.js < setter_interface_multiple_params.js
queries_in_file type-at-pos setter_interface_multiple_params.js --pretty

# Objects
assert_errors "$FLOW" check-contents setter_object_missing_param.js < setter_object_missing_param.js
queries_in_file type-at-pos setter_object_missing_param.js --pretty
assert_errors "$FLOW" check-contents setter_object_multiple_params.js < setter_object_multiple_params.js
queries_in_file type-at-pos setter_object_multiple_params.js --pretty

# Object types
assert_errors "$FLOW" check-contents setter_type_missing_param.js < setter_type_missing_param.js
queries_in_file type-at-pos setter_type_missing_param.js --pretty
assert_errors "$FLOW" check-contents setter_type_multiple_params.js < setter_type_multiple_params.js
queries_in_file type-at-pos setter_type_multiple_params.js --pretty


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

assert_ok "$FLOW" get-def example.js 12 10 --strip-root --json

# foo;
#  ^
printf "use of const binding = "
assert_ok "$FLOW" get-def const.js 7 4 --strip-root --pretty
printf "use of shadowed const binding = "
assert_ok "$FLOW" get-def const.js 10 2 --strip-root --pretty

printf "let binding = "
assert_ok "$FLOW" get-def let.js 3 6 --strip-root --pretty
printf "use of let binding = "
assert_ok "$FLOW" get-def let.js 7 4 --strip-root --pretty
printf "use of shadowed let binding = "
assert_ok "$FLOW" get-def let.js 10 2 --strip-root --pretty
printf "uninitialized let binding = "
assert_ok "$FLOW" get-def let.js 12 6 --strip-root --pretty
printf "conditional write of let binding = "
assert_ok "$FLOW" get-def let.js 14 4 --strip-root --pretty
printf "use of conditionally initialized let binding = "
assert_ok "$FLOW" get-def let.js 18 2 --strip-root --pretty
printf "let binding initialized to member expression = "
assert_ok "$FLOW" get-def let.js 28 8 --strip-root --pretty

# import thing from "./exports_default.js";
#           ^
printf "default import points to default export = "
assert_ok "$FLOW" get-def imports.js 3 10 --strip-root --pretty

# import thing from "./exports_default.js";
#               ^
printf "default import points to source file (from keyword) = "
assert_ok "$FLOW" get-def imports.js 3 15 --strip-root --pretty

# import thing from "./exports_default.js";
#                        ^
printf "import source points to source file = "
assert_ok "$FLOW" get-def imports.js 3 24 --strip-root --pretty

# thing;
#    ^
printf "local reference points to default import = "
assert_ok "$FLOW" get-def imports.js 4 4 --strip-root --pretty

# import {foo, bar as baz} from "./helpers/exports_named.js";
#          ^
printf "named import of const points to named export = "
assert_ok "$FLOW" get-def imports.js 6 10 --strip-root --pretty

printf "named import of let points to named export = "
assert_ok "$FLOW" get-def imports.js 6 29 --strip-root --pretty

printf "named import of var points to named export = "
assert_ok "$FLOW" get-def imports.js 6 40 --strip-root --pretty

printf "named import of function points to named export = "
assert_ok "$FLOW" get-def imports.js 6 51 --strip-root --pretty

printf "named import of class points to named export = "
assert_ok "$FLOW" get-def imports.js 6 56 --strip-root --pretty

# import {foo, bar as baz} from "./helpers/exports_named.js";
#                   ^
printf "aliased named import points to named export (as) = "
assert_ok "$FLOW" get-def imports.js 6 19 --strip-root --pretty

# import {foo, bar as baz} from "./helpers/exports_named.js";
#               ^
printf "aliased named import points to named export (remote name) = "
assert_ok "$FLOW" get-def imports.js 6 15 --strip-root --pretty

# import {foo, bar as baz} from "./helpers/exports_named.js";
#                      ^
printf "aliased named import points to named export (local name) = "
assert_ok "$FLOW" get-def imports.js 6 22 --strip-root --pretty

# foo;
#  ^
printf "local reference points to named import = "
assert_ok "$FLOW" get-def imports.js 7 2 --strip-root --pretty

# baz;
#  ^
printf "local reference points to aliased named import = "
assert_ok "$FLOW" get-def imports.js 8 2 --strip-root --pretty

# import * as things from "./helpers/exports_named.js";
#          ^
printf "namespaced import points to imported file (as) = "
assert_ok "$FLOW" get-def imports.js 10 11 --strip-root --pretty

# import * as things from "./helpers/exports_named.js";
#        ^
printf "namespaced import points to imported file (remote name) = "
assert_ok "$FLOW" get-def imports.js 10 8 --strip-root --pretty

# import * as things from "./helpers/exports_named.js";
#               ^
printf "namespaced import points to imported file (local name) = "
assert_ok "$FLOW" get-def imports.js 10 15 --strip-root --pretty

# things;
#    ^
printf "local reference points to namespaced import = "
assert_ok "$FLOW" get-def imports.js 11 4 --strip-root --pretty

# import type {DoesNotExist} from "./doesNotExist";
#                ^
printf "imports of non-existent module leads to error = "
assert_ok "$FLOW" get-def imports.js 14 17 --strip-root --pretty

# const x: DoesNotExist<number> = "foo";
#                ^
printf "use of imports of non-existent module points to import loc = "
assert_ok "$FLOW" get-def imports.js 15 17 --strip-root --pretty

# import * as test_lib from 'test_lib';
#        ^
printf "namespaced import points to declare module (remote name) = "
assert_ok "$FLOW" get-def imports.js 24 8 --strip-root --pretty

# import * as test_lib from 'test_lib';
#          ^
printf "namespaced import points to declare module (as) = "
assert_ok "$FLOW" get-def imports.js 24 11 --strip-root --pretty

# import * as test_lib from 'test_lib';
#               ^
printf "namespaced import points to declare module (local name) = "
assert_ok "$FLOW" get-def imports.js 24 15 --strip-root --pretty

# import * as test_lib from 'test_lib';
#                               ^
printf "import source points to declare module = "
assert_ok "$FLOW" get-def imports.js 24 33 --strip-root --pretty

# test_lib;
#    ^
printf "local reference points to declare module = "
assert_ok "$FLOW" get-def imports.js 25 4 --strip-root --pretty

# import typeof typeof_thing from "./helpers/exports_default.js";
#          ^
printf "typeof default import (typeof) = "
assert_ok "$FLOW" get-def imports.js 27 10 --strip-root --pretty

# import typeof typeof_thing from "./helpers/exports_default.js";
#                   ^
printf "typeof default import (local name) = "
assert_ok "$FLOW" get-def imports.js 27 19 --strip-root --pretty

# ("foo": typeof_thing);
#             ^
printf "local reference of typeof default import = "
assert_ok "$FLOW" get-def imports.js 28 13 --strip-root --pretty

# import typeof * as things_ns from "./helpers/exports_named.js";
#        ^
printf "typeof namespace import (typeof) = "
assert_ok "$FLOW" get-def imports.js 30 8 --strip-root --pretty

# import typeof * as things_ns from "./helpers/exports_named.js";
#                       ^
printf "typeof namespace import (local name) = "
assert_ok "$FLOW" get-def imports.js 30 23 --strip-root --pretty

# ({}: things_ns);
#         ^
printf "local reference of typeof namespace import = "
assert_ok "$FLOW" get-def imports.js 32 9 --strip-root --pretty

printf "class name (should be itself) = "
assert_ok "$FLOW" get-def class.js 3 8 --strip-root --pretty
printf "class property read = "
assert_ok "$FLOW" get-def class.js 13 6 --strip-root --pretty
printf "class property write = "
assert_ok "$FLOW" get-def class.js 14 6 --strip-root --pretty
printf "class methods = "
assert_ok "$FLOW" get-def class.js 15 6 --strip-root --pretty
printf "refined class properties = "
assert_ok "$FLOW" get-def class.js 18 8 --strip-root --pretty
# TODO get private properties working
printf "private class property access = "
assert_ok "$FLOW" get-def class.js 7 15 --strip-root --pretty
printf "private class property assignment = "
assert_ok "$FLOW" get-def class.js 8 15 --strip-root --pretty
printf "members of maybe types = "
assert_ok "$FLOW" get-def class.js 22 5 --strip-root --pretty
printf "members of unions with null/void = "
assert_ok "$FLOW" get-def class.js 26 5 --strip-root --pretty

printf "member of a nonexistent imported type with type parameters = "
assert_ok "$FLOW" get-def imports.js 16 4 --strip-root --pretty
printf "member of a type alias for \`any\` with type parameters = "
assert_ok "$FLOW" get-def imports.js 22 4 --strip-root --pretty

printf "member of an object type alias = "
assert_ok "$FLOW" get-def objects.js 5 4 --strip-root --pretty
printf "member of an unannotated object type = "
assert_ok "$FLOW" get-def objects.js 8 4 --strip-root --pretty

printf "optional chain initial property = "
assert_ok "$FLOW" get-def optional_chaining.js 17 6 --strip-root --pretty
printf "optional chain subsequent property = "
assert_ok "$FLOW" get-def optional_chaining.js 17 10 --strip-root --pretty
printf "optional chain initial property of null = "
assert_ok "$FLOW" get-def optional_chaining.js 18 7 --strip-root --pretty
printf "optional chain subsequent property of null = "
assert_ok "$FLOW" get-def optional_chaining.js 18 11 --strip-root --pretty

printf "shorthand destructuring = "
assert_ok "$FLOW" get-def objects.js 11 11 --strip-root --pretty
printf "non-shorthand destructuring = "
assert_ok "$FLOW" get-def objects.js 12 11 --strip-root --pretty
printf "destructuring without type alias = "
assert_ok "$FLOW" get-def objects.js 14 11 --strip-root --pretty
# This one should return itself
printf "bogus array destructuring of an object = "
assert_ok "$FLOW" get-def objects.js 15 11 --strip-root --pretty
printf "property access assigned to a variable = "
assert_ok "$FLOW" get-def objects.js 16 10 --strip-root --pretty

printf "property access on the arg to the idx callback = "
assert_ok "$FLOW" get-def idx.js 12 25 --strip-root --pretty
printf "nested property access on the arg to the idx callback = "
assert_ok "$FLOW" get-def idx.js 12 29 --strip-root --pretty

printf "global (builtin) variable = "
assert_ok "$FLOW" get-def global.js 3 3 --strip-root --pretty

printf "object destructuring key = "
assert_ok "$FLOW" get-def destructuring.js 5 9 --strip-root --pretty
printf "object destructuring binding = "
assert_ok "$FLOW" get-def destructuring.js 5 14 --strip-root --pretty

queries_in_file "get-def" "annot.js"
queries_in_file "get-def" "exports.js" --pretty
queries_in_file "get-def" "identifier.js"
queries_in_file "get-def" "literals.js"
queries_in_file "get-def" "require.js"
queries_in_file "get-def" "imports_from_cjs.js"
queries_in_file "get-def" "utilities.js"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

queries_in_file get-def "destructuring.js"
queries_in_file get-def "assignment.js"
queries_in_file get-def "require.js"
queries_in_file get-def "react.js"
queries_in_file get-def "override.js"
queries_in_file get-def "import.js"
queries_in_file get-def "module_ref.js"
queries_in_file get-def "refinements.js"
queries_in_file get-def "declare.js"
queries_in_file get-def "static.js"
queries_in_file get-def "constructor.js"
queries_in_file get-def "utilities.js"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

queries_in_file "get-def" "main.js"


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

queries_in_file "get-def" "requires.js" --strip-root --pretty
queries_in_file "get-def" "types.js" --strip-root --pretty


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

echo "nothing"
assert_exit "$EXIT_COULD_NOT_FIND_FLOWCONFIG" "$FLOW" type-at-pos 3 5 < d0.js

echo "with file"
assert_exit "$EXIT_COULD_NOT_FIND_FLOWCONFIG" "$FLOW" type-at-pos no-config/d1.js 3 5

echo "with file and config"
assert_ok "$FLOW" type-at-pos --strip-root config/no-config/d2.js 3 5

echo "with bad root directory"
assert_ok "$FLOW" type-at-pos --root ./config/no-config 3 5 < ./config/no-config/d2.js

echo "with bad root directory, and file"
assert_ok "$FLOW" type-at-pos --strip-root --root ./config/no-config ./config/no-config/d2.js 3 5

echo "with bad root directory, and file"
assert_ok "$FLOW" type-at-pos --strip-root --root ./config/no-config ./config/no-config/d2.js 3 5

echo "with file batch coverage"
assert_ok "$FLOW" batch-coverage --strip-root ./config/no-config/d2.js

echo "with bad root directory, and file batch coverage"
assert_exit "$EXIT_COULD_NOT_FIND_FLOWCONFIG" "$FLOW" batch-coverage --root ./config/no-config ./config/no-config/d2.js


#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

printf "'package/sub' does not resolve:\n"
assert_errors "$FLOW" status
echo

# package.js and package/package.json are duplicate providers. we pick package.js,
# which means `package/sub` doesn't resolve.
printf "duplicate provider:\n"
mv fixtures/package package
# changing a package will restart the server, so update saved_state_file_changes
if [ -f .flow.saved_state_file_changes ]; then
  printf "package/package.json\npackage/index.js\npackage/sub.js\n" > .flow.saved_state_file_changes
fi
assert_ok "$FLOW" force-recheck package/package.json package/index.js package/sub.js
assert_errors "$FLOW" status
echo

# changing `package` should force `test` to get rechecked -- this tests whether
# `test` has the proper phantom dependents to notice that package/package.json is now
# the provider.
printf "'package/sub' module resolves to package:\n"
mv package.js fixtures/package.js
assert_ok "$FLOW" force-recheck package/package.json package/index.js package/sub.js package.js
assert_errors "$FLOW" status
echo

printf "'package/sub' does not resolve again:\n"
mv package fixtures/package
mv fixtures/package.js package.js
# changing a package will restart the server, so update saved_state_file_changes
if [ -f .flow.saved_state_file_changes ]; then
  echo "" > .flow.saved_state_file_changes
fi
assert_ok "$FLOW" force-recheck package/package.json package/index.js package/sub.js package.js
assert_errors "$FLOW" status