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
- 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 rulesloop, thestore.oncallback, and theExt.createconfig all need matching indentation to avoid parsing errors. - Invalid syntax throwing off parsing: You have
searchCriteria.push["Hello"]—this is wrong!pushis a method, so it should be called with parentheses:searchCriteria.push("Hello"). - Potential logic quirk: When you do
searchCriteria.push(emptyRules), you’re pushing the entireemptyRulesarray as a single element intosearchCriteria. If you want to add the object insideemptyRulesdirectly, 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
相关产品推荐
相关产品推荐

