Adding eslint for code .\src\ (#17)

This commit is contained in:
TfTHacker 2021-10-30 20:04:07 +02:00 committed by GitHub
parent 02ac033b15
commit 6fdd374cb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 9 deletions

2
.eslintignore Normal file
View File

@ -0,0 +1,2 @@
npm node_modules
build

22
.eslintrc Normal file
View File

@ -0,0 +1,22 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"sourceType": "module"
},
"rules": {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": ["error", { "args": "none" }],
"@typescript-eslint/ban-ts-comment": "off",
"no-prototype-builtins": "off",
"@typescript-eslint/no-empty-function": "off"
}
}

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# vscode
.vscode
# Intellij # Intellij
*.iml *.iml
.idea .idea

View File

@ -33,7 +33,7 @@ Quick starting guide for new plugin devs:
- Update your `manifest.json` with your new version number, such as `1.0.1`, and the minimum Obsidian version required for your latest release. - Update your `manifest.json` with your new version number, such as `1.0.1`, and the minimum Obsidian version required for your latest release.
- Update your `versions.json` file with `"new-plugin-version": "minimum-obsidian-version"` so older versions of Obsidian can download an older version of your plugin that's compatible. - Update your `versions.json` file with `"new-plugin-version": "minimum-obsidian-version"` so older versions of Obsidian can download an older version of your plugin that's compatible.
- Create new GitHub release using your new version number as the "Tag version". Use the exact version number, don't include a prefix `v`. See here for an example: https://github.com/obsidianmd/obsidian-sample-plugin/releases - Create new GitHub release using your new version number as the "Tag version". Use the exact version number, don't include a prefix `v`. See here for an example: https://github.com/obsidianmd/obsidian-sample-plugin/releases
- Upload the files `manifest.json`, `main.js`, `styles.css` as binary attachments. - Upload the files `manifest.json`, `main.js`, `styles.css` as binary attachments. Note: The manifest.json file must be in two places, first the root path of your repository and also in the release.
- Publish the release. - Publish the release.
### Adding your plugin to the community plugin list ### Adding your plugin to the community plugin list
@ -52,6 +52,17 @@ Quick starting guide for new plugin devs:
- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`. - Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`.
### Improve code quality with eslint (optional)
- [ESLint](https://eslint.org/) is a tool that analyzes your code to quickly find problems. You can run ESLint against your plugin to find common bugs and ways to improve your code.
- To use eslint with this project, make sure to install eslint from terminal:
- `npm install -g eslint`
- To use eslint to analyze this project use this command:
- `eslint main.ts`
- eslint will then create a report with suggestions for code improvement by file and line number.
- If your source code is in a folder, such as `src`, you can use eslint with this command to analyze all files in that folder:
- `eslint .\src\`
### API Documentation ### API Documentation
See https://github.com/obsidianmd/obsidian-api See https://github.com/obsidianmd/obsidian-api

12
main.ts
View File

@ -15,7 +15,7 @@ export default class MyPlugin extends Plugin {
await this.loadSettings(); await this.loadSettings();
// This creates an icon in the left ribbon. // This creates an icon in the left ribbon.
let ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => { const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
// Called when the user clicks the icon. // Called when the user clicks the icon.
new Notice('This is a notice!'); new Notice('This is a notice!');
}); });
@ -23,7 +23,7 @@ export default class MyPlugin extends Plugin {
ribbonIconEl.addClass('my-plugin-ribbon-class'); ribbonIconEl.addClass('my-plugin-ribbon-class');
// This adds a status bar item to the bottom of the app. Does not work on mobile apps. // This adds a status bar item to the bottom of the app. Does not work on mobile apps.
let statusBarItemEl = this.addStatusBarItem(); const statusBarItemEl = this.addStatusBarItem();
statusBarItemEl.setText('Status Bar Text'); statusBarItemEl.setText('Status Bar Text');
// This adds a simple command that can be triggered anywhere // This adds a simple command that can be triggered anywhere
@ -49,7 +49,7 @@ export default class MyPlugin extends Plugin {
name: 'Open sample modal (complex)', name: 'Open sample modal (complex)',
checkCallback: (checking: boolean) => { checkCallback: (checking: boolean) => {
// Conditions to check // Conditions to check
let markdownView = this.app.workspace.getActiveViewOfType(MarkdownView); const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (markdownView) { if (markdownView) {
// If checking is true, we're simply "checking" if the command can be run. // If checking is true, we're simply "checking" if the command can be run.
// If checking is false, then we want to actually perform the operation. // If checking is false, then we want to actually perform the operation.
@ -95,12 +95,12 @@ class SampleModal extends Modal {
} }
onOpen() { onOpen() {
let {contentEl} = this; const {contentEl} = this;
contentEl.setText('Woah!'); contentEl.setText('Woah!');
} }
onClose() { onClose() {
let {contentEl} = this; const {contentEl} = this;
contentEl.empty(); contentEl.empty();
} }
} }
@ -114,7 +114,7 @@ class SampleSettingTab extends PluginSettingTab {
} }
display(): void { display(): void {
let {containerEl} = this; const {containerEl} = this;
containerEl.empty(); containerEl.empty();

View File

@ -12,9 +12,11 @@
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/node": "^16.11.1", "@types/node": "^16.11.1",
"esbuild": "0.13.8", "esbuild": "0.13.11",
"obsidian": "^0.12.17", "obsidian": "^0.12.17",
"tslib": "2.3.1", "tslib": "2.3.1",
"typescript": "4.4.4" "typescript": "4.4.4",
"@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0"
} }
} }