Dmitrii Filippov | 3d7ad55 | 2020-03-26 12:01:27 +0100 | [diff] [blame] | 1 | # Copyright (C) 2020 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """This file contains macro to run eslint and define a eslint test rule.""" |
| 16 | |
| 17 | load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary", "nodejs_test") |
| 18 | |
| 19 | def eslint(name, plugins, srcs, config, ignore, extensions = [".js"], data = []): |
| 20 | """ Macro to define eslint rules for files. |
| 21 | |
Tao Zhou | 74ac76b | 2020-04-03 10:15:36 +0200 | [diff] [blame] | 22 | Args: |
| 23 | name: name of the rule |
| 24 | plugins: list of npm dependencies with plugins, for example "@npm//eslint-config-google" |
| 25 | srcs: list of files to be checked (ignored in {name}_bin rule) |
| 26 | config: eslint config file |
| 27 | ignore: eslint ignore file |
| 28 | extensions: list of file extensions to be checked. This is an additional filter for |
| 29 | srcs list. Each extension must start with '.' character. |
| 30 | Default: [".js"]. |
| 31 | data: list of additional dependencies. For example if a config file extends an another |
| 32 | file, this other file must be added to data. |
Dmitrii Filippov | 3d7ad55 | 2020-03-26 12:01:27 +0100 | [diff] [blame] | 33 | |
Tao Zhou | 74ac76b | 2020-04-03 10:15:36 +0200 | [diff] [blame] | 34 | Generate: 2 rules: |
| 35 | {name}_test rule - runs eslint tests. You can run this rule with |
| 36 | 'bazel test {name}_test' command. The rule tests all files from srcs with specified |
| 37 | extensions inside the package where eslint macro is called. |
| 38 | {name}_bin rule - runs eslint with specified settings; ignores srcs. To use this rule |
| 39 | you must pass a folder to check, for example: |
David Ostrovsky | d9e02c6 | 2020-04-03 19:56:48 +0200 | [diff] [blame^] | 40 | bazel run {name}_test -- --fix $(pwd)/polygerrit-ui/app |
Dmitrii Filippov | 3d7ad55 | 2020-03-26 12:01:27 +0100 | [diff] [blame] | 41 | """ |
| 42 | entry_point = "@npm//:node_modules/eslint/bin/eslint.js" |
| 43 | bin_data = [ |
| 44 | "@npm//eslint:eslint", |
| 45 | config, |
| 46 | ignore, |
| 47 | ] + plugins + data |
| 48 | common_templated_args = [ |
| 49 | "--ext", |
| 50 | ",".join(extensions), |
| 51 | "-c", |
| 52 | # Use rlocation/rootpath instead of location. |
| 53 | # See note and example here: |
| 54 | # https://bazelbuild.github.io/rules_nodejs/Built-ins.html#nodejs_binary |
| 55 | "$$(rlocation $(rootpath {}))".format(config), |
| 56 | "--ignore-path", |
| 57 | "$$(rlocation $(rootpath {}))".format(ignore), |
| 58 | ] |
| 59 | nodejs_test( |
| 60 | name = name + "_test", |
| 61 | entry_point = entry_point, |
| 62 | data = bin_data + srcs, |
| 63 | # Bazel generates 2 .js files, where names of the files are generated from the name |
| 64 | # of the rule: {name}_test_require_patch.js and {name}_test_loader.js |
| 65 | # Ignore these 2 files, for simplicity do not use {name} in the patterns. |
| 66 | templated_args = common_templated_args + [ |
| 67 | "--ignore-pattern", |
| 68 | "*_test_require_patch.js", |
| 69 | "--ignore-pattern", |
| 70 | "*_test_loader.js", |
| 71 | native.package_name(), |
| 72 | ], |
| 73 | # Should not run sandboxed. |
| 74 | tags = [ |
| 75 | "local", |
| 76 | "manual", |
| 77 | ], |
| 78 | ) |
| 79 | |
| 80 | nodejs_binary( |
| 81 | name = name + "_bin", |
| 82 | entry_point = "@npm//:node_modules/eslint/bin/eslint.js", |
| 83 | data = bin_data, |
| 84 | # Bazel generates 2 .js files, where names of the files are generated from the name |
| 85 | # of the rule: {name}_bin_require_patch.js and {name}_bin_loader.js |
| 86 | # Ignore these 2 files, for simplicity do not use {name} in the patterns. |
| 87 | templated_args = common_templated_args + [ |
| 88 | "--ignore-pattern", |
| 89 | "*_bin_require_patch.js", |
| 90 | "--ignore-pattern", |
| 91 | "*_bin_loader.js", |
| 92 | ], |
| 93 | ) |