扫描源码中是否包含react :param params: :return:
(self, params)
| 34 | } |
| 35 | |
| 36 | def analyze(self, params): |
| 37 | """ |
| 38 | 扫描源码中是否包含react |
| 39 | :param params: |
| 40 | :return: |
| 41 | """ |
| 42 | source_dir = params.source_dir |
| 43 | filter_mgr = FilterPathUtil(params) |
| 44 | relpos = len(source_dir) + 1 |
| 45 | |
| 46 | issues = [] |
| 47 | package_json_list = [] |
| 48 | self._get_package_json(source_dir, package_json_list) |
| 49 | package_json_list = [path for path in package_json_list if not filter_mgr.should_filter_path(path[relpos:])] |
| 50 | LogPrinter.info(f"搜索到的package文件有: f{package_json_list}") |
| 51 | if not package_json_list: |
| 52 | # 项目根目录下有package.json文件,扫描项目中的react的js文件 |
| 53 | LogPrinter.debug("no package.json") |
| 54 | toscans = PathMgr().get_dir_files(source_dir, (".js", ".jsx")) |
| 55 | for file in toscans: |
| 56 | reacrJsFile = re.match(".*/[^/]*react[^/]*$", file, re.I) |
| 57 | if not reacrJsFile: |
| 58 | continue |
| 59 | jsContent = open(file, "r").read().strip() |
| 60 | versionMatch = re.match(r"(^/\*.*?v((\d{1,2})\.\d{1,2}\.\d{1,2})).*?\*/", jsContent, re.I | re.U | re.S) |
| 61 | if versionMatch and int(versionMatch.group(3)) < 16 and versionMatch.group(2) != "15.6.2": |
| 62 | path = file[relpos:] |
| 63 | if filter_mgr.should_filter_path(path): |
| 64 | continue |
| 65 | msg = ( |
| 66 | "该项目使用的ReactJs的版本号是:" |
| 67 | + versionMatch.group(2) |
| 68 | + ",版本号小于16.0.0或者不为15.6.2,需要更新到16.0.0版本以上或者为15.6.2版本。对应模块为:" |
| 69 | + file |
| 70 | ) |
| 71 | issues.append( |
| 72 | { |
| 73 | "path": path, |
| 74 | "rule": "FbRjs", |
| 75 | "msg": msg, |
| 76 | "line": versionMatch.group(1).count("\n") + 1, |
| 77 | "column": 100, |
| 78 | } |
| 79 | ) |
| 80 | else: |
| 81 | LogPrinter.debug("have package.json") |
| 82 | self._scan_package_json(issues, package_json_list, relpos, filter_mgr) |
| 83 | |
| 84 | LogPrinter.debug(issues) |
| 85 | return issues |
| 86 | |
| 87 | def _scan_package_json(self, issues, package_json_list, relpos, filter_mgr): |
| 88 | """ |
nothing calls this directly
no test coverage detected