# Description:
#   CSP Evaluator libraries (Parser, Evaluator, Widget).

load("//javascript/typescript:build_defs.bzl", "ts_library")
load("//tools/build_defs/js:rules.bzl", "js_binary", "js_library")
load(
    "//javascript/closure/builddefs:builddefs.bzl",
    "CLOSURE_COMPILER_FLAGS_FULL",
)
load("//testing/karma/builddefs:karma_web_test_suite.bzl", "karma_web_test_suite")
load("//testing/web/build_defs:web.bzl", "custom_browser")

licenses(["notice"])

exports_files(["LICENSE"])

package_group(
    name = "csp_evaluator",
    packages = [
        "//javascript/security/csp/csp_evaluator/...",
        "//third_party/csp_evaluator/...",
    ],
)

package(default_visibility = ["//visibility:public"])

filegroup(
    name = "opensource_files",
    testonly = 1,
    srcs = [
        "AUTHORS",
        "CONTRIBUTING",
        "LICENSE",
        "README.md",
        ":js_files",
        ":test_files",
        "//javascript/security/csp/csp_evaluator/ui:js_files",
        "//javascript/security/csp/csp_evaluator/ui:soy_files",
        "//javascript/security/csp/csp_evaluator/ui:test_files",
        "//third_party/csp_evaluator/allowlist_bypasses:js_files",
        "//third_party/csp_evaluator/allowlist_bypasses/json:allowlistbypasses_json",
        "//third_party/csp_evaluator/checks:js_files",
        "//third_party/csp_evaluator/checks:test_files",
        "//third_party/csp_evaluator/opensource:compiler.flags",
        "//third_party/csp_evaluator/opensource:do.sh",
        "//third_party/csp_evaluator/opensource:download-libs.sh",
    ],
)

JS_COMPILE_DEFS = [
    "--language_out=ECMASCRIPT5",
    "--jscomp_error=missingReturn",
    "--jscomp_error=missingProvide",
    "--closure_pass",
    "--compute_function_side_effects=true",
    "--jscomp_error=accessControls",
    "--jscomp_error=checkDebuggerStatement",
    "--jscomp_error=checkRegExp",
    "--jscomp_error=checkTypes",
    "--jscomp_error=checkVars",
    "--jscomp_error=const",
    "--jscomp_error=constantProperty",
    "--jscomp_error=duplicate",
    "--jscomp_error=es5Strict",
    "--jscomp_error=externsValidation",
    "--jscomp_error=globalThis",
    "--jscomp_error=invalidCasts",
    "--jscomp_error=missingProperties",
    "--jscomp_error=nonStandardJsDocs",
    "--jscomp_error=strictModuleDepCheck",
    "--jscomp_error=suspiciousCode",
    "--jscomp_error=undefinedVars",
    "--jscomp_error=unknownDefines",
    "--jscomp_error=uselessCode",
    "--jscomp_error=visibility",
    "--jscomp_warning=deprecated",
    "--remove_unused_prototype_props",
    "--smart_name_removal",
    "--strict",
    "--generate_exports",
]

js_binary(
    name = "all_js_binary",
    compile = 1,
    defs = CLOSURE_COMPILER_FLAGS_FULL + JS_COMPILE_DEFS,
    externs_list = ["//javascript/security/csp/csp_editor:externs"],
    deps = ["all_js"],
)

js_library(
    name = "all_js",
    srcs = [],
    deps_mgmt = "legacy",
    exports = [
        "//javascript/security/csp/csp_evaluator/appengine:all_js",
        "//third_party/csp_evaluator/checks:all_checks",
        "//javascript/security/csp/csp_evaluator/proto:all_js",
        "//javascript/security/csp/csp_evaluator/ui:all_js",
        "//third_party/csp_evaluator/allowlist_bypasses:all_bypasses",
        ":csp",
        ":evaluator",
        ":finding",
        ":parser",
        ":utils",
    ],
)

js_binary(
    name = "optimized_binary",
    compile = True,
    defs = ["--language_out=ECMASCRIPT6"],
    deps = [":optimized_library"],
)

js_library(
    name = "optimized_library",
    srcs = [],
    exports = [
        ":csp",
        ":evaluator",
        ":finding",
        ":parser",
        ":utils",
        "//third_party/csp_evaluator/checks:all_checks",
        "//third_party/csp_evaluator/allowlist_bypasses:all_bypasses",
        "//third_party/csp_evaluator/lighthouse:lighthouse_checks",
    ],
)

ts_library(
    name = "csp",
    srcs = ["csp.ts"],
    deps = [
        ":finding",
    ],
)

ts_library(
    name = "ts_test_lib",
    testonly = True,
    srcs = [
        "csp_test.ts",
        "evaluator_test.ts",
        "finding_test.ts",
        "parser_test.ts",
        "utils_test.ts",
    ],
    deps = [
        ":csp",
        ":evaluator",
        ":finding",
        ":parser",
        ":utils",
        "//third_party/javascript/closure/array",
        "//third_party/javascript/closure/object",
        "//third_party/javascript/typings/jasmine",
    ],
)

custom_browser(
    name = "safari-iphone-stable",
    browser = "//testing/web/browsers/mobile/v12_0:safari-iphone",
)

karma_web_test_suite(
    name = "ts_tests",
    browsers = [
        ":safari-iphone-stable",
        "//testing/web/browsers:chrome-linux",
        "//testing/web/browsers:chrome-win7",
        "//testing/web/browsers/v63_0:firefox-linux",
        "//testing/web/browsers/v90_0:firefox-win7",
    ],
    deps = [":ts_test_lib"],
)

ts_library(
    name = "evaluator",
    srcs = ["evaluator.ts"],
    deps = [
        ":csp",
        ":finding",
        "//third_party/csp_evaluator/checks:checker",
        "//third_party/csp_evaluator/checks:parser_checks",
        "//third_party/csp_evaluator/checks:security_checks",
        "//third_party/csp_evaluator/checks:strictcsp_checks",
    ],
)

ts_library(
    name = "finding",
    srcs = ["finding.ts"],
    deps = [],
)

ts_library(
    name = "parser",
    srcs = ["parser.ts"],
    deps = [
        ":csp",
    ],
)

ts_library(
    name = "utils",
    srcs = ["utils.ts"],
    deps = [
        ":csp",
    ],
)

filegroup(
    name = "js_files",
    srcs = glob(["*.js"]),
)

filegroup(
    name = "test_files",
    testonly = 1,
    srcs = glob([
        "*_test.html",
        "*_test.js",
        "*_test_dom.html",
    ]),
)

test_suite(
    name = "JsTestSuite",
    tests = [
        ":ts_tests",
        "//javascript/security/csp/csp_evaluator/appengine:appengine_tests",
        "//javascript/security/csp/csp_evaluator/proto:proto_tests",
        "//javascript/security/csp/csp_evaluator/ui:widget_test",
        "//third_party/csp_evaluator/checks:checks_tests",
        "//third_party/csp_evaluator/lighthouse:lighthouse_tests",
    ],
)
