代码风格一致性

我发誓我已经从 discourse-plugin-skeleton 复制了所有 lint 配置到我的插件中,当我在我的插件目录中运行

pnpm prettier  --write "assets/**/*.{scss,js,gjs,hbs}"

时,得到的结果与我在 discourse 核心目录中运行

./bin/lint  --fix plugins/discourse-pfaffmanager/assets/**/*.{scss,js,gjs,hbs}

时得到的结果不同。

可以预料到,后者与 GitHub Actions 运行时发生的情况相匹配。

我真正想要的是在 VSCode 中保存时能发生“正确的事情”,但我很确定那是另一个问题。

3 个赞

是的,@cvx / @david

我也注意到了这一点……插件中的代码检查都是“雪花”材料,我们在 CI 中为旧版和新版代码检查风格设置了分支,并在仓库中要求进行大量设置。

我认为一个很棒的目标是修复它,以便 bin/lint 也能用于插件和主题。我们还没有实现这一点,它目前只适用于核心和核心插件。

2 个赞

确实,主题/插件的 linting 是有意与核心分离的。它们可能会运行不同版本的 linting 工具和配置。这对于我们推出更改而不会突然破坏所有代码库中的 linting 至关重要。

我们或许可以让 bin/lint 正常工作——它需要 cd 进入插件/主题目录,运行 pnpm i,然后在该目录的上下文中运行任何 linting 命令。

它应该会。我们团队的大多数成员都使用 VSCode(或 VSCode 的衍生版本),所以我们优化的就是这个。Eslint 和 Prettier 的 VSCode 插件会搜索给定文件的“最近的”安装和配置。

您是否在主题/插件目录中运行了 pnpm i?这是安装其 linting 依赖项所必需的,以便 VSCode 能够找到正确的东西。

1 个赞

有趣的转折

  • 允许它可选地运行核心规则
  • 允许它重新配置现有配置以运行核心规则

那有什么好处呢?:sweat_smile:

主题/插件的 linting 与核心的在根本上是不同的。我们有主题/插件特定的规则。主题有它们自己的魔术全局变量,ESLint 需要知道这些(themePrefix/settings

我们非常注意维护这一点,在主题/插件骨架(theme/plugin-skeleton)仓库中记录它,并通过大规模拉取请求(mass-pr)有条不紊地推出变更。

2 个赞

作为一个自我升级的工具,但我完全理解我们有其他系统

好的。我们有用于自动更新(从骨架)的脚本,在这里。让这些脚本更容易在单个主题/插件上运行可能是一个不错的项目。

顺便说一下,当我说它们有不同的配置时,我并不是说我们在到处复制配置。我们有一个版本化的 lint configs 包,其中包含 prettier、eslint、stylelint 和 ember-template-lint 的共享配置。将主题/插件升级到最新规则通常只需在 package.json 中提升 lint-configs 的版本即可。

1 个赞

但我很确定,当我遵循插件中进行的 linting 时,github action 中的 linting 测试会失败。

啊哈!!!pnpm i 似乎是我错过的魔法。我已经将 ‘pnpm i’ 添加到我的更新 discourse 核心的脚本和执行 linting 的脚本中。。。

所以现在我的脚本的作用如下:

if [[ "$ARG" == "eslint" ]]
then
  # 参见 https://github.com/discourse/.github/blob/main/.github/workflows/discourse-plugin.yml
  if [ -f plugin.rb ]
  then
    echo "正在 lint 当前目录。"
  else
    echo "不在插件中。使用 $DEFAULT_PLUGIN"
    cd $DEFAULT_PLUGIN
  fi
  echo "正在 LINT $(pwd)"
  #cd /home/pfaffman/src/literatecomputing/discourse-display-email
  # pnpm install
  echo "正在执行 `pnpm i`"
  pnpm -i

  echo -n ESLINT . . .
  pnpm eslint --no-error-on-unmatched-pattern {test,assets,admin/assets}/javascripts
  exit_code="?"
  if [[ $exit_code -ne 0  ]]
  then
    echo "eslint 不满意 -- $exit_code -- 尝试修复"
    pnpm eslint --fix --no-error-on-unmatched-pattern {test,assets,admin/assets}/javascripts
    exit_code="?"
    if [[ $exit_code -ne 0  ]]
    then
      echo "eslint 仍然不满意 -- $exit_code -- 结束"
      exit
    fi
  fi

  echo ESLINT 完成。

  ## styllint

  echo -n STYLELINT . . .
  pnpm stylelint --allow-empty-input "assets/**/*.scss"
  exit_code="?"
    if [[ $exit_code -ne 0  ]]
    then
      echo "stylelint 不满意。尝试修复 . . . "
    fi
  pnpm stylelint --fix --allow-empty-input "assets/**/*.scss"
  pnpm stylelint --allow-empty-input "assets/**/*.scss"
  exit_code="?"
    if [[ $exit_code -ne 0  ]]
    then
      echo "stylelint 仍然不满意。这很遗憾。结束"
      exit
    fi
  echo "完成!"

  ## end stylelint

  ## PRETTIER

  echo -n "Prettier . . . "
  if [ -n "$(find assets -type f \( -name "*.scss" -o -name "*.js" -o -name "*.gjs" -o -name "*.hbs" \) 2>/dev/null)" ]; then
    #pnpm prettier --write                  'assets/**/*.{scss,js,gjs,es6,hbs}'
    # echo "正在执行 pnpm prettier --write --log-level warn assets/**/*.{scss,js,gjs,es6,hbs}"
    pnpm prettier --write --log-level error "assets/**/*.{scss,js,gjs,hbs}"
    if [[ $? -ne 0  ]]
    then
      echo "prettier assets 做了点事!!! -- $?"
      sleep 5
    fi
  fi
  if [ -n "$(find admin/assets -type f \( -name "*.scss" -o -name "*.js" -o -name "*.gjs" -o -name "*.hbs" \) 2>/dev/null)" ]; then
    #pnpm prettier --write                  'assets/**/*.{scss,js,gjs,es6,hbs}'
    # echo "正在执行 pnpm prettier --write --log-level warn admin/assets/**/*.{scss,js,gjs,hbs}"
    pnpm prettier --write --log-level log "assets/**/*.{scss,js,gjs,es6,hbs}"
    if [[ $? -ne 0  ]]
    then
      echo "prettier admin/assets 做了点事!!! -- $?"
      pnpm prettier --check --log-level log "assets/**/*.{scss,js,gjs,es6,hbs}"
    fi
  fi
  if [ -n "$(find test -type f \( -name "*.js" -o -name "*.gjs" \) 2>/dev/null)" ]; then
    #pnpm prettier --write                  'assets/**/*.{scss,js,gjs,es6,hbs}'
    # echo "正在执行 pnpm prettier --write --log-level warn assets/**/*.{scss,js,gjs,es6,hbs}"
    pnpm prettier --write --log-level warn "test/**/*.{js,gjs}"
    if [[ $? -ne 0  ]]
    then
      echo "prettier test 做了点事!!! -- $?"
      pnpm prettier --check --loglevel log "test/**/*.{js,gjs}"
    fi
  fi
  echo "Prettier 完成"

  echo "ember-template-lint"
  pnpm ember-template-lint --fix --no-error-on-unmatched-pattern assets/javascripts admin/assets/javascripts
  exit_code="?"
    if [[ $exit_code -ne 0  ]]
    then
      echo "完成 ember-template-lint --fix --no-error-on-unmatched-pattern assets/javascripts -- $exit_code"
      echo sleep 5
      sleep 5
    fi
  #bundle exec stree write Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake') $(git ls-files '*.thor')
  bundle exec stree check Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake') $(git ls-files '*.thor')
  exit_code="?"
    if [[ $exit_code -ne 0  ]]
    then
      echo "stree 不满意。尝试在 2 秒内修复 . . . "
      sleep 2
      echo "开始!!!"
      bundle exec stree write Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake') $(git ls-files '*.thor')
    fi
  bundle exec stree check Gemfile $(git ls-files '*.rb') $(git ls-files '*.rake') $(git ls-files '*.thor')
  exit_code="?"
    if [[ $exit_code -ne 0  ]]
    then
      echo "stree 仍然不满意。这很遗憾。等待让你更遗憾"
      sleep 15
    fi
  echo stree 完成

  echo "rubocop!"
  bundle exec rubocop -A $(find . -name "*.rb"|grep -v gems)
  exit_code="?"
  if [[ $exit_code -ne 0  ]]
  then
    echo "rubocop 完成 -- $exit_code"
    sleep 15
  fi
  exit
fi


但我也有这个,我认为它的作用几乎相同:

if [[ "$ARG" ==  "lint" ]]
then
  cd ~/src/discourse-repos/discourse
  find plugins/discourse-pfaffmanager/assets -type f \( -name "*.scss" -o -name "*.js" -o -name "*.gjs" -o -name "*.hbs" -o -name "*.rb" \) -exec ./bin/lint --fix {} +
  cd -
fi

现在唯一的区别是核心 linting 对此不满意(而且我认为我明白为什么核心能找到它而插件找不到,而且尽早修复这些弃用没有坏处):


/home/pfaffman/src/discourse-repos/discourse/plugins/discourse-pfaffmanager/assets/javascripts/discourse/components/modal/really-delete.gjs
  2:8  error  Use Glimmer components(@glimmer/component) instead of classic components(@ember/component)  ember/no-classic-components

✖ 1 个问题 (1 个错误,0 个警告)

如果我使用 bin/lint,那么我就不必担心 linting 会发生变化,从而需要更新我 54 行的脚本,对吗?

等等。在 skeleton 仓库中记录它们在哪里

我很想得到一些关于如何有条不紊地推出更改的指导,尽管我理解我的一小部分东西并不是你们的问题。。。这不应该在这里,但我已经尝试了几次来弄清楚 mass-pr,但还没有成功。我通常只是定期从 skeleton 的最新 pull 中复制一些 .whatever 文件,然后听之任之。

而且有 16 个这样的脚本。我是否需要关注该仓库,并在其中任何一个更新时运行它?而且很多主题似乎没有克隆 skeleton 仓库,所以它们无法复制所需的文件?

而且有几个以类似 sed: can’t read s/default.yml/stree-compat.yml/: No such file or directory 的错误失败?因为它们没有克隆 skeleton 仓库?(我一有机会就会在适当的地方开一个主题)。

bin/lint 使用核心的 linter 配置,因此不支持主题或插件。如果你使用它,你的主题/插件持续集成(CI)可能会出现失败。例如,即使是 Prettier 的微小版本升级也可能在空白字符处理方面产生细微的变化。

是的,这基本上就是批量拉取(mass-pr)脚本所做的事情 :+1:

update-linting.sh 是你想要的那个。其他脚本用于更专业的部署。

你可以这样做。或者,当你想要更新主题/插件的 linter 配置时,可以每隔一段时间重新运行它。由于配置是完全自包含的,核心中的更改永远不会影响你的主题/插件中的 linter——它们是完全解耦的。

太棒了!所以我就时不时地运行那一个,它就会更新所有的 linting 规则。其他的那些是用来做一些神奇的事情,比如帮助迁移到新的 ember 框架之类的?也许我开始理解了,我可以像示例中那样列出仓库。

明白了。我感到困惑,因为我还没有运行 pnpm i,所以主题/插件的 lint 正在做错误的事情,而核心的 lint 与 GitHub 保持一致。

是的,但我喜欢花 5 个小时来自动化一个我用手工 3 分钟就能完成的任务,这似乎是个不错的方式。:rofl: