Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: syntax errors in JS example sections #18186

Merged
merged 5 commits into from
Jul 10, 2022

Conversation

lionralfs
Copy link
Contributor

@lionralfs lionralfs commented Jul 10, 2022

Summary

Hey everyone!

I've come across this modernization effort for JS code samples and decided to test the feasibility of using ESLint to automate some of the work. However, I've quickly noticed that around 500 code samples in the files/en-us/web/api/ directory contain syntax errors (ESLint reports parsing errors). I then started to fix some of them, which is the state of this PR. Before proceeding to fix the remaining errors I wanted to ask for some maintainer input on whether this is worth pursuing. I imagine fixing everything in one big PR makes reviewing somewhat painful.

I've purposely not commited my ESLint setup, but if you want to try it for yourself, this is how I did it:

  1. npm install --save-dev eslint eslint-plugin-markdown
  2. Modify .eslintrc.js:
module.exports = {
  extends: [
    // "plugin:json/recommended"
    "plugin:markdown/recommended",
  ],
  parserOptions: {
    ecmaVersion: "latest",
    // allows top-level await among other things
    sourceType: "module",
  },
  env: {
    es6: true,
    browser: true,
    node: true,
  },
};
  1. npx eslint "files/en-us/web/api/**/*.md"

Obviously this is not a perfect setup but it already highlights some existing issues with the code samples.

This PR…

  • Adds a new document
  • Rewrites (or significantly expands) a document
  • Fixes a typo, bug, or other error

@lionralfs lionralfs requested a review from a team as a code owner July 10, 2022 15:58
@lionralfs lionralfs requested review from jpmedley and removed request for a team July 10, 2022 15:58
@github-actions github-actions bot added the Content:WebAPI Web API docs label Jul 10, 2022
@lionralfs lionralfs marked this pull request as draft July 10, 2022 15:59
@github-actions

This comment was marked as resolved.

@Josh-Cena
Copy link
Member

Ahhh, I'm also looking into eslint-plugin-markdown. Glad someone is on the same page! Yes, please do go ahead—these PRs are easy to review so just keep it at around ~50 files changed. (It would be a problem if we have 100 syntax errors in our code...)

@lionralfs
Copy link
Contributor Author

So you would prefer PRs in batches of 50 changed files? I've just checked and ~380 files have at least one syntax (parsing) error.

@Josh-Cena
Copy link
Member

Josh-Cena commented Jul 10, 2022

Ah, let's spread them across 4 PRs then—about 100 lines of diff per PR would be the best.

@lionralfs
Copy link
Contributor Author

Just FYI: I'm planning to add a few more fixes to this PR before I open a new one

@teoli2003
Copy link
Contributor

Yes, our idea is to have a linter running periodically (or automatically, we'll see). For this to work, we need to run it once (like you do), find the errors, and fix them.

Then we can run it weekly, and the weekly PR will be small enough to be reviewed quickly.

@teoli2003
Copy link
Contributor

Just FYI: I'm planning to add a few more fixes to this PR before I open a new one

You are welcome.

@@ -19,7 +19,7 @@ The **`delete()`** method of the {{domxref("XRAnchor")}} interface removes an an
## Syntax

```js
delete()
anchor.delete()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily a syntax fix but this stops the parser from complaining since delete is an operator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep—we do the same for generator.return(). See also https://github.com/orgs/mdn/discussions/149

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we don't update these. We will change the type from js to jssyntax very soon. Meanwhile, we should ignore this one.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh... Maybe until we reach a resolution for https://github.com/orgs/mdn/discussions/148...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether you agreed on this change or want me to revert it. I'd appreciate an update

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh... Maybe until we reach a resolution for https://github.com/orgs/mdn/discussions/148...

Yes, let's keep the original until this discussion is done (and the syntax section in the meta-docs).

@lionralfs
Copy link
Contributor Author

Some things I'm noticing:

  • Sometimes, the language mode is set incorrectly (html/js). For pseudocode, I've removed it completely
  • Some variable names are problematic in certain contexts, for example: the parser doesn't like default in ESM contexts. Probably relevant for further work (see comment above regarding automated linting)

@@ -120,7 +120,7 @@ function openRequestedPopup() {

## Best practices

```js
```html
<script type="text/javascript">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep it js and remove the <script> element (3 times in this file)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As those code blocks still include html, would you use a js code block followed by a html one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please.

@lionralfs lionralfs marked this pull request as ready for review July 10, 2022 17:44
@Josh-Cena
Copy link
Member

Some variable names are problematic in certain contexts, for example: the parser doesn't like default in ESM contexts.

We should rename those variables.

@teoli2003
Copy link
Contributor

Some variable names are problematic in certain contexts, for example: the parser doesn't like default in ESM contexts.

We should rename those variables.

Yes, variables (or pseudo-variables in syntax sections) that have problematic names should be replaced: people often cut and past these, and then modify (actually declaring the variable with the same name, etc.)

Copy link
Contributor

@teoli2003 teoli2003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(See comments above)

@lionralfs lionralfs force-pushed the fix-js-example-syntax-errors branch from 5f0d73c to cb67bcc Compare July 10, 2022 19:11
@lionralfs lionralfs requested a review from teoli2003 July 10, 2022 19:40
Copy link
Contributor

@teoli2003 teoli2003 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot. I'm really happy with the idea to use ESLint to find and fix syntax errors.

@teoli2003 teoli2003 merged commit 5b82af5 into mdn:main Jul 10, 2022
@lionralfs lionralfs deleted the fix-js-example-syntax-errors branch July 10, 2022 19:55
@OnkarRuikar
Copy link
Contributor

OnkarRuikar commented Jul 17, 2022

I've purposely not commited my ESLint setup..

@lionralfs it would be great if you commit just the setup(no .md files) on a feature branch in your fork(or a PR here) and share with us.

We need to have discussion about which rules to disable. For example, we need to disable no-undef, no-console, no-alert etc.(this will save us from annotating hundreds of code blocks to disable the rules individually).
The idea is when we'll reach zero .md files flagged in content(after fixing them) then we can commit the ESLint setup along with our agreed config file.

@Josh-Cena
Copy link
Member

We'll start by fixing syntax errors so that ESLint can work at all—and then negotiating with the parser about what to parse and what to report. For example I still strongly believe

```js
const a = 1;
// OR
const a = 2;
```

Is strictly better than two code blocks or using two identifiers, just to make the parser happy. Making this work probably requires tweaking the parser a bit.

@Josh-Cena
Copy link
Member

Oh btw, I think we can have parserOptions and env right now in the main branch's config—just not the plugins and rules sections. When I'm writing random test scripts I often come across ESLint parser errors like "class is a reserved word" because the parser is malconfigured.

@lionralfs
Copy link
Contributor Author

lionralfs commented Jul 17, 2022

I've pushed my ESLint setup to my fork: lionralfs:eslint-config
This also includes an .eslintignore file which I've been using to skip over certain .md files with more complicated syntax errors.
I've been documenting what I've skipped in various comments/PR descriptions and will most likely compile them into a single list when I'm done with the remaining errors (which should only be 1 more PR).

@lionralfs
Copy link
Contributor Author

lionralfs commented Jul 17, 2022

Ok so here we go. PR #18442 is the fifth and last (for now) PR to fix syntax/parsing errors.

As promised, I'll document all encountered issues that I've skipped here:

List of remaining syntax parsing/errors

The .eslintignore file I've used to exclude all of these

files/en-us/web/api/xranchor/delete/index.md
files/en-us/web/api/webvr_api/using_the_webvr_api/index.md
files/en-us/web/api/webglrenderingcontext/texparameter/index.md
files/en-us/web/api/webgl_api/tutorial/using_textures_in_webgl/index.md
files/en-us/web/api/webgl_api/tutorial/lighting_in_webgl/index.md
files/en-us/web/api/webgl_api/tutorial/creating_3d_objects_using_webgl/index.md
files/en-us/web/api/webgl_api/by_example/textures_from_code/index.md
files/en-us/web/api/webgl_api/by_example/scissor_animation/index.md
files/en-us/web/api/webgl_api/by_example/raining_rectangles/index.md
files/en-us/web/api/webgl_api/by_example/hello_vertex_attributes/index.md
files/en-us/web/api/webgl_api/by_example/hello_glsl/index.md
files/en-us/web/api/web_speech_api/using_the_web_speech_api/index.md
files/en-us/web/api/web_crypto_api/non-cryptographic_uses_of_subtle_crypto/index.md
files/en-us/web/api/web_audio_api/web_audio_spatialization_basics/index.md
files/en-us/web/api/web_audio_api/visualizations_with_web_audio_api/index.md
files/en-us/web/api/web_audio_api/using_audioworklet/index.md
files/en-us/web/api/web_audio_api/simple_synth/index.md
files/en-us/web/api/usbdevice/opened/index.md
files/en-us/web/api/urlsearchparams/delete/index.md
files/en-us/web/api/transformstreamdefaultcontroller/error/index.md
files/en-us/web/api/stylepropertymap/delete/index.md
files/en-us/web/api/streams_api/using_readable_streams/index.md
files/en-us/web/api/shadowroot/index.md
files/en-us/web/api/rtcpeerconnection/getstats/index.md
files/en-us/web/api/readablestreambyobreader/read/index.md
files/en-us/web/api/readablebytestreamcontroller/error/index.md
files/en-us/web/api/paintworklet/registerpaint/index.md
files/en-us/web/api/mediastream_image_capture_api/index.md
files/en-us/web/api/mediadevices/getusermedia/index.md
files/en-us/web/api/media_streams_api/taking_still_photos/index.md
files/en-us/web/api/indexeddb_api/checking_when_a_deadline_is_due/index.md
files/en-us/web/api/idbobjectstore/delete/index.md
files/en-us/web/api/idbcursor/delete/index.md
files/en-us/web/api/idbcursor/continue/index.md
files/en-us/web/api/headers/delete/index.md
files/en-us/web/api/formdata/delete/index.md
files/en-us/web/api/fontfaceset/delete/index.md
files/en-us/web/api/elementinternals/states/index.md
files/en-us/web/api/element/replacewith/index.md
files/en-us/web/api/element/remove/index.md
files/en-us/web/api/element/prepend/index.md
files/en-us/web/api/element/append/index.md
files/en-us/web/api/customstateset/delete/index.md
files/en-us/web/api/css_painting_api/guide/index.md
files/en-us/web/api/cookiestore/delete/index.md
files/en-us/web/api/cachestorage/delete/index.md
files/en-us/web/api/cache/delete/index.md
files/en-us/web/api/abortsignal/abort/index.md

@teoli2003
Copy link
Contributor

Hi! I've created a discussion with the amazing list of errors you found. I grouped them into categories, problems, so that we can discuss them and find solutions: mdn/discussions#158

That way, we will have the discussion in one well-visible place rather than buried in PR.

🎉 Thanks a lot for this amazing work, @lionralfs! Much, much appreciated. 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:WebAPI Web API docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants