• 切り替えへ… +
    browser.coffee cake.coffee coffee-script.coffee command.coffee grammar.coffee helpers.coffee index.coffee lexer.coffee nodes.coffee optparse.coffee register.coffee repl.coffee rewriter.coffee scope.litcoffee sourcemap.litcoffee
  • helpers.coffee

  • ¶

    このファイルは **レキサー**, **リライター**, **ノード**間で共有するよくあるヘルパー関数を含んでいます。オブジェクトのマージ, 配列のフラット化、文字のカウントなど。

  • ¶

    与えられた文字列の先頭を調べ、それがシーケンスと一致するかどうかを確認します。

    exports.starts = (string, literal, start) ->
      literal is string.substr start, literal.length
  • ¶

    与えられた文字列の末尾を調べ、それがシーケンスと一致するかどうかを確認します。

    exports.ends = (string, literal, back) ->
      len = literal.length
      literal is string.substr string.length - len - (back or 0), len
  • ¶

    文字列を `n` 回繰り返します。

    exports.repeat = repeat = (str, n) ->
  • ¶

    文字列を巧妙なアルゴリズムを使用してO(log(n))で連結します。

      res = ''
      while n > 0
        res += str if n & 1
        n >>>= 1
        str += str
      res
  • ¶

    配列から偽の値をすべて取り除きます。

    exports.compact = (array) ->
      item for item in array when item
  • ¶

    文字列内で文字列が出現する回数を数えます。

    exports.count = (string, substr) ->
      num = pos = 0
      return 1/0 unless substr.length
      num++ while pos = 1 + string.indexOf substr, pos
      num
  • ¶

    オブジェクトをマージし、両方の属性を持つ新しいコピーを返します。`Base#compile`が呼び出されるたびに使用され、他のブランチを汚すことなく Optionenハッシュのプロパティをツリー下に伝達できるようにします。

    exports.merge = (options, overrides) ->
      extend (extend {}, options), overrides
  • ¶

    ソースオブジェクトを他のオブジェクトの属性で拡張します(シャローコピー)。

    extend = exports.extend = (object, properties) ->
      for key, val of properties
        object[key] = val
      object
  • ¶

    配列のフラット化されたバージョンを返します。ノードから`children`のリストを取得するのに便利です。

    exports.flatten = flatten = (array) ->
      flattened = []
      for element in array
        if '[object Array]' is Object::toString.call element
          flattened = flattened.concat flatten element
        else
          flattened.push element
      flattened
  • ¶

    オブジェクトからキーを削除し、値を返します。ノードがオプションハッシュ内で特定のメソッドを探している場合に役立ちます。

    exports.del = (obj, key) ->
      val =  obj[key]
      delete obj[key]
      val
  • ¶

    一般的な Array::some

    exports.some = Array::some ? (fn) ->
      return true for e in this when fn e
      false
  • ¶

    ドキュメントをコメントに配置し、CoffeeScriptでコンパイルする「標準」形式のCoffeeScriptコード文字列を作成することにより、リテラシーCoffeeScriptコードを反転するためのシンプルな関数です。

    exports.invertLiterate = (code) ->
      maybe_code = true
      lines = for line in code.split('\n')
        if maybe_code and /^([ ]{4}|[ ]{0,3}\t)/.test line
          line
        else if maybe_code = /^\s*$/.test line
          line
        else
          '# ' + line
      lines.join '\n'
  • ¶

    2つのjisonスタイルのロケーションデータオブジェクトを一緒にマージします。`last`が提供されていない場合、これは単に`first`を返します。

    buildLocationData = (first, last) ->
      if not last
        first
      else
        first_line: first.first_line
        first_column: first.first_column
        last_line: last.last_line
        last_column: last.last_column
  • ¶

    これはオブジェクトをパラメーターとして受け取り、そのオブジェクトがASTノードである場合にそのオブジェクトのlocationDataを更新する関数を返します。オブジェクトはどちらの場合でも返されます。

    exports.addLocationDataFn = (first, last) ->
      (obj) ->
        if ((typeof obj) is 'object') and (!!obj['updateLocationDataIfMissing'])
          obj.updateLocationDataIfMissing buildLocationData(first, last)
    
        return obj
  • ¶

    jisonのロケーションデータを文字列に変換します。 `obj`はトークンまたはlocationDataのいずれかになります。

    exports.locationDataToString = (obj) ->
      if ("2" of obj) and ("first_line" of obj[2]) then locationData = obj[2]
      else if "first_line" of obj then locationData = obj
    
      if locationData
        "#{locationData.first_line + 1}:#{locationData.first_column + 1}-" +
        "#{locationData.last_line + 1}:#{locationData.last_column + 1}"
      else
        "No location data"
  • ¶

    `basename`の`coffee.md`と互換性のあるバージョンで、ファイルのサンス拡張子を返します。

    exports.baseFileName = (file, stripExt = no, useWinPathSep = no) ->
      pathSep = if useWinPathSep then /\\|\// else /\//
      parts = file.split(pathSep)
      file = parts[parts.length - 1]
      return file unless stripExt and file.indexOf('.') >= 0
      parts = file.split('.')
      parts.pop()
      parts.pop() if parts[parts.length - 1] is 'coffee' and parts.length > 1
      parts.join('.')
  • ¶

    ファイル名がCoffeeScriptファイルを表しているかどうかを判別します。

    exports.isCoffee = (file) -> /\.((lit)?coffee|coffee\.md)$/.test file
  • ¶

    ファイル名がリテラルCoffeeScriptファイルを表しているかどうかを判別します。

    exports.isLiterate = (file) -> /\.(litcoffee|coffee\.md)$/.test file
  • ¶

    特定の位置からSyntaxErrorをスローします。エラーの `toString`は「標準」形式のエラーメッセージ`<filename>:<line>:<col>: <message>`行エラーとエラーを示すマーカーを返します。

    exports.throwSyntaxError = (message, location) ->
      error = new SyntaxError message
      error.location = location
      error.toString = syntaxErrorToString
  • ¶

    コンパイラのスタックトレースを表示する代わりに、カスタムエラーメッセージを表示します(これは、たとえばCoffeeScriptをコンパイルするNode.jsアプリケーションでエラーが発生した場合に便利です)。

      error.stack = error.toString()
    
      throw error
  • ¶

    ソースコード情報がまだない場合、コンパイラのSyntaxErrorを更新します。

    exports.updateSyntaxError = (error, code, filename) ->
  • ¶

    他のエラーの `stack`プロパティを台無しにしないでください(つまり、考えられるバグ)。

      if error.toString is syntaxErrorToString
        error.code or= code
        error.filename or= filename
        error.stack = error.toString()
      error
    
    syntaxErrorToString = ->
      return Error::toString.call @ unless @code and @location
    
      {first_line, first_column, last_line, last_column} = @location
      last_line ?= first_line
      last_column ?= first_column
    
      filename = @filename or '[stdin]'
      codeLine = @code.split('\n')[first_line]
      start    = first_column
  • ¶

    複数行エラーの最初の行のみを表示します。

      end      = if first_line is last_line then last_column + 1 else codeLine.length
      marker   = codeLine[...start].replace(/[^\s]/g, ' ') + repeat('^', end - start)
  • ¶

    色が有効なTTYで実行されているかどうかを確認します。

      if process?
        colorsEnabled = process.stdout?.isTTY and not process.env?.NODE_DISABLE_COLORS
    
      if @colorful ? colorsEnabled
        colorize = (str) -> "\x1B[1;31m#{str}\x1B[0m"
        codeLine = codeLine[...start] + colorize(codeLine[start...end]) + codeLine[end..]
        marker   = colorize marker
    
      """
        #{filename}:#{first_line + 1}:#{first_column + 1}: error: #{@message}
        #{codeLine}
        #{marker}
      """
    
    exports.nameWhitespaceCharacter = (string) ->
      switch string
        when ' ' then 'space'
        when '\n' then 'newline'
        when '\r' then 'carriage return'
        when '\t' then 'tab'
        else string