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

Enforce code style via linter (part of #446) #448

Merged
merged 15 commits into from
Sep 26, 2021

Conversation

danyill
Copy link
Contributor

@danyill danyill commented Sep 17, 2021

This will eventually close #446 but is draft for now.

Updating dependencies to use standard for lining has resulted in package-lock being rewritten to a new version.

Immediate output is: 3242 problems.
After auto-fixing we get 1269 problems.
Most seem to be spaces and tabs and things which are never used.
I'm happy to go through these.

I guess ideally we would wait until we had no open PRs before doing something like this but this PR just allows us to understand the implications.

WDYT?

@danyill
Copy link
Contributor Author

danyill commented Sep 17, 2021

I had thought it would be good to set up a test framework prior to making "big changes" but the extension does still seem to work at this point. I have no experience as to whether linters are likely to break working code.

@ggrossetie
Copy link
Member

Updating dependencies to use standard for lining has resulted in package-lock being rewritten to a new version.

The reason package-lock.json contains a lot of changes is because you are using npm <= 6. You should use npm 7 on this project (with Node >= 14).
Currently, the lock file is using version 2 (you can see it in the diff: lockfileVersion: 2).
If you install dependencies with npm <= 6 it will regenerate the lock file using version 1.

I guess ideally we would wait until we had no open PRs before doing something like this

Yes, otherwise we will need to rebase and resolve conflicts.

I had thought it would be good to set up a test framework prior to making "big changes" but the extension does still seem to work at this point.

Indeed that would be good but it will take time until we have a good test overage.

I have no experience as to whether linters are likely to break working code.

I believe that standard --fix is safe unless there's a bug in standard.
Having said that, it's probably a good idea to perform extensive (manual) tests once all lint issues are fixed but before merging this pull request.

@danyill
Copy link
Contributor Author

danyill commented Sep 19, 2021

Something is happening -- and it certainly took some time.

23:03 $ npm run lint

> asciidoctor-vscode@0.0.0 lint
> eslint 'src/**/*.ts' 'src-preview/**/*.ts' --format unix

/home/mulhollandd/Documents/asciidoctor-vscode/src/asciidocExtensions.ts:12:31: 'Thenable' is not defined. [Error/no-undef]
/home/mulhollandd/Documents/asciidoctor-vscode/src/asciidocExtensions.ts:21:30: 'Thenable' is not defined. [Error/no-undef]
/home/mulhollandd/Documents/asciidoctor-vscode/src/asciidocExtensions.ts:51:36: 'Thenable' is not defined. [Error/no-undef]
/home/mulhollandd/Documents/asciidoctor-vscode/src/commands/openDocumentLink.ts:40:30: Expected 'undefined' and instead saw 'void'. [Error/no-void]
/home/mulhollandd/Documents/asciidoctor-vscode/src/commands/openDocumentLink.ts:42:21: Expected 'undefined' and instead saw 'void'. [Error/no-void]
/home/mulhollandd/Documents/asciidoctor-vscode/src/features/workspaceSymbolProvider.ts:13:30: 'Thenable' is not defined. [Error/no-undef]
/home/mulhollandd/Documents/asciidoctor-vscode/src/features/workspaceSymbolProvider.ts:99:47: 'Thenable' is not defined. [Error/no-undef]
/home/mulhollandd/Documents/asciidoctor-vscode/src/features/workspaceSymbolProvider.ts:135:59: 'Thenable' is not defined. [Error/no-undef]
/home/mulhollandd/Documents/asciidoctor-vscode/src/security.ts:23:91: 'Thenable' is not defined. [Error/no-undef]
/home/mulhollandd/Documents/asciidoctor-vscode/src/security.ts:29:57: 'Thenable' is not defined. [Error/no-undef]
/home/mulhollandd/Documents/asciidoctor-vscode/src/security.ts:59:99: 'Thenable' is not defined. [Error/no-undef]
/home/mulhollandd/Documents/asciidoctor-vscode/src/security.ts:72:63: 'Thenable' is not defined. [Error/no-undef]

The Thenable didn't have a type definition. I took one from the Markdown repo and placed in src/typings. But I can't seem to get eslint to recognise it. And I'm a little uncertain about the undefined/void at the moment.

I haven't done any significant testing yet and I would want to spend some time on it because there were quite a few non-automatic changes and I may have introduced errors (particularly around isNullOrUndefined where I decided it shouldn't always need to be written as the or of both expressions but I may not have always chosen correctly).

@ggrossetie
Copy link
Member

The Thenable didn't have a type definition. I took one from the Markdown repo and placed in src/typings. But I can't seem to get eslint to recognise it.

I think we need to add:

{
  "env": {
    "node": true,
    "es6": true
  }
}

in the .eslintrc.js file

And I'm a little uncertain about the undefined/void at the moment.

I can't understand why we do such things:

Promise.resolve(void 0)
        .then(() => vscode.commands.executeCommand('vscode.open', resource))
        .then(() => void 0)

vscode.commands.executeCommand('vscode.open', resource) is asynchronous and void 0 is a function that returns undefined but we could write:

public async execute(args: OpenDocumentLinkArgs) {
  // ...
  await vscode.commands.executeCommand('vscode.open', resource)
  return
}

That's almost what the Markdown plugin is doing: https://github.com/microsoft/vscode/blob/2f33470e28c1a3440b2c5e0ea9205b1d0a1e6f1b/extensions/markdown-language-features/src/commands/openDocumentLink.ts#L66-L82

@ggrossetie ggrossetie marked this pull request as ready for review September 23, 2021 07:01
@ggrossetie
Copy link
Member

@danyill I've replaced Thenable by Promise (included in Node typing) and replaced Promise.resolve(void 0) by async/await.

@ggrossetie
Copy link
Member

I've also enabled GitHub Action CI workflow (lint and compile) on pull requests

@danyill
Copy link
Contributor Author

danyill commented Sep 23, 2021

Thank you, that's marvellous. I'd like to spend an hour or two on the weekend seeing if I can find obvious flaws and then hopefully we can merge a few things.

@danyill
Copy link
Contributor Author

danyill commented Sep 25, 2021

I did some tests.
I only tested on Linux.
I only tested on VS Code 1.60.1.

  • Check that syntax highlighting still appears
  • Check that preview runs (locked and preview to the side)
  • Check for no errors in web view (Developer Tools) for a simple document
  • Check that document symbols exist
  • Check that I can create a simple Export to PDF with wkhtmltopdf (Initial Fail had to expand null/undefined)
  • Check that I can create a simple Export to PDF with asciidoctor-pdf (Initial Fail, looks like the option check wasn't appropriately case sensitive)
  • Check that I can save to HTML
  • Check that I can save to Docbook
  • Check that Docbook 4.5 conversion works
  • Check that with security disabled I see admonitions
  • Check that error diagnostics work by adding an incorrect heading level
  • Check that scroll sync works and can be disabled and re-enabled with options (Initial Fail, it seems this option requires an extension restart but they do work. I'll log an issue for this soon)
  • Check that links work correctly (FAIL they don't but I think there are issues logged about this so I haven't fixed here)
  • Check that using Asciidoctor standard styling works (Initial Fail, requires preview to be closed and then reopened)
  • Check that Kroki works
  • Check that an image paste works
  • Test that showing or hiding front matter setting works
  • Check that Asciimath works in the preview
  • Check that snippet insertion works
  • Check that the outline is populated and I can go to sections with Ctrl+Shift+O

In general things seem to be working OK, somewhat obscured by the number of bugs and usability issues on various features.
I think any breakages will not be difficult to fix if they occur.

If you're happy with this PR @Mogztter feel free to merge away otherwise I'll be happy to look into any aspect in more detail.

@ggrossetie
Copy link
Member

ggrossetie commented Sep 25, 2021

Check that links work correctly (FAIL they don't but I think there are issues logged about this so I haven't fixed here)

From what I seen in the code (before your changes) it won't work because the regular expressions are designed to work with Markdown syntax (not AsciiDoc syntax).

private readonly linkPattern = /(\[[^\]]*\]\(\s*)((([^\s\(\)]|\(\S*?\))+))\s*(".*?")?\)/g;
private readonly referenceLinkPattern = /(\[([^\]]+)\]\[\s*?)([^\s\]]*?)\]/g;
private readonly definitionPattern = /^([\t ]*\[([^\]]+)\]:\s*)(\S+)/gm;

If you're happy with this PR @Mogztter feel free to merge away otherwise I'll be happy to look into any aspect in more detail.

If you can confirm what exactly is not working with links (to make sure that this is related to existing issues) that would be great. Then, I think we can safely merge this pull request.

Great work 🎉

@danyill
Copy link
Contributor Author

danyill commented Sep 25, 2021 via email

@danyill
Copy link
Contributor Author

danyill commented Sep 25, 2021

If you can confirm what exactly is not working with links (to make sure that this is related to existing issues) that would be great. Then, I think we can safely merge this pull request.

I've had a look, I think it's unrelated.

Links within the preview were only recently added (see #397 and #400) and worked fine previously. I think the Webview is now handled differently due to upstream changes and file links end up with a prefix of https://file+.vscode-resource.vscode-webview.net/. This doesn't work with the previously link handling strategy so we need to make sure these are handled differently so they are resolved correctly.

if (passThroughLinkSchemes.some((scheme) => node.href.startsWith(scheme))) {
return;
}
hrefText = node.getAttribute('href');
}
// If original link doesn't look like a url, delegate back to VS Code to resolve
if (!/^[a-z\-]+:/i.test(hrefText)) {
messaging.postMessage('clickLink', { href: hrefText });
event.preventDefault();
event.stopPropagation();
return;
}

At least half a fix looks something like the following where we make sure we don't pass through file links and then ensure that it is handled by VS code to resolve.

			let hrefText = node.getAttribute('data-href');
			if (!hrefText) {
				// Pass through known schemes
				if (passThroughLinkSchemes.some((scheme) => node.href.startsWith(scheme) && !node.href.startsWith('https://file+.vscode-resource.vscode-webview.net/'))) {
					return;
				}
				hrefText = node.getAttribute('href');
			}

			// If original link doesn't look like a url, delegate back to VS Code to resolve
			if (!/^[a-z\-]+:/i.test(hrefText) || hrefText.startsWith('http://https//file+.vscode-resource.vscode-webview.net')) {
				messaging.postMessage('clickLink', { href: hrefText });
				event.preventDefault();
				event.stopPropagation();
				return;
			}

I'll look into this as part of #435.

@ggrossetie
Copy link
Member

Perfect, then we are good to go 😀

@ggrossetie ggrossetie merged commit cd477c7 into asciidoctor:master Sep 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enforce a code style (via a linter) to make the code consistent
2 participants