项目根目录下有package.json文件,遍历json,看是否包含react关键字 :return:
(self, issues, package_json_list, relpos, filter_mgr)
| 85 | return issues |
| 86 | |
| 87 | def _scan_package_json(self, issues, package_json_list, relpos, filter_mgr): |
| 88 | """ |
| 89 | 项目根目录下有package.json文件,遍历json,看是否包含react关键字 |
| 90 | :return: |
| 91 | """ |
| 92 | for package_json in package_json_list: |
| 93 | LogPrinter.info(f"正在扫描: {package_json}") |
| 94 | with open(package_json, "r") as f: |
| 95 | try: |
| 96 | packageJsonContent = json.loads(f.read()) |
| 97 | except json.decoder.JSONDecodeError as err: |
| 98 | logger.info("项目文件 %s 格式有误读取失败" % package_json) |
| 99 | raise TaskError(code=E_NODE_TASK_CONFIG, msg='json读取失败%s' % str(err)) |
| 100 | if not isinstance(packageJsonContent, dict): |
| 101 | continue |
| 102 | |
| 103 | strList = linecache.getlines(package_json) |
| 104 | |
| 105 | for (k, v) in packageJsonContent.items(): |
| 106 | kmatch = re.match(".*dependencies", k, re.I) |
| 107 | if not kmatch: |
| 108 | continue |
| 109 | LogPrinter.debug(kmatch.group()) |
| 110 | if not isinstance(v, dict): |
| 111 | continue |
| 112 | for (k1, v1) in v.items(): |
| 113 | # 如不是字符串,则跳过 |
| 114 | if not isinstance(v1, str): |
| 115 | continue |
| 116 | reactMatch = re.match(r"^react((-dom((-server)|(-fiber))?)|(-with-addons))?$", k1, re.I) |
| 117 | if not reactMatch: |
| 118 | continue |
| 119 | LogPrinter.info(f"当前value是: {v1}") |
| 120 | versionMatch = re.match(r"\D*((\d{1,2})(\.\d{1,2}){0,2})", v1, re.I | re.U) |
| 121 | if versionMatch and int(versionMatch.group(2)) < 16 and versionMatch.group(1) != "15.6.2": |
| 122 | path = package_json[relpos:] |
| 123 | if filter_mgr.should_filter_path(path): |
| 124 | continue |
| 125 | lineContent = '"' + k1 + '": "' + v1 + '",' |
| 126 | LogPrinter.debug(lineContent) |
| 127 | for lineCount, lineStr in enumerate(strList): |
| 128 | if lineStr.strip() == lineContent: |
| 129 | # LogPrinter.debug('在', packageJson, '文件的第', lineCount + 1, '行') |
| 130 | msg = ( |
| 131 | "该项目使用的ReactJs的版本号是:" |
| 132 | + versionMatch.group(1) |
| 133 | + ",版本号小于16.0.0或者不为15.6.2,需要更新到16.0.0版本以上或者为15.6.2版本。对应模块为:" |
| 134 | + k1 |
| 135 | ) |
| 136 | issues.append( |
| 137 | {"path": path, "rule": "FbRjs", "msg": msg, "line": lineCount + 1, "column": 0} |
| 138 | ) |
| 139 | |
| 140 | def _get_package_json(self, root_dir, toscans): |
| 141 | """ |