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: use synckit to support Prettier 3 #199

Merged
merged 25 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ name: Test

on:
push:
branches: ['master']
branches: ["master"]
pull_request:
branches: ['master']
branches: ["master"]

jobs:
build:
strategy:
matrix:
# os: ['ubuntu-latest', 'macos-latest', 'windows-latest']
os: ['ubuntu-latest']
os: ["ubuntu-latest"]

runs-on: ${{ matrix.os }}

Expand All @@ -20,8 +20,8 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 16.x
cache: 'yarn'
cache: "yarn"
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn build && yarn build:worker
- run: xvfb-run -a yarn test
if: runner.os == 'Linux'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dist/
test/fixtures/temp
coverage/
.env
/*.vsix
16 changes: 0 additions & 16 deletions .gitlab-ci.yml

This file was deleted.

11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# v6.0.0 (Fri Dec 1 2023)

#### 🚀 Enhancement

- fix: use `worker_threads` via [`synckit`](https://github.com/un-ts/synckit) to support Prettier 3

#### ⚠️ Breaking Changes
- `prettier-eslint` is now required as a dev dependency
- Prettier v3.0 is now required
- ESLint >= v8.52.0 is now required

# v5.1.0 (Fri Mar 10 2023)

:tada: This release contains work from a new contributor! :tada:
Expand Down
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ A Visual Studio Code Extension to format JavaScript and TypeScript code using th

This extension requires the following NPM packages to be installed either locally or globally:

- `prettier@^2.5.1`
- `eslint@^8.7.0`
- `prettier@^3.1.0`
- `eslint@^8.52.0`
- `prettier-eslint@^16.1.2`
- `@typescript-eslint/parser@^5.0.1` and `typescript@^4.4.4` **(Only for TypeScript projects)**
- `vue-eslint-parser@^8.0.0` **(Only for Vue projects)**

Expand All @@ -41,24 +42,24 @@ This extension requires the following NPM packages to be installed either locall

### Install Dependencies

First we need to install `prettier` and `eslint` as dev dependencies in your project. Run one of the following commands based on your project requirements. The commands listed below use `yarn` but you can also use `npm`. Just replace `yarn add` with `npm i`
First we need to install `prettier`, `eslint`, and `prettier-eslint` as dev dependencies in your project. Run one of the following commands based on your project requirements. The commands listed below use `yarn` but you can also use `npm`. Just replace `yarn add` with `npm i`

#### Minimum Requirements

```bash
yarn add -D prettier@^2.5.1 eslint@^8.7.0
yarn add -D prettier@^3.1.0 eslint@^8.52.0 prettier-eslint@^16.1.2
```

#### TypeScript Projects

```bash
yarn add -D prettier@^2.5.1 eslint@^8.7.0 @typescript-eslint/parser@^5.0.1 typescript@^4.4.4
yarn add -D prettier@^3.1.0 eslint@^8.52.0 prettier-eslint@^16.1.2 @typescript-eslint/parser@^5.0.1 typescript@^4.4.4
```

#### Vue Projects

```bash
yarn add -D prettier@^2.5.1 eslint@^8.7.0 vue-eslint-parser@^8.0.0
yarn add -D prettier@^3.1.0 eslint@^8.52.0 prettier-eslint@^16.1.2 vue-eslint-parser@^8.0.0
```

### Project Settings
Expand Down Expand Up @@ -146,9 +147,10 @@ These projects are setup to work with the VS Code Prettier ESLint extension. Use

## Support for Earlier Versions

- Prettier 1.9 & ESLint 6.8 - Version 3.10 ([DOWNLOAD](https://marketplace.visualstudio.com/_apis/public/gallery/publishers/rvest/vsextensions/vs-code-prettier-eslint/3.1.0/vspackage) | [README](https://github.com/idahogurl/vs-code-prettier-eslint/tree/3.x))
- Prettier 2.0 & ESLint 7.0 - Version 4.10 ([DOWNLOAD](https://marketplace.visualstudio.com/_apis/public/gallery/publishers/rvest/vsextensions/vs-code-prettier-eslint/4.1.0/vspackage) |
- Prettier 1.9 & ESLint 6.8 - Version 3.10.0 ([DOWNLOAD](https://marketplace.visualstudio.com/_apis/public/gallery/publishers/rvest/vsextensions/vs-code-prettier-eslint/3.1.0/vspackage) | [README](https://github.com/idahogurl/vs-code-prettier-eslint/tree/3.x))
- Prettier 2.0 & ESLint 7.0 - Version 4.10.0 ([DOWNLOAD](https://marketplace.visualstudio.com/_apis/public/gallery/publishers/rvest/vsextensions/vs-code-prettier-eslint/4.1.0/vspackage) |
[README](https://github.com/idahogurl/vs-code-prettier-eslint/tree/4.x))
- Prettier 2.0 & ESLint 8.0 - Version 5.1.0 ([DOWNLOAD](https://marketplace.visualstudio.com/_apis/public/gallery/publishers/rvest/vsextensions/vs-code-prettier-eslint/5.1.0/vspackage) | [README](https://github.com/idahogurl/vs-code-prettier-eslint/blob/5.x/README.md))

## Contributing

Expand Down
26 changes: 17 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"type": "git",
"url": "https://github.com/idahogurl/vs-code-prettier-eslint"
},
"version": "5.1.1",
"version": "6.0.0",
"engines": {
"vscode": "^1.63.0",
"node": "^16.13.0"
Expand Down Expand Up @@ -48,10 +48,13 @@
"pretest": "yarn lint",
"test": "node ./test/runTest.js",
"test:update": "SNAPSHOT_UPDATE=true node ./test/runTest.js",
"dev": "NODE_ENV=dev node ./scripts/build.js",
"watch": "NODE_ENV=dev,watch node ./scripts/build.js",
"build": "NODE_ENV=prod node ./scripts/build.js",
"package": "yarn build && vsce package --yarn",
"dev:watch": "yarn dev --watch --sourcemap",
"build:dev": "yarn build:extension && yarn build:worker",
"build": "yarn build:extension --minify && yarn build:worker --minify",
idahogurl marked this conversation as resolved.
Show resolved Hide resolved
"build:extension": "esbuild ./src/extension.js --bundle --platform=node --target=node16 --external:vscode --external:../worker.mjs --outfile=./dist/extension.js",
"build:worker": "esbuild ./src/worker.mjs --bundle --platform=node --format=esm --target=node16 --outfile=./dist/worker.mjs",
"package:dev": "yarn build:dev && vsce package",
"package": "yarn build && vsce package",
"contributors:add": "all-contributors add",
"contributors:generate": "all-contributors generate"
},
Expand All @@ -66,23 +69,24 @@
"all-contributors-cli": "^6.24.0",
"auto": "^10.42.2",
"esbuild": "^0.16.14",
"esbuild-plugin-text-replace": "^1.2.0",
"eslint": "^8.31.0",
"eslint": "^8.52.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^27.2.0",
"jest": "^29.3.1",
"jest-environment-node": "^29.3.1",
"ovsx": "^0.7.1",
"prettier": "^2.8.1",
"prettier": "^3.1.0",
"prettier-eslint": "^16.1.2",
"ts-node": "^10.9.1",
"typescript": "^4.9.5",
"vsce": "^2.15.0"
},
"dependencies": {
"find-up": "^6.3.0",
"ignore": "^5.2.4",
"prettier-eslint": "^15.0.1"
"require-relative": "^0.8.7",
"synckit": "^0.8.6"
idahogurl marked this conversation as resolved.
Show resolved Hide resolved
idahogurl marked this conversation as resolved.
Show resolved Hide resolved
},
"contributes": {
"configuration": {
Expand All @@ -96,6 +100,10 @@
}
}
},
"vsce": {
"dependencies": false,
"yarn": true
},
"__metadata": {
"id": "d4b06bd6-36a0-469f-be55-c0a73413b688",
"publisherDisplayName": "Rebecca Vest",
Expand Down
File renamed without changes
File renamed without changes
38 changes: 0 additions & 38 deletions scripts/build.js

This file was deleted.

89 changes: 55 additions & 34 deletions src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,71 @@
import {
languages, window, TextEdit, Range, workspace,
} from 'vscode';
import * as path from 'path';
import { createSyncFn } from 'synckit';
import requireRelative from 'require-relative';

import format from './formatter';
import { isFilePathMatchedByEslintIgnore, isFilePathMatchedByPrettierIgnore } from './ignore';
import isFilePathMatchedByIgnore from './ignore';

const path = require('path');
const outputChannel = window.createOutputChannel('Prettier Eslint'); // create output channel for error logging
const formatText = createSyncFn(require.resolve('./worker.mjs'));

let outputChannel;
/**
* Resolves the path of a module relative to a file path.
* @param {string} filePath - The file path to resolve the module path relative to.
* @param {string} moduleName - The name of the module to resolve.
* @returns {string} - The resolved module path.
*/
function getModulePath(filePath, moduleName) {
try {
return requireRelative.resolve(moduleName, filePath);
} catch (error) {
return require.resolve(moduleName);
}
}

async function formatter(document) {
/**
* Formats the given document using 'prettier-eslint' package.
* If the document is ignored by either Prettier or ESLint,
* logs a message to the console and does nothing.
* @param {TextDocument} document - The document to format.
* @returns {TextEdit[]} - Formatted text edits.
*/
function formatter(document) {
const documentPath = path.dirname(document.fileName);
const workspaceDir = workspace?.workspaceFolders.find((w) => documentPath.startsWith(w.uri.path))
?.uri.path;

try {
if (
isFilePathMatchedByEslintIgnore(document.fileName, workspaceDir)
|| isFilePathMatchedByPrettierIgnore(document.fileName, workspaceDir)
// eslint-disable-next-line no-empty
) {
console.log('File ignored.');
outputChannel.appendLine('File ignored. Matches entry in .eslintignore or .prettierignore');
} else {
const startLine = document.lineAt(0);
const endLine = document.lineAt(document.lineCount - 1);
const range = new Range(startLine.range.start, endLine.range.end);
if (isFilePathMatchedByIgnore(document.fileName, workspaceDir, '.eslintignore')) {
outputChannel.appendLine(`File ${document.fileName} is ignored by ESLint.`);
return [];
}

const text = document.getText(range);
const extensionConfig = workspace?.getConfiguration('vs-code-prettier-eslint');
const formatted = await format({
text,
filePath: document.fileName,
extensionConfig,
});
return [TextEdit.replace(range, formatted)];
if (isFilePathMatchedByIgnore(document.fileName, workspaceDir, '.prettierignore')) {
outputChannel.appendLine(`File ${document.fileName} is ignored by Prettier.`);
return [];
}

const startLine = document.lineAt(0);
const endLine = document.lineAt(document.lineCount - 1);
const range = new Range(startLine.range.start, endLine.range.end);
const text = document.getText(range);
const prettierLast = workspace.getConfiguration('vs-code-prettier-eslint').get('prettierLast');

const formatted = formatText({
text,
prettierEslintPath: getModulePath(document.fileName, 'prettier-eslint'),
filePath: document.fileName,
extensionConfig: { prettierLast },
});

return [TextEdit.replace(range, formatted)];
} catch (err) {
outputChannel.appendLine(`Error: ${err.message} \n${err.stack}`);
}
}

const formattingProvider = {
provideDocumentRangeFormattingEdits(document) {
return formatter(document);
},
};

const supportedLanguages = [
'css',
'graphql',
Expand All @@ -71,8 +91,9 @@ const supportedLanguages = [
];

supportedLanguages.forEach((language) => {
languages.registerDocumentRangeFormattingEditProvider(language, formattingProvider);
languages.registerDocumentRangeFormattingEditProvider(language, {
provideDocumentRangeFormattingEdits(document) {
return formatter(document);
},
});
});

// Create output channel for error logging
outputChannel = window.createOutputChannel('Prettier Eslint');
9 changes: 0 additions & 9 deletions src/formatter.js

This file was deleted.

34 changes: 9 additions & 25 deletions src/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,18 @@ function getIsIgnored(filename) {
return instance.ignores.bind(instance);
}

export function isFilePathMatchedByEslintIgnore(filePath, workspaceDir) {
export default function isFilePathMatchedByIgnore(filePath, workspaceDirectory, ignoreFileName) {
const options = { cwd: path.dirname(filePath) };
if (workspaceDir) {
options.stopAt = workspaceDir;
if (workspaceDirectory) {
options.stopAt = workspaceDirectory;
}
const eslintIgnorePath = findUpSync('.eslintignore', options);
if (!eslintIgnorePath) {
const ignorePath = findUpSync(ignoreFileName, options);
if (!ignorePath) {
return false;
}

const eslintIgnoreDir = path.dirname(eslintIgnorePath);
const filePathRelativeToEslintIgnoreDir = path.relative(eslintIgnoreDir, filePath);
const isIgnored = getIsIgnored(eslintIgnorePath);
return isIgnored(filePathRelativeToEslintIgnoreDir);
}

export function isFilePathMatchedByPrettierIgnore(filePath, workspaceDir) {
const options = { cwd: path.dirname(filePath) };
if (workspaceDir) {
options.stopAt = workspaceDir;
}
const prettierIgnorePath = findUpSync('.prettierignore', options);
if (!prettierIgnorePath) {
return false;
}

const prettierIgnoreDir = path.dirname(prettierIgnorePath);
const filePathRelativeToPrettierIgnoreDir = path.relative(prettierIgnoreDir, filePath);
const isIgnored = getIsIgnored(prettierIgnorePath);
return isIgnored(filePathRelativeToPrettierIgnoreDir);
const ignoreDir = path.dirname(ignorePath);
const filePathRelativeToIgnoreDir = path.relative(ignoreDir, filePath);
const isIgnored = getIsIgnored(ignorePath);
return isIgnored(filePathRelativeToIgnoreDir);
}
Loading
Loading