From d39b1eab515e5707efb73c7ecadcc357dc9b3615 Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Tue, 2 Apr 2024 15:44:10 +0200 Subject: [PATCH] Fix eslint warnings and add build/lint step to CI (#116) And ignore some of the most noisy ones for now. Should fix these later obviously, but for now I was mainly just concerned with fixing the "are you sure you want to continue despite the errors reported" warning that would be shown each time one started a debugging session. Signed-off-by: Anders Eknert --- .eslintrc.yml | 19 ++++ .github/workflows/build.yaml | 34 +++++++ .gitignore | 2 + package-lock.json | 181 +++++++++++++++++++++++------------ package.json | 13 ++- src/extension.ts | 82 ++++++++-------- src/github-installer.ts | 2 +- src/opa.ts | 30 +++--- src/util.ts | 6 +- 9 files changed, 238 insertions(+), 131 deletions(-) create mode 100644 .eslintrc.yml create mode 100644 .github/workflows/build.yaml diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 0000000..ff6281c --- /dev/null +++ b/.eslintrc.yml @@ -0,0 +1,19 @@ +env: + browser: true + es2021: true +extends: + - eslint:recommended + - plugin:@typescript-eslint/recommended +parser: '@typescript-eslint/parser' +parserOptions: + ecmaVersion: latest + sourceType: module +plugins: + - '@typescript-eslint' +rules: + '@typescript-eslint/no-explicit-any': 'off' + '@typescript-eslint/no-var-requires': 'off' + '@typescript-eslint/no-unused-vars': 'off' + 'no-undef': 'off' + 'no-case-declarations': 'off' + 'require-yield': 'off' diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..d5ed904 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,34 @@ +name: Build + +on: + push: + pull_request: + branches: [ "master" ] + workflow_dispatch: + +jobs: + build: + name: Build and lint + runs-on: ubuntu-latest + permissions: + contents: read + security-events: write + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + npm install --include=dev + + - name: Compile TypeScript + run: npm run compile + + - name: Run ESLint + run: npx eslint --format @microsoft/eslint-formatter-sarif --output-file eslint-results.sarif . + continue-on-error: true + + - name: Upload analysis results to GitHub + uses: github/codeql-action/upload-sarif@v3 + with: + sarif_file: eslint-results.sarif diff --git a/.gitignore b/.gitignore index 86466ec..b587f73 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ node_modules .vscode-test/ *.vsix .DS_Store + +eslint-results.sarif diff --git a/package-lock.json b/package-lock.json index 079c794..4b4b770 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,13 +14,15 @@ "vscode-languageclient": "^9.0.1" }, "devDependencies": { + "@microsoft/eslint-formatter-sarif": "^3.0.0", "@types/mocha": "10.0.6", "@types/node": "20.11.13", "@types/vscode": "1.75.0", - "@typescript-eslint/eslint-plugin": "^6.14.0", - "@typescript-eslint/parser": "^6.14.0", - "eslint": "^8.56.0", - "typescript": "5.3.3" + "@typescript-eslint/eslint-plugin": "^7.2.0", + "@typescript-eslint/parser": "^7.2.0", + "eslint": "^8.57.0", + "typescript": "^5.3.3", + "typescript-eslint": "^7.2.0" }, "engines": { "vscode": "^1.75.0" @@ -105,9 +107,9 @@ } }, "node_modules/@eslint/js": { - "version": "8.56.0", - "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.56.0.tgz", - "integrity": "sha512-gMsVel9D7f2HLkBma9VbtzZRehRogVRfbr++f06nL2vnCGCNlzOD+/MUov/F4p8myyAHspEhVobgjpX64q5m6A==", + "version": "8.57.0", + "resolved": "https://registry.npmjs.org/@eslint/js/-/js-8.57.0.tgz", + "integrity": "sha512-Ys+3g2TaW7gADOJzPt83SJtCDhMjndcDMFVQ/Tj9iA1BfJzFKD9mAUXT3OenpuPHbI6P/myECxRJrofUsDx/5g==", "dev": true, "engines": { "node": "^12.22.0 || ^14.17.0 || >=16.0.0" @@ -196,6 +198,21 @@ "integrity": "sha512-6EwiSjwWYP7pTckG6I5eyFANjPhmPjUX9JRLUSfNPC7FX7zK9gyZAfUEaECL6ALTpGX5AjnBq3C9XmVWPitNpw==", "dev": true }, + "node_modules/@microsoft/eslint-formatter-sarif": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/@microsoft/eslint-formatter-sarif/-/eslint-formatter-sarif-3.0.0.tgz", + "integrity": "sha512-KIKkT44hEqCzqxODYwFMUvYEK0CrdHx/Ll9xiOWgFbBSRuzbxmVy4d/tzfgoucGz72HJZNOMjuyzFTBKntRK5Q==", + "dev": true, + "dependencies": { + "eslint": "^8.9.0", + "jschardet": "latest", + "lodash": "^4.17.14", + "utf8": "^3.0.0" + }, + "engines": { + "node": ">= 14" + } + }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", @@ -253,9 +270,9 @@ } }, "node_modules/@types/semver": { - "version": "7.5.6", - "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.6.tgz", - "integrity": "sha512-dn1l8LaMea/IjDoHNd9J52uBbInB796CDffS6VdIxvqYCPSG0V0DzHp76GpaWnlhg88uYyPbXCDIowa86ybd5A==", + "version": "7.5.8", + "resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.8.tgz", + "integrity": "sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ==", "dev": true }, "node_modules/@types/vscode": { @@ -265,16 +282,16 @@ "dev": true }, "node_modules/@typescript-eslint/eslint-plugin": { - "version": "6.20.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-6.20.0.tgz", - "integrity": "sha512-fTwGQUnjhoYHeSF6m5pWNkzmDDdsKELYrOBxhjMrofPqCkoC2k3B2wvGHFxa1CTIqkEn88nlW1HVMztjo2K8Hg==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-7.2.0.tgz", + "integrity": "sha512-mdekAHOqS9UjlmyF/LSs6AIEvfceV749GFxoBAjwAv0nkevfKHWQFDMcBZWUiIC5ft6ePWivXoS36aKQ0Cy3sw==", "dev": true, "dependencies": { "@eslint-community/regexpp": "^4.5.1", - "@typescript-eslint/scope-manager": "6.20.0", - "@typescript-eslint/type-utils": "6.20.0", - "@typescript-eslint/utils": "6.20.0", - "@typescript-eslint/visitor-keys": "6.20.0", + "@typescript-eslint/scope-manager": "7.2.0", + "@typescript-eslint/type-utils": "7.2.0", + "@typescript-eslint/utils": "7.2.0", + "@typescript-eslint/visitor-keys": "7.2.0", "debug": "^4.3.4", "graphemer": "^1.4.0", "ignore": "^5.2.4", @@ -290,8 +307,8 @@ "url": "https://opencollective.com/typescript-eslint" }, "peerDependencies": { - "@typescript-eslint/parser": "^6.0.0 || ^6.0.0-alpha", - "eslint": "^7.0.0 || ^8.0.0" + "@typescript-eslint/parser": "^7.0.0", + "eslint": "^8.56.0" }, "peerDependenciesMeta": { "typescript": { @@ -300,15 +317,15 @@ } }, "node_modules/@typescript-eslint/parser": { - "version": "6.20.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-6.20.0.tgz", - "integrity": "sha512-bYerPDF/H5v6V76MdMYhjwmwgMA+jlPVqjSDq2cRqMi8bP5sR3Z+RLOiOMad3nsnmDVmn2gAFCyNgh/dIrfP/w==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-7.2.0.tgz", + "integrity": "sha512-5FKsVcHTk6TafQKQbuIVkXq58Fnbkd2wDL4LB7AURN7RUOu1utVP+G8+6u3ZhEroW3DF6hyo3ZEXxgKgp4KeCg==", "dev": true, "dependencies": { - "@typescript-eslint/scope-manager": "6.20.0", - "@typescript-eslint/types": "6.20.0", - "@typescript-eslint/typescript-estree": "6.20.0", - "@typescript-eslint/visitor-keys": "6.20.0", + "@typescript-eslint/scope-manager": "7.2.0", + "@typescript-eslint/types": "7.2.0", + "@typescript-eslint/typescript-estree": "7.2.0", + "@typescript-eslint/visitor-keys": "7.2.0", "debug": "^4.3.4" }, "engines": { @@ -319,7 +336,7 @@ "url": "https://opencollective.com/typescript-eslint" }, "peerDependencies": { - "eslint": "^7.0.0 || ^8.0.0" + "eslint": "^8.56.0" }, "peerDependenciesMeta": { "typescript": { @@ -328,13 +345,13 @@ } }, "node_modules/@typescript-eslint/scope-manager": { - "version": "6.20.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-6.20.0.tgz", - "integrity": "sha512-p4rvHQRDTI1tGGMDFQm+GtxP1ZHyAh64WANVoyEcNMpaTFn3ox/3CcgtIlELnRfKzSs/DwYlDccJEtr3O6qBvA==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-7.2.0.tgz", + "integrity": "sha512-Qh976RbQM/fYtjx9hs4XkayYujB/aPwglw2choHmf3zBjB4qOywWSdt9+KLRdHubGcoSwBnXUH2sR3hkyaERRg==", "dev": true, "dependencies": { - "@typescript-eslint/types": "6.20.0", - "@typescript-eslint/visitor-keys": "6.20.0" + "@typescript-eslint/types": "7.2.0", + "@typescript-eslint/visitor-keys": "7.2.0" }, "engines": { "node": "^16.0.0 || >=18.0.0" @@ -345,13 +362,13 @@ } }, "node_modules/@typescript-eslint/type-utils": { - "version": "6.20.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-6.20.0.tgz", - "integrity": "sha512-qnSobiJQb1F5JjN0YDRPHruQTrX7ICsmltXhkV536mp4idGAYrIyr47zF/JmkJtEcAVnIz4gUYJ7gOZa6SmN4g==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-7.2.0.tgz", + "integrity": "sha512-xHi51adBHo9O9330J8GQYQwrKBqbIPJGZZVQTHHmy200hvkLZFWJIFtAG/7IYTWUyun6DE6w5InDReePJYJlJA==", "dev": true, "dependencies": { - "@typescript-eslint/typescript-estree": "6.20.0", - "@typescript-eslint/utils": "6.20.0", + "@typescript-eslint/typescript-estree": "7.2.0", + "@typescript-eslint/utils": "7.2.0", "debug": "^4.3.4", "ts-api-utils": "^1.0.1" }, @@ -363,7 +380,7 @@ "url": "https://opencollective.com/typescript-eslint" }, "peerDependencies": { - "eslint": "^7.0.0 || ^8.0.0" + "eslint": "^8.56.0" }, "peerDependenciesMeta": { "typescript": { @@ -372,9 +389,9 @@ } }, "node_modules/@typescript-eslint/types": { - "version": "6.20.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-6.20.0.tgz", - "integrity": "sha512-MM9mfZMAhiN4cOEcUOEx+0HmuaW3WBfukBZPCfwSqFnQy0grXYtngKCqpQN339X3RrwtzspWJrpbrupKYUSBXQ==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-7.2.0.tgz", + "integrity": "sha512-XFtUHPI/abFhm4cbCDc5Ykc8npOKBSJePY3a3s+lwumt7XWJuzP5cZcfZ610MIPHjQjNsOLlYK8ASPaNG8UiyA==", "dev": true, "engines": { "node": "^16.0.0 || >=18.0.0" @@ -385,13 +402,13 @@ } }, "node_modules/@typescript-eslint/typescript-estree": { - "version": "6.20.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-6.20.0.tgz", - "integrity": "sha512-RnRya9q5m6YYSpBN7IzKu9FmLcYtErkDkc8/dKv81I9QiLLtVBHrjz+Ev/crAqgMNW2FCsoZF4g2QUylMnJz+g==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-7.2.0.tgz", + "integrity": "sha512-cyxS5WQQCoBwSakpMrvMXuMDEbhOo9bNHHrNcEWis6XHx6KF518tkF1wBvKIn/tpq5ZpUYK7Bdklu8qY0MsFIA==", "dev": true, "dependencies": { - "@typescript-eslint/types": "6.20.0", - "@typescript-eslint/visitor-keys": "6.20.0", + "@typescript-eslint/types": "7.2.0", + "@typescript-eslint/visitor-keys": "7.2.0", "debug": "^4.3.4", "globby": "^11.1.0", "is-glob": "^4.0.3", @@ -413,17 +430,17 @@ } }, "node_modules/@typescript-eslint/utils": { - "version": "6.20.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-6.20.0.tgz", - "integrity": "sha512-/EKuw+kRu2vAqCoDwDCBtDRU6CTKbUmwwI7SH7AashZ+W+7o8eiyy6V2cdOqN49KsTcASWsC5QeghYuRDTyOOg==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-7.2.0.tgz", + "integrity": "sha512-YfHpnMAGb1Eekpm3XRK8hcMwGLGsnT6L+7b2XyRv6ouDuJU1tZir1GS2i0+VXRatMwSI1/UfcyPe53ADkU+IuA==", "dev": true, "dependencies": { "@eslint-community/eslint-utils": "^4.4.0", "@types/json-schema": "^7.0.12", "@types/semver": "^7.5.0", - "@typescript-eslint/scope-manager": "6.20.0", - "@typescript-eslint/types": "6.20.0", - "@typescript-eslint/typescript-estree": "6.20.0", + "@typescript-eslint/scope-manager": "7.2.0", + "@typescript-eslint/types": "7.2.0", + "@typescript-eslint/typescript-estree": "7.2.0", "semver": "^7.5.4" }, "engines": { @@ -434,16 +451,16 @@ "url": "https://opencollective.com/typescript-eslint" }, "peerDependencies": { - "eslint": "^7.0.0 || ^8.0.0" + "eslint": "^8.56.0" } }, "node_modules/@typescript-eslint/visitor-keys": { - "version": "6.20.0", - "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-6.20.0.tgz", - "integrity": "sha512-E8Cp98kRe4gKHjJD4NExXKz/zOJ1A2hhZc+IMVD6i7w4yjIvh6VyuRI0gRtxAsXtoC35uGMaQ9rjI2zJaXDEAw==", + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-7.2.0.tgz", + "integrity": "sha512-c6EIQRHhcpl6+tO8EMR+kjkkV+ugUNXOmeASA1rlzkd8EPIriavpWoiEz1HR/VLhbVIdhqnV6E7JZm00cBDx2A==", "dev": true, "dependencies": { - "@typescript-eslint/types": "6.20.0", + "@typescript-eslint/types": "7.2.0", "eslint-visitor-keys": "^3.4.1" }, "engines": { @@ -837,16 +854,16 @@ } }, "node_modules/eslint": { - "version": "8.56.0", - "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.56.0.tgz", - "integrity": "sha512-Go19xM6T9puCOWntie1/P997aXxFsOi37JIHRWI514Hc6ZnaHGKY9xFhrU65RT6CcBEzZoGG1e6Nq+DT04ZtZQ==", + "version": "8.57.0", + "resolved": "https://registry.npmjs.org/eslint/-/eslint-8.57.0.tgz", + "integrity": "sha512-dZ6+mexnaTIbSBZWgou51U6OmzIhYM2VcNdtiTtI7qPNZm35Akpr0f6vtw3w1Kmn5PYo+tZVfh13WrhpS6oLqQ==", "dev": true, "dependencies": { "@eslint-community/eslint-utils": "^4.2.0", "@eslint-community/regexpp": "^4.6.1", "@eslint/eslintrc": "^2.1.4", - "@eslint/js": "8.56.0", - "@humanwhocodes/config-array": "^0.11.13", + "@eslint/js": "8.57.0", + "@humanwhocodes/config-array": "^0.11.14", "@humanwhocodes/module-importer": "^1.0.1", "@nodelib/fs.walk": "^1.2.8", "@ungap/structured-clone": "^1.2.0", @@ -1631,6 +1648,15 @@ "resolved": "https://registry.npmjs.org/jsbn/-/jsbn-0.1.1.tgz", "integrity": "sha512-UVU9dibq2JcFWxQPA6KCqj5O42VOmAY3zQUfEKxU0KpTGXwNoCjkX1e13eHNvw/xPynt6pU0rZ1htjWTNTSXsg==" }, + "node_modules/jschardet": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/jschardet/-/jschardet-3.1.2.tgz", + "integrity": "sha512-mw3CBZGzW8nUBPYhFU2ztZ/kJ6NClQUQVpyzvFMfznZsoC///ZQ30J2RCUanNsr5yF22LqhgYr/lj807/ZleWA==", + "dev": true, + "engines": { + "node": ">=0.1.90" + } + }, "node_modules/json-buffer": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/json-buffer/-/json-buffer-3.0.1.tgz", @@ -2515,6 +2541,31 @@ "node": ">=14.17" } }, + "node_modules/typescript-eslint": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/typescript-eslint/-/typescript-eslint-7.2.0.tgz", + "integrity": "sha512-VqXEBqzPxJlR8Lfg2Dywe4XpIk637kwp2sfMQ+vudNHo48TUvnlHzAyFMQknv0AdhvZFXQN0a0t9SPI3rsAYew==", + "dev": true, + "dependencies": { + "@typescript-eslint/eslint-plugin": "7.2.0", + "@typescript-eslint/parser": "7.2.0" + }, + "engines": { + "node": "^16.0.0 || >=18.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/typescript-eslint" + }, + "peerDependencies": { + "eslint": "^8.56.0" + }, + "peerDependenciesMeta": { + "typescript": { + "optional": true + } + } + }, "node_modules/undici-types": { "version": "5.26.5", "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", @@ -2529,6 +2580,12 @@ "punycode": "^2.1.0" } }, + "node_modules/utf8": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/utf8/-/utf8-3.0.0.tgz", + "integrity": "sha512-E8VjFIQ/TyQgp+TZfS6l8yp/xWppSAHzidGiRrqe4bK4XP9pTRyKFgGJpO3SN7zdX4DeomTrwaseCHovfpFcqQ==", + "dev": true + }, "node_modules/uuid": { "version": "3.4.0", "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz", diff --git a/package.json b/package.json index 3ca2780..eaf2f1f 100644 --- a/package.json +++ b/package.json @@ -170,16 +170,19 @@ "scripts": { "vscode:prepublish": "npm run compile", "compile": "tsc -p ./", - "watch": "tsc -watch -p ./" + "watch": "tsc -watch -p ./", + "lint": "eslint ." }, "devDependencies": { + "@microsoft/eslint-formatter-sarif": "^3.0.0", "@types/mocha": "10.0.6", "@types/node": "20.11.13", "@types/vscode": "1.75.0", - "@typescript-eslint/eslint-plugin": "^6.14.0", - "@typescript-eslint/parser": "^6.14.0", - "eslint": "^8.56.0", - "typescript": "5.3.3" + "@typescript-eslint/eslint-plugin": "^7.2.0", + "@typescript-eslint/parser": "^7.2.0", + "eslint": "^8.57.0", + "typescript": "^5.3.3", + "typescript-eslint": "^7.2.0" }, "dependencies": { "@fohlen/github-release-downloader": "^1.0.0", diff --git a/src/extension.ts b/src/extension.ts index 04fd88b..4de45c9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -10,7 +10,7 @@ import { configureLanguageServers } from './ls/configure'; import { activateLanguageServers } from './ls/activate'; import { advertiseLanguageServers } from './ls/advertise'; -export let opaOutputChannel = vscode.window.createOutputChannel('OPA'); +export const opaOutputChannel = vscode.window.createOutputChannel('OPA'); export class JSONProvider implements vscode.TextDocumentContentProvider { @@ -65,23 +65,23 @@ export function activate(context: vscode.ExtensionContext) { context.subscriptions.push(vscode.commands.registerCommand('opa.show.commands', () => { const extension = vscode.extensions.getExtension("tsandall.opa"); if (extension !== undefined) { - let commands = extension.packageJSON.contributes.commands; + const commands = extension.packageJSON.contributes.commands; commands.push({ command: 'editor.action.goToDeclaration', title: 'Go to Definition' }); } })); vscode.languages.registerDocumentFormattingEditProvider({ scheme: 'file', language: 'rego' }, { provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] | Thenable { - let editor = vscode.window.activeTextEditor; + const editor = vscode.window.activeTextEditor; if (!editor) { return []; } // opa fmt doesn't support block formatting // so we must always select the entire document - let selectionRange = getFullDocumentSelection(editor); + const selectionRange = getFullDocumentSelection(editor); - let content = editor.document.getText(selectionRange); + const content = editor.document.getText(selectionRange); return new Promise((resolve, reject) => { runOPAFormatter(context, content, editor, reject, resolve, selectionRange); @@ -90,11 +90,11 @@ export function activate(context: vscode.ExtensionContext) { }); vscode.workspace.onDidSaveTextDocument((document: vscode.TextDocument) => { - let onFormat: boolean = vscode.workspace.getConfiguration('formatOnSave')['on']; + const onFormat: boolean = vscode.workspace.getConfiguration('formatOnSave')['on']; if (onFormat !== true) { return; } - let editor = vscode.window.activeTextEditor; + const editor = vscode.window.activeTextEditor; if (!editor) { return; } @@ -114,12 +114,12 @@ export function activate(context: vscode.ExtensionContext) { const outputUri = vscode.Uri.parse(`json:output.jsonc`); -let coveredHighlight = vscode.window.createTextEditorDecorationType({ +const coveredHighlight = vscode.window.createTextEditorDecorationType({ backgroundColor: 'rgba(64,128,64,0.5)', isWholeLine: true }); -let notCoveredHighlight = vscode.window.createTextEditorDecorationType({ +const notCoveredHighlight = vscode.window.createTextEditorDecorationType({ backgroundColor: 'rgba(128,64,64,0.5)', isWholeLine: true }); @@ -141,7 +141,7 @@ function runOPAFormatter(context: vscode.ExtensionContext, content: string, edit } if (code !== 0) { - let err = new Error("error running opa fmt :: " + stderr); + const err = new Error("error running opa fmt :: " + stderr); opaOutputShowError(err.message); reject(err); } else { @@ -153,8 +153,8 @@ function runOPAFormatter(context: vscode.ExtensionContext, content: string, edit } function getFullDocumentSelection(editor: vscode.TextEditor) { - let firstLine = editor.document.lineAt(0); - let lastLine = editor.document.lineAt(editor.document.lineCount - 1); + const firstLine = editor.document.lineAt(0); + const lastLine = editor.document.lineAt(editor.document.lineCount - 1); return new vscode.Selection(firstLine.range.start, lastLine.range.end); } @@ -203,7 +203,7 @@ function removeCoverage() { function setFileCoverage(result: any) { Object.keys(result.files).forEach(fileName => { - let report = result.files[fileName]; + const report = result.files[fileName]; if (!report) { return; } @@ -256,7 +256,7 @@ function setEvalOutput(provider: JSONProvider, uri: vscode.Uri, stderr: string, function activateCheckFile(context: vscode.ExtensionContext) { const checkRegoFile = () => { - let editor = vscode.window.activeTextEditor; + const editor = vscode.window.activeTextEditor; if (!editor) { return; } @@ -264,7 +264,7 @@ function activateCheckFile(context: vscode.ExtensionContext) { // Only check rego files if (doc.languageId === 'rego') { - let args: string[] = ['check']; + const args: string[] = ['check']; ifInWorkspace(() => { if (opa.canUseBundleFlags()) { @@ -281,7 +281,7 @@ function activateCheckFile(context: vscode.ExtensionContext) { } opa.runWithStatus(context, 'opa', args, '', (code: number, stderr: string, stdout: string) => { - let output = stderr; + const output = stderr; if (output.trim() !== '') { opaOutputShowError(output); } else { @@ -314,7 +314,7 @@ function activateCoverWorkspace(context: vscode.ExtensionContext) { return; } - for (let fileName in fileCoverage) { + for (const fileName in fileCoverage) { if (editor.document.fileName.endsWith(fileName)) { removeCoverage(); return; @@ -323,7 +323,7 @@ function activateCoverWorkspace(context: vscode.ExtensionContext) { fileCoverage = {}; - let args: string[] = ['test', '--coverage', '--format', 'json']; + const args: string[] = ['test', '--coverage', '--format', 'json']; ifInWorkspace(() => { if (opa.canUseBundleFlags()) { @@ -355,7 +355,7 @@ function activateEvalPackage(context: vscode.ExtensionContext) { opa.parse(context, 'opa', opa.getDataDir(editor.document.uri), (pkg: string, _: string[]) => { - let { inputPath, args } = createOpaEvalArgs(editor, pkg); + const { inputPath, args } = createOpaEvalArgs(editor, pkg); args.push('--metrics'); opa.run(context, 'opa', args, 'data.' + pkg, (stderr, result) => { @@ -378,10 +378,10 @@ function activateEvalSelection(context: vscode.ExtensionContext) { const evalSelectionCommand = vscode.commands.registerCommand('opa.eval.selection', onActiveWorkspaceEditor(outputUri, (editor: vscode.TextEditor) => { opa.parse(context, 'opa', opa.getDataDir(editor.document.uri), (pkg: string, imports: string[]) => { - let { inputPath, args } = createOpaEvalArgs(editor, pkg, imports); + const { inputPath, args } = createOpaEvalArgs(editor, pkg, imports); args.push('--metrics'); - let text = editor.document.getText(editor.selection); + const text = editor.document.getText(editor.selection); opa.run(context, 'opa', args, text, (stderr, result) => { setEvalOutput(provider, outputUri, stderr, result, inputPath); @@ -401,7 +401,7 @@ function activateEvalCoverage(context: vscode.ExtensionContext) { const evalCoverageCommand = vscode.commands.registerCommand('opa.eval.coverage', onActiveWorkspaceEditor(outputUri, (editor: vscode.TextEditor) => { - for (let fileName in fileCoverage) { + for (const fileName in fileCoverage) { if (editor.document.fileName.endsWith(fileName)) { removeCoverage(); return; @@ -412,11 +412,11 @@ function activateEvalCoverage(context: vscode.ExtensionContext) { opa.parse(context, 'opa', opa.getDataDir(editor.document.uri), (pkg: string, imports: string[]) => { - let { inputPath, args } = createOpaEvalArgs(editor, pkg, imports); + const { inputPath, args } = createOpaEvalArgs(editor, pkg, imports); args.push('--metrics'); args.push('--coverage'); - let text = editor.document.getText(editor.selection); + const text = editor.document.getText(editor.selection); opa.run(context, 'opa', args, text, (stderr, result) => { setEvalOutput(provider, outputUri, stderr, result, inputPath); @@ -441,7 +441,7 @@ function activateTestWorkspace(context: vscode.ExtensionContext) { opaOutputChannel.show(true); opaOutputChannel.clear(); - let args: string[] = ['test']; + const args: string[] = ['test']; args.push('--verbose'); @@ -473,11 +473,11 @@ function activateTraceSelection(context: vscode.ExtensionContext) { if (!editor) { return; } - let text = editor.document.getText(editor.selection); + const text = editor.document.getText(editor.selection); opa.parse(context, 'opa', opa.getDataDir(editor.document.uri), (pkg: string, imports: string[]) => { - let { args } = createOpaEvalArgs(editor, pkg, imports); + const { args } = createOpaEvalArgs(editor, pkg, imports); args.push('--format', 'pretty'); args.push('--explain', 'full'); @@ -506,13 +506,13 @@ function activateProfileSelection(context: vscode.ExtensionContext) { if (!editor) { return; } - let text = editor.document.getText(editor.selection); + const text = editor.document.getText(editor.selection); opa.parse(context, 'opa', opa.getDataDir(editor.document.uri), (pkg: string, imports: string[]) => { opaOutputChannel.show(true); opaOutputChannel.clear(); - let { args } = createOpaEvalArgs(editor, pkg, imports); + const { args } = createOpaEvalArgs(editor, pkg, imports); args.push('--profile'); args.push('--format', 'pretty'); @@ -538,11 +538,11 @@ function activatePartialSelection(context: vscode.ExtensionContext) { if (!editor) { return; } - let text = editor.document.getText(editor.selection); + const text = editor.document.getText(editor.selection); opa.parse(context, 'opa', opa.getDataDir(editor.document.uri), (pkg: string, imports: string[]) => { - let depsArgs = ['deps', '--format', 'json',]; + const depsArgs = ['deps', '--format', 'json',]; ifInWorkspace(() => { depsArgs.push(...opa.getRootParams()); @@ -553,14 +553,14 @@ function activatePartialSelection(context: vscode.ExtensionContext) { depsArgs.push('data.' + pkg); opa.run(context, 'opa', depsArgs, '', (_, result: any) => { - let refs = result.base.map((ref: any) => opa.refToString(ref)); + const refs = result.base.map((ref: any) => opa.refToString(ref)); refs.push('input'); vscode.window.showQuickPick(refs).then((selection: string | undefined) => { if (selection !== undefined) { opaOutputChannel.show(true); opaOutputChannel.clear(); - let { args } = createOpaEvalArgs(editor, pkg, imports); + const { args } = createOpaEvalArgs(editor, pkg, imports); args.push('--partial'); args.push('--format', 'pretty'); args.push('--unknowns', selection); @@ -617,7 +617,7 @@ function onActiveWorkspaceEditor(forURI: vscode.Uri, cb: (editor: vscode.TextEdi vscode.workspace.openTextDocument(forURI) .then(function (doc: any) { - let found = vscode.window.visibleTextEditors.find((ed: vscode.TextEditor) => { + const found = vscode.window.visibleTextEditors.find((ed: vscode.TextEditor) => { return ed.document.uri === doc.uri; }); @@ -631,13 +631,13 @@ function onActiveWorkspaceEditor(forURI: vscode.Uri, cb: (editor: vscode.TextEdi // TODO(tsandall): test non-workspace mode. I don't know if this plugin // will work if a single file is loaded. Certain features may not work // but many can. - let editor = vscode.window.activeTextEditor; + const editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showErrorMessage('No active editor'); return; } - let inWorkspace = !!vscode.workspace.workspaceFolders; + const inWorkspace = !!vscode.workspace.workspaceFolders; cb(editor, !!inWorkspace); }; @@ -647,7 +647,7 @@ let informAboutWorkspace = true; const informAboutWorkspaceOption = "Don't show this tip again"; function ifInWorkspace(yes: () => void, no: () => void = () => { }) { - if (!!vscode.workspace.workspaceFolders) { + if (vscode.workspace.workspaceFolders) { yes(); } else { if (informAboutWorkspace) { @@ -696,7 +696,7 @@ function formatErrors(error: string): string { } else if (output.errors !== undefined) { errors = output.errors; } - let msg = []; + const msg = []; for (let i = 0; i < errors.length; i++) { let location_prefix; if (errors[i].location.file !== '') { @@ -741,7 +741,7 @@ function getInputPath(): string { // If the rootDir is a file:// URL then just append /input.json onto the // end. Otherwise use the path.join function to get a platform-specific file // path returned. - let rootDir = opa.getDataDir(parsed); + const rootDir = opa.getDataDir(parsed); if (parsed.scheme === 'file') { return parsed.toString() + '/input.json'; @@ -756,7 +756,7 @@ class RegoDefinitionProvider implements vscode.DefinitionProvider { position: vscode.Position, token: vscode.CancellationToken): Thenable { - let args: string[] = ['oracle', 'find-definition', '--stdin-buffer']; + const args: string[] = ['oracle', 'find-definition', '--stdin-buffer']; ifInWorkspace(() => { args.push(...opa.getRootParams()); }); @@ -784,7 +784,7 @@ class RegoDefinitionProvider implements vscode.DefinitionProvider { } function createOpaEvalArgs(editor: vscode.TextEditor, pkg: string, imports: string[] = []): { inputPath: string, args: string[] } { - let args: string[] = ['eval']; + const args: string[] = ['eval']; args.push('--stdin'); args.push('--package', pkg); diff --git a/src/github-installer.ts b/src/github-installer.ts index c26d0ed..6c33e3b 100644 --- a/src/github-installer.ts +++ b/src/github-installer.ts @@ -10,7 +10,7 @@ import * as vscode from 'vscode'; import { opaOutputChannel } from './extension'; const releaseDownloader = require('@fohlen/github-release-downloader'); -let installDeclined: { [repo: string]: boolean } = {}; +const installDeclined: { [repo: string]: boolean } = {}; export function promptForInstall( binaryConfigKey: string, diff --git a/src/opa.ts b/src/opa.ts index 170b07d..5c9ac35 100644 --- a/src/opa.ts +++ b/src/opa.ts @@ -10,7 +10,7 @@ import { existsSync } from 'fs'; import { dirname } from 'path'; import { advertiseLanguageServers } from './ls/advertise'; -var regoVarPattern = new RegExp('^[a-zA-Z_][a-zA-Z0-9_]*$'); +const regoVarPattern = new RegExp('^[a-zA-Z_][a-zA-Z0-9_]*$'); export function getDataDir(uri: vscode.Uri): string { // NOTE(tsandall): we don't have a precise version for 3be55ed6 so @@ -25,12 +25,12 @@ export function getDataDir(uri: vscode.Uri): string { } export function canUseBundleFlags(): boolean { - let bundleMode = vscode.workspace.getConfiguration('opa').get('bundleMode', true); + const bundleMode = vscode.workspace.getConfiguration('opa').get('bundleMode', true); return installedOPASameOrNewerThan("0.14.0-dev") && bundleMode; } export function canUseStrictFlag(): boolean { - let strictMode = vscode.workspace.getConfiguration('opa').get('strictMode', true); + const strictMode = vscode.workspace.getConfiguration('opa').get('strictMode', true); return strictMode && installedOPASameOrNewerThan("0.37.0"); } @@ -61,13 +61,8 @@ function replacePathVariables(path: string): string { // Returns a list of root data path URIs based on the plugin configuration. export function getRoots(): string[] { const roots = vscode.workspace.getConfiguration('opa').get('roots', []); - let formattedRoots = new Array(); - roots.forEach((root: string) => { - root = replacePathVariables(root); - formattedRoots.push(getDataDir(vscode.Uri.parse(root))); - }); - return formattedRoots; + return roots.map((root: string) => getDataDir(vscode.Uri.parse(replacePathVariables(root)))); } // Returns a list of root data parameters in an array @@ -76,11 +71,8 @@ export function getRoots(): string[] { export function getRootParams(): string[] { const flag = dataFlag(); const roots = getRoots(); - let params = new Array(); - roots.forEach(root => { - params.push(`${flag}=${root}`); - }); - return params; + + return roots.map((root) => `${flag}=${root}`) } // Returns a list of schema parameters in an array. @@ -200,8 +192,8 @@ export function refToString(ref: any[]): string { export function parse(context: vscode.ExtensionContext, opaPath: string, path: string, cb: (pkg: string, imports: string[]) => void, onerror: (output: string) => void) { run(context, opaPath, ['parse', path, '--format', 'json'], '', (_, result) => { - let pkg = getPackage(result); - let imports = getImports(result); + const pkg = getPackage(result); + const imports = getImports(result); cb(pkg, imports); }, onerror); } @@ -301,7 +293,7 @@ function getOpaPath(context: vscode.ExtensionContext | undefined, path: string, } function getOpaEnv(): NodeJS.ProcessEnv { - let env = vscode.workspace.getConfiguration('opa').get('env', {}); + const env = vscode.workspace.getConfiguration('opa').get('env', {}); return Object.fromEntries(Object.entries(env).map(([k, v]) => [k, replacePathVariables(v as string)])); } @@ -309,14 +301,14 @@ function getOpaEnv(): NodeJS.ProcessEnv { // runWithStatus executes the OPA binary at path with args and stdin. The // callback is invoked with the exit status, stderr, and stdout buffers. export function runWithStatus(context: vscode.ExtensionContext | undefined, path: string, args: string[], stdin: string, cb: (code: number, stderr: string, stdout: string) => void) { - let opaPath = getOpaPath(context, path, true); + const opaPath = getOpaPath(context, path, true); if (opaPath === undefined) { return; } console.log("spawn:", opaPath, "args:", args.toString()); - let proc = cp.spawn(opaPath, args, {env: {...process.env, ...getOpaEnv()}}); + const proc = cp.spawn(opaPath, args, {env: {...process.env, ...getOpaEnv()}}); proc.stdin.write(stdin); proc.stdin.end(); diff --git a/src/util.ts b/src/util.ts index ecba62b..9893854 100644 --- a/src/util.ts +++ b/src/util.ts @@ -12,7 +12,7 @@ export function getPackage(parsed: any): string { export function getImports(parsed: any): string[] { if (parsed.imports !== undefined) { return parsed.imports.map((x: any) => { - let str = getPathString(x.path.value); + const str = getPathString(x.path.value); if (!x.alias) { return str; } @@ -38,11 +38,11 @@ export function getPathString(path: any): string { } export function getPrettyTime(ns: number): string { - let seconds = ns / 1e9; + const seconds = ns / 1e9; if (seconds >= 1) { return seconds.toString() + 's'; } - let milliseconds = ns / 1e6; + const milliseconds = ns / 1e6; if (milliseconds >= 1) { return milliseconds.toString() + 'ms'; }