Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(esbuildHelperChecker): catch js parse error #12285

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async function checkDir(opts: { dir: string }) {
for (const jsFile of jsFiles) {
const vars = await getGlobalVars({
content: fs.readFileSync(path.join(opts.dir, jsFile), 'utf-8'),
fileName: jsFile,
});
for (const v of vars) {
varMap[v] = varMap[v] || [];
Expand All @@ -40,24 +41,37 @@ async function checkDir(opts: { dir: string }) {
logger.info(`[esbuildHelperChecker] No conflicts found.`);
}

export async function getGlobalVars(opts: { content: string }) {
const ast = parser.parse(opts.content, {
sourceType: 'module',
sourceFilename: 'foo.js',
plugins: [],
});
export async function getGlobalVars(opts: {
content: string;
fileName: string;
}) {
const vars: string[] = [];
ast.program.body.forEach((node) => {
if (t.isVariableDeclaration(node)) {
node.declarations.forEach((declaration) => {
if (t.isVariableDeclarator(declaration)) {
if (t.isIdentifier(declaration.id)) {
vars.push(declaration.id.name);
/**
* 产物目录可能会有用户自定义的非标准 js 文件,会导致 parse 异常,这里转为 error 提示,不中断程序
* eg. git lfs 会生成不符合 js 规范的 js 文件
*/
try {
const ast = parser.parse(opts.content, {
sourceType: 'module',
sourceFilename: 'foo.js',
plugins: [],
});
ast.program.body.forEach((node) => {
if (t.isVariableDeclaration(node)) {
node.declarations.forEach((declaration) => {
if (t.isVariableDeclarator(declaration)) {
if (t.isIdentifier(declaration.id)) {
vars.push(declaration.id.name);
}
}
}
});
}
});
});
}
});
} catch (error: any) {
logger.error(
`[esbuildHelperChecker] Failed to parse ${opts.fileName}, ${error.message}`,
hanzebang marked this conversation as resolved.
Show resolved Hide resolved
);
}
Comment on lines +44 to +74
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refine the hardcoded filename in parser options.

The sourceFilename property in the parser options is currently hardcoded to 'foo.js'. It would be more appropriate to use the actual fileName from the function's parameters to enhance the error context and debugging process.

-      sourceFilename: 'foo.js',
+      sourceFilename: opts.fileName,

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
export async function getGlobalVars(opts: {
content: string;
fileName: string;
}) {
const vars: string[] = [];
ast.program.body.forEach((node) => {
if (t.isVariableDeclaration(node)) {
node.declarations.forEach((declaration) => {
if (t.isVariableDeclarator(declaration)) {
if (t.isIdentifier(declaration.id)) {
vars.push(declaration.id.name);
/**
* 产物目录可能会有用户自定义的非标准 js 文件,会导致 parse 异常,这里转为 error 提示,不中断程序
* eg. git lfs 会生成不符合 js 规范的 js 文件
*/
try {
const ast = parser.parse(opts.content, {
sourceType: 'module',
sourceFilename: 'foo.js',
plugins: [],
});
ast.program.body.forEach((node) => {
if (t.isVariableDeclaration(node)) {
node.declarations.forEach((declaration) => {
if (t.isVariableDeclarator(declaration)) {
if (t.isIdentifier(declaration.id)) {
vars.push(declaration.id.name);
}
}
}
});
}
});
});
}
});
} catch (error: any) {
logger.error(
`[esbuildHelperChecker] Failed to parse ${opts.fileName}, ${error.message}`,
);
}
export async function getGlobalVars(opts: {
content: string;
fileName: string;
}) {
const vars: string[] = [];
/**
* 产物目录可能会有用户自定义的非标准 js 文件,会导致 parse 异常,这里转为 error 提示,不中断程序
* eg. git lfs 会生成不符合 js 规范的 js 文件
*/
try {
const ast = parser.parse(opts.content, {
sourceType: 'module',
sourceFilename: opts.fileName,
plugins: [],
});
ast.program.body.forEach((node) => {
if (t.isVariableDeclaration(node)) {
node.declarations.forEach((declaration) => {
if (t.isVariableDeclarator(declaration)) {
if (t.isIdentifier(declaration.id)) {
vars.push(declaration.id.name);
}
}
});
}
});
} catch (error: any) {
logger.error(
`[esbuildHelperChecker] Failed to parse ${opts.fileName}, ${error.message}`,
);
}

return vars;
}

Expand Down
Loading