【问题标题】:Prettier for YAML - SyntaxError: Nested mappings are not allowed in compact mappings更漂亮的 YAML - SyntaxError:紧凑映射中不允许嵌套映射
【发布时间】:2021-03-10 22:43:56
【问题描述】:

在我的项目中,有一个用于 NewRelic newrelic.yml 的 YAML 文件,其中包含用于设置 app_name 的 Ruby 代码。现有配置正在运行,我需要编辑该文件,但是当我尝试执行 yarn commit 时它失败并出现以下错误,对我来说这看起来像是一个 Prettier 错误。

我尝试在行前添加#prettier-ignore,但仍然导致提交失败。

node_modules/prettier/index.js:13588
      throw error;
      ^

SyntaxError: Nested mappings are not allowed in compact mappings (18:13)
  17 |   # Relic.  For more details, see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/renaming-applications
> 18 |   app_name: <%= ENV["SERVER_ENV"] == 'staging' ? 'MyApp (Staging)' : 'MyApp' %>
     |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我的代码编辑器是相关的 VSCode。

所以我想弄清楚为什么# prettier-ignore 不能像更漂亮的文档所指出的那样工作。或者弄清楚如何让事情格式化以通过,这显然是首选。

【问题讨论】:

    标签: ruby-on-rails ruby yaml prettier prettier-vscode


    【解决方案1】:

    似乎 Prettier 不处理 YAML 文件中的 ERB。

    我会将所有常用配置添加到一个块中,然后通过 YAML aliases 重新使用该块。

    common: &default_settings
      license_key: 1234
      log_lever: info
      # ...
    
    production:
      <<: *default_settings
      app_name: MyApp
    
    staging:
      <<: *default_settings
      app_name: MyApp (Staging)
    

    【讨论】:

    • 谢谢,我会尝试一下,似乎大多数教程都显示使用 ERB 从 CONFIG_VARS 和许可证密钥设置不同环境的名称。所以让 Prettier 忽略整个文件可能会更好。 (这很遗憾,因为确保其余格式正确无误)。
    猜你喜欢
    • 1970-01-01
    • 2020-10-05
    • 2019-10-27
    • 2016-07-13
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多