streetsidesoftware / cspell

A Spell Checker for Code!
https://cspell.org
MIT License
1.17k stars 84 forks source link

[Bug]: Suggestions not working for `flagWords` #5835

Closed klonos closed 1 day ago

klonos commented 2 days ago

Kind of Issue

Runtime - command-line tools (GitHub Action actually)

Description

Over in #4133 it is implied that something like this would work:

flagWords:
  - ‘color: Colour’

The above suggests a .yml format, however as per the documentation here: https://cspell.org/docs/getting-started/#1-create-a-configuration-file, a .json file can also be used. If I am thinking correctly, then the equivalent for the above in the .json file would be as follows:

  "flagWords": [
    "color: Colour"
  ],

Is that ^^ correct? Is the expected format "<forbidden word>: <suggestions separated by commas>" or something else?

We currently have the following specified, but it does not seem to work (no suggestions offered):

  "flagWords": [
    "blacklist: denylist, deny list, deny, block, denied list, blocked list",
    "blacklisted: disallowed, denied, blocked",
    "blacklisting: disallowing, denying, blocking",
    "master: primary, main, active, default, base",
    "slave: secondary, replica, standby, fallback",
    "webmaster: admin, administrator",
    "whitelist: allowlist, allow list, allow, allowed list",
    "whitelisted: allowed",
    "whitelisting: allowing"
    "clicked_button: triggering_element"
  ],

Also in #4133, @gituserjava mentioned the following:

When we set enableSuggestions to true and specify flagwords as shown below ...

But I could not find any reference to that flag in the documentation of flagWords here: https://cspell.org/docs/forbidden-words

So, is enableSuggestions a thing, and if so, then how does it get set? Is it a thing that goes into cspell.json, or somewhere else?

cspell.json

{
  "$schema": "https://raw.githubusercontent.com/streetsidesoftware/cspell/main/cspell.schema.json",
  "version": "0.2",
  "dictionaryDefinitions": [
    {
      "name": "backdrop",
      "path": "backdrop.dic"
    },
    {
      "name": "drupal",
      "path": "https://git.drupalcode.org/project/drupal/-/raw/HEAD/core/misc/cspell/dictionary.txt"
    }
  ],
  "dictionaries": [
    "backdrop",
    "bash",
    "drupal",
    "css",
    "en-gb",
    "en-us",
    "filetypes",
    "fonts",
    "fullstack",
    "html",
    "lorem-ipsum",
    "php",
    "publicLicenses",
    "softwareTerms",
    "sql",
    "typescript"
  ],
  "flagWords": [
    "blacklist: denylist, deny list, deny, block, denied list, blocked list",
    "blacklisted: disallowed, denied, blocked",
    "blacklisting: disallowing, denying, blocking",
    "master: primary, main, active, default, base",
    "slave: secondary, replica, standby, fallback",
    "webmaster: admin, administrator",
    "whitelist: allowlist, allow list, allow, allowed list",
    "whitelisted: allowed",
    "whitelisting: allowing"
    "clicked_button: triggering_element"
  ],
  "globRoot": "../",
  "ignorePaths": [
    "*.svg",
    "/.*/",
    "/core/includes/diff.inc",
    "/core/includes/file.mimetypes.inc",
    "/core/includes/standard.inc",
    "/core/includes/transliteration/",
    "/core/misc/ckeditor/",
    "/core/misc/html5.js",
    "/core/misc/jquery.js",
    "/core/misc/jquery-extend*",
    "/core/misc/jquery-html-prefilter*",
    "/core/misc/jquery.*.js",
    "/core/misc/opensans/",
    "/core/misc/smartmenus/",
    "/core/misc/ui/",
    "/core/modules/ckeditor5/lib/ckeditor5/",
    "/core/modules/search/tests/UnicodeTest.txt",
    "/core/modules/system/system.tar.inc",
    "/core/themes/bartik/color/preview.html",
    "/files/",
    "/.*"
  ],
  "overrides": [
    {
      "filename": "/core/modules/ckeditor/ckeditor.module",
      "ignoreWords": ["pastetext", "pastefromword", "showblocks"]
    },
    {
      "filename": "/core/modules/filter/tests/filter.test",
      "ignoreWords": ["nbbb", "nccc", "nddd", "neee", "nfff"]
    },
    {
      "filename": "/core/modules/simpletest/tests/file.test",
      "ignoreWords": ["NONEXISTINGFILENAME", "ufail"]
    },
    {
      "filename": "/core/themes/bartik/color/color.inc",
      "ignoreWords": ["Blackdrop"]
    }
  ]
}

Example Repository

https://github.com/backdrop/backdrop

Code of Conduct

klonos commented 2 days ago

Another thing that I would like to please get an answer for is the following: does this feature allow complex code suggestions, or is it only "simple words". For instance, I have kept things simple, by adding the following:

  "flagWords": [
    "clicked_button: triggering_element"
  ],

However, once we get the overall flagWords/suggestions thing working, I would like to ideally have this instead:

  "flagWords": [
    "$form_state['clicked_button']: $form_state['triggering_element']"
  ],

Would that be something that works with this feature, or would it be "too confusing" and adding such complex words/suggestions would break things?

Another similar use case is the use of phrases instead of words: would things work with multiple words for the flagged word and suggestion(s), or do the word/suggestion pairs need to be single words? In other words, would something like this be expected to work? (notice the spaces in the items between the : separator):

  "flagWords": [
    "say one thing: suggest another thing",
    "singleword: suggestion with many words",
    "multiple words forbidden: singlewordsuggestion"
  ],

PS: Admittedly, the above is not "spell-checking" per se, rather than using CSpell as a means to flag usage of deprecated things in code - however I still think that it'd be useful.

Jason3S commented 2 days ago

@klonos,

CSpell Action

Thank you for the link to the repository. I took a quick look. It seems the cspell action is failing to load the configuration:

image

I think changing the path to the config to be relative might help. The error message is confusing and it should have failed the run, but it didn't. 🤔

      - uses: actions/checkout@v4
        name: Checkout code
      - uses: streetsidesoftware/cspell-action@v5
        name: Run CSpell
        with:
          incremental_files_only: true
-         config: .cspell/cspell.json
+         config: "./.cspell/cspell.json"

Suggestions

You got the format correct. To test it, I added blacklist to the README and misspelled Backddrop.

npx cspell -c ./.cspell/cspell.json README.md --no-progress
image

To make things easier, I suggest either moving all of .cspell to .config or to add a cspell.json file to the root or in .config.

.config/cpsell.json

{
  "import": ["../.cspell/cspell.json"]
}

If you do that, you can test with:

npx cspell .
klonos commented 2 days ago

Thanks for taking the time to have a look and make suggestions @Jason3S 🙏🏼 ...I'll have a crack at it and report back soon.

But as you said, it is odd that it is not failing in any obvious way - CSpell is otherwise working (as in it is detecting typos in our PRs and seems to be respecting words/files excluded or words added to the custom dictionary etc.).

klonos commented 2 days ago

...sorry, I just realized that the suggestion here is only for the command line, rather than the GitHub Action. I don't suppose there's any progress on https://github.com/streetsidesoftware/cspell-action/issues/1455 though (?).

Jason3S commented 1 day ago

@klonos,

If possible, please upgrade the action to v6: - uses: streetsidesoftware/cspell-action@v6

image
klonos commented 1 day ago

Yes, thank you @Jason3S ...I will do that 👍🏼 (was actually looking at the release history last night to catch up on changes). I'll let you know once done.

Jason3S commented 1 day ago

@klonos,

I have release cspell-action@v6.5.0 that contains suggestions. Please let me know how well it works for you.

I'm going to close this for now, because cspell is working as expected and suggestions were a feature request: cspell-action: Support suggestions #1455.

Kind regards, Jason

klonos commented 1 day ago

Thanks @Jason3S 🙏🏼 ...this is exciting! ...and to see how serious I was on actually testing this on first chance, I had this tab open in a dedicated screen, as a constant reminder 😁 ...just real life/work seems to be getting in the way of fun things.

Jason3S commented 1 day ago

Another thing that I would like to please get an answer for is the following: does this feature allow complex code suggestions, or is it only "simple words". For instance, I have kept things simple, by adding the following:

  "flagWords": [
    "clicked_button: triggering_element"
  ],

However, once we get the overall flagWords/suggestions thing working, I would like to ideally have this instead:

  "flagWords": [
    "$form_state['clicked_button']: $form_state['triggering_element']"
  ],

Would that be something that works with this feature, or would it be "too confusing" and adding such complex words/suggestions would break things?

Another similar use case is the use of phrases instead of words: would things work with multiple words for the flagged word and suggestion(s), or do the word/suggestion pairs need to be single words? In other words, would something like this be expected to work? (notice the spaces in the items between the : separator):

  "flagWords": [
    "say one thing: suggest another thing",
    "singleword: suggestion with many words",
    "multiple words forbidden: singlewordsuggestion"
  ],

PS: Admittedly, the above is not "spell-checking" per se, rather than using CSpell as a means to flag usage of deprecated things in code - however I still think that it'd be useful.

I just realized that I didn't fully respond to this.

At the moment, there isn't any plan to support complex detection and suggestions. It has been asked for, but most of the requests were like "$form_state['clicked_button']: $form_state['triggering_element']" which as you point out, is not a spelling issue, but more of a lint issue.

In addition, detecting multi word issues has challenges:

Given:

flagWords:
- all four one->all for one

Detecting the following would be easy:

One for all and all four one.

But what about when there is a line break?

One for all and all four one.

Or in code: 'one for all and all' + ' four one.'?