You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CoffeeScript代码报Unexpected Indentation错误,求排查原因

Fixing the "Unexpected Indentation" Error in Your CoffeeScript Code

Hey there! Let's break down why you're seeing that "Unexpected Indentation" error and get your code back on track.

First off, CoffeeScript is extremely strict about indentation rules—you can’t mix tabs and spaces, and every line within the same code block must use the exact same number of indentation spaces. That’s the #1 culprit behind this error. Let’s walk through the fixes for your code:

Key Issues to Address

  1. Inconsistent indentation: Double-check that your entire file uses either tabs or spaces (I recommend sticking with 2 or 4 spaces consistently). For example, lines inside the for rule in rules loop, the store.on callback, and the Ext.create config all need matching indentation to avoid parsing errors.
  2. Invalid syntax throwing off parsing: You have searchCriteria.push["Hello"]—this is wrong! push is a method, so it should be called with parentheses: searchCriteria.push("Hello").
  3. Potential logic quirk: When you do searchCriteria.push(emptyRules), you’re pushing the entire emptyRules array as a single element into searchCriteria. If you want to add the object inside emptyRules directly, use the spread operator: searchCriteria.push(...emptyRules).

Corrected Code

Here’s your code with consistent 4-space indentation and all fixes applied:

evaluateContainerRules: ( rules, containerType ) ->
    deferred = Ext.create( 'Deft.promise.Deferred' )
    searchCriteria = []
    for rule in rules
        if rule.searchTerms? and rule.searchTerms.length > 0
            searchCriteria.push( rule )
            console.log 'rule'
            console.log 'searchCriteria'
    if searchCriteria.length <= 0
        emptyRules = [ { searchOption: 'EMPTY_RULE', searchTerms: true } ]
        console.log 'emptyRules'
        # Push the individual rule object instead of the whole array
        searchCriteria.push( ...emptyRules )
        searchCriteria.push("Hello")
        console.log 'searchCriteria'
    store = Ext.create( 'Traverse.core.store.admin.container.ContainerMemberSummarySearchStore',
        params:
            searchCriterias: searchCriteria
            traverseTypeEnums: if containerType is 'device' then [ Traverse.core.enumeration.TraverseType.DEVICE ] else [ Traverse.core.enumeration.TraverseType.TEST ]
    )
    complete = false
    store.on( 'load', ( records, operation, success ) ->
        # NOTE: callback is fired multiple times for a paging store, only execute logic during the first call
        if complete
            return
        complete = true
        if success
            deferred.resolve( store )
        else
            store.destroyStore()
            @showError( operation.getError() )
            deferred.reject( operation.getError() )
        return
    , scope: @ )
    store.load()
    return deferred.promise

Quick Tips to Avoid Indentation Headaches

  • Use a code editor that shows invisible characters (like tabs/spaces) to spot inconsistencies instantly.
  • Set your editor to auto-convert tabs to spaces—this eliminates accidental mixing of indent types.
  • Double-check indentation after pasting code from other sources—pasted code often brings messy indentation along with it.

内容的提问来源于stack exchange,提问作者Priya N

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:58:59