使用TypeScript/ESLint开发Firebase云函数时如何修复缩进错误?
问题
使用TypeScript/ESLint开发Firebase云函数时,因缩进错误无法部署,错误示例:
35:1 error Unexpected tab character no-tabs
35:1 error Expected indentation of 6 spaces but found 2 tabs indent
此前使用Swift,VSCode制表符大小设置为4,代码如下:
import * as functions from "firebase-functions"; import * as admin from "firebase-admin"; admin.initializeApp; const db = admin.firestore(); interface Quote { id: string index: number authorName: string quote: string occupation: string imageURL: string webImageURL: string } interface User { id: string name: string currentQuote: [Quote] } interface QuoteData { id: string highestIndex: number } // get users to noitify, update new timme, get next quote, notify the user export const taskRunner = functions.runWith({ memory: "2GB" }).pubsub .schedule("* * * * *").onRun(async (context) => { // consistent timestamp const now = admin.firestore.Timestamp.now(); now.toDate().toTimeString(); // query all documents ready to perform const query = db.collection("users").where("scheduleTime", "<=", now); const needToNotify = await query.get(); if (needToNotify.docChanges.length <= 0) { return; } const adminDataQuery = db.collection("adminData").doc("quotesData"); const adminData = await adminDataQuery.get(); const quoteData = adminData.data() as QuoteData; // Loop through query results needToNotify.forEach(async (snapshot) => { const nextNotifTime = now.toDate(); nextNotifTime.setDate(nextNotifTime.getDate() + 1); snapshot.ref.update({ "scheduleTime": admin.firestore.Timestamp .fromDate(nextNotifTime), }); const userData = snapshot.data() as User; // Set temporary query for data let nextQuoteQuery = db.collection("quotes") .where("quoteIndex", "==", 0).limit(1); const defaultQuoteQuery = db.collection("quotes") .where("quoteIndex", "==", userData.currentQuote[0].index + 1) .limit(1); if (userData.currentQuote.length > 0) { const nextQuoteIndex = userData.currentQuote[0].index + 1; if (quoteData.highestIndex > nextQuoteIndex) { return; } nextQuoteQuery = db.collection("quotes") .where("quoteIndex", "==", nextQuoteIndex) .limit(1); } else { nextQuoteQuery = defaultQuoteQuery; } const nextQuote = await nextQuoteQuery.get(); // Should only contain one quote, // Since we called.limit(1) on the above query nextQuote.forEach(async (quoteData) => { const nextQuoteData = quoteData.data() as Quote; snapshot.ref.update({ currentQuote: [nextQuoteData] }); // Push new quote notifications to users const message = { notification: { title: nextQuoteData.authorName, body: nextQuoteData.quote, }, topic: "quote", }; await admin.messaging().send(message); }); }); });
解决方案
统一VSCode缩进设置,替换制表符为空格
- 打开VSCode设置(快捷键
Ctrl+,或Cmd+,) - 搜索
Editor: Insert Spaces,勾选该选项(强制输入制表符时插入空格) - 搜索
Editor: Tab Size,设置为2(匹配ESLint报错期望的2空格缩进规则) - 打开代码文件,按
Ctrl+Shift+P(或Cmd+Shift+P)打开命令面板,输入Convert Indentation to Spaces并执行,自动替换所有制表符为对应空格
- 打开VSCode设置(快捷键
自定义ESLint缩进规则(可选,保留4空格)
若希望保持VSCode的4空格设置,修改项目根目录的.eslintrc.js(或.eslintrc.json)文件,添加缩进规则:module.exports = { // 其他已有规则... rules: { "indent": ["error", 4], // 设置缩进为4空格 "no-tabs": "error" // 禁止使用制表符 } };修改后重启VSCode或ESLint服务生效
自动修复所有缩进错误
在VSCode中按Ctrl+Shift+P,输入ESLint: Fix All Auto-Fixable Problems并执行,ESLint会自动修复所有可修复的缩进问题,包括制表符替换和缩进长度调整
内容的提问来源于stack exchange,提问作者Trevor
相关产品推荐
相关产品推荐

