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

refactor: Break out code into testable modules #265

Merged
merged 13 commits into from Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions .eslintrc.js
Expand Up @@ -3,18 +3,33 @@
/** @type {import('eslint').Linter.Config} */
const config = {
extends: ['stylelint', 'plugin:node/recommended', 'prettier'],
parser: '@babel/eslint-parser',
parserOptions: {
ecmaVersion: 2020,
sourceType: 'script',
requireConfigFile: false,
babelOptions: {
presets: [['@babel/preset-env', { targets: { node: '4' } }]],
},
},
env: {
node: true,
es2020: true,
},
rules: {
strict: ['error', 'safe'],
'node/no-missing-require': ['error', { allowModules: ['vscode'] }],
'require-jsdoc': 'error',
'no-warning-comments': ['warn', { terms: ['todo'], location: 'start' }],
},
overrides: [
{
files: ['**/__tests__/**/*'],
rules: {
'node/no-unpublished-require': 'off',
},
},
],
};

module.exports = config;
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/REPORT_A_BUG.md
Expand Up @@ -36,7 +36,7 @@ e.g. "Yes"

e.g. `0.84.0`

> Which version of stylelint are you using?
> Which version of Stylelint are you using?

e.g. `13.2.0`

Expand All @@ -52,4 +52,4 @@ e.g. "No warnings to be flagged."

e.g. "The following warnings were flagged:"

<!-- If the bug can be reproduced using the stylelint CLI, e.g. `npx stylelint "src/**/*.css"`, please create the issue in the stylelint issue tracker (https://github.com/stylelint/stylelint/issues/new?template=REPORT_A_BUG.md) instead. -->
<!-- If the bug can be reproduced using the Stylelint CLI, e.g. `npx stylelint "src/**/*.css"`, please create the issue in the Stylelint issue tracker (https://github.com/stylelint/stylelint/issues/new?template=REPORT_A_BUG.md) instead. -->
14 changes: 7 additions & 7 deletions .github/workflows/testing.yml
Expand Up @@ -61,16 +61,16 @@ jobs:
- name: Download VS Code
run: npm run download-vscode

- name: Run lib tests
run: npm run test:lib -- --coverage

- name: Run unit tests
run: npm run test:unit -- --coverage

- name: Run VS Code tests (Linux)
- name: Run integration tests
run: npm run test:integration -- --coverage

- name: Run end-to-end tests (Linux)
if: runner.os == 'Linux'
run: xvfb-run -a npm run test:workspace -- --silent
run: xvfb-run -a npm run test:e2e -- --silent

- name: Run VS Code tests (non-Linux)
- name: Run end-to-end tests (non-Linux)
if: runner.os != 'Linux'
run: npm run test:workspace -- --silent
run: npm run test:e2e -- --silent
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -85,6 +85,7 @@ eggs
parts
lib64
.sass-cache
*.tsbuildinfo

# -------------------------------------------------
# Your own project's ignores
Expand Down
8 changes: 7 additions & 1 deletion .vscode/launch.json
Expand Up @@ -5,8 +5,14 @@
"name": "Launch Extension",
"type": "extensionHost",
"request": "launch",
"preLaunchTask": "Bundle Extension (Watch)",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "${workspaceRoot}/fixture.css"],
"args": [
"--extensionDevelopmentPath=${workspaceRoot}",
"${workspaceRoot}/fixture.css",
"--trace-warnings"
],
"env": { "NODE_ENV": "development" },
"stopOnEntry": false
},
{
Expand Down
38 changes: 38 additions & 0 deletions .vscode/tasks.json
@@ -0,0 +1,38 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Bundle Extension (Watch)",
"type": "shell",
"isBackground": true,
"command": "npm run bundle-watch",
"problemMatcher": [
{
"source": "esbuild",
"applyTo": "closedDocuments",
"severity": "error",
"fileLocation": "relative",
"pattern": [
{
"regexp": "> (.*?):([0-9]+):([0-9]+): (warning|error): (.+)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
],
"background": {
"activeOnStart": true,
"beginsPattern": {
"regexp": "\\[watch\\] build started"
},
"endsPattern": {
"regexp": "\\[watch\\] build finished"
}
}
}
]
}
]
}
13 changes: 13 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Breaking Changes

- Dropped support for Stylelint 13 and prior; only Stylelint 14 is supported now. See the [migration guide](README.md#%EF%B8%8F-stylelint-13x-and-prior-is-no-longer-supported) for more details.
- Removed bundled copy of Stylelint; local or global installation is now required. See the [migration guide](README.md#%EF%B8%8F-stylelint-is-no-longer-bundled) for more details.
- Validation and completion now only works for documents with language identifiers `css`, `less`, and `postcss` by default. See the [migration guide](README.md#%EF%B8%8F-only-css-and-postcss-are-validated-by-default) for more details.
- `stylelint.syntax` configuration option removed; use `stylelint.customSyntax` instead. See the [migration guide](README.md#%EF%B8%8F-only-css-and-postcss-are-validated-by-default) for more details.

### Changes

- Updated [vscode-languageserver](https://github.com/Microsoft/vscode-languageserver-node) to v7, conforming to LSP v3.16.0.

## [0.87.6](https://github.com/stylelint/vscode-stylelint/compare/v0.87.5...v0.87.6) (2021-10-04)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion __mocks__/os.js
@@ -1,6 +1,6 @@
'use strict';

const os = jest.createMockFromModule('os');
const os = jest.requireActual('os');

/**
* Mock platform.
Expand Down
51 changes: 45 additions & 6 deletions __mocks__/path.js
Expand Up @@ -4,35 +4,74 @@ const path = jest.requireActual('path');

/**
* Mock platform.
* @type {'posix' | 'win32'}
* @type {'posix' | 'win32' | undefined}
*/
let mockPlatform = 'posix';
let mockPlatform = undefined;

/**
* Sets the mock platform.
* @param {'posix' | 'win32'} platform
* Sets the mock platform. A value of `undefined` will use the actual platform.
* @param {'posix' | 'win32'} [platform]
* @returns {void}
*/
const __mockPlatform = (platform) => {
mockPlatform = platform;
};

/**
* @type {{
* default: Record<string | symbol, Function>,
* posix: Record<string | symbol, Function>,
* win32: Record<string | symbol, Function>,
* }}
*/
const mockedFns = {
default: {},
posix: {},
win32: {},
};

const pathProxy = new Proxy(path, {
get(_, name) {
if (name === '__mockPlatform') {
return __mockPlatform;
}

if (!mockPlatform) {
if (typeof path[name] === 'function') {
if (mockedFns.default[name]) {
return mockedFns.default[name];
}

mockedFns.default[name] = jest.fn(path[name]);

return mockedFns.default[name];
}

return path[name];
}

if (mockPlatform === 'win32') {
if (typeof path.win32[name] === 'function') {
return jest.fn(path.win32[name]);
if (mockedFns.win32[name]) {
return mockedFns.win32[name];
}

mockedFns.win32[name] = jest.fn(path.win32[name]);

return mockedFns.win32[name];
}

return path.win32[name];
}

if (typeof path.posix[name] === 'function') {
return jest.fn(path.posix[name]);
if (mockedFns.posix[name]) {
return mockedFns.posix[name];
}

mockedFns.posix[name] = jest.fn(path.posix[name]);

return mockedFns.posix[name];
}

return path.posix[name];
Expand Down
41 changes: 41 additions & 0 deletions __mocks__/vscode-languageserver/node.js
@@ -0,0 +1,41 @@
'use strict';

const nodeLSP = jest.createMockFromModule('vscode-languageserver/node');

/** @typedef {(globalModulesPath?: string, cwd?: string, trace?: TracerFn) => any} MockResolver */

/** @type {{[package: string]: MockResolver}} */
let mockedResolutions = {};

/**
* Mocks module resolution for a package.
* @param {string} packageName
* @param {MockResolver} resolver
*/
nodeLSP.Files.__mockResolution = (packageName, resolver) => {
mockedResolutions[packageName] = resolver;
};

/**
* Resets all mocked resolutions.
*/
nodeLSP.Files.__resetMockedResolutions = () => {
mockedResolutions = {};
};

/** @type {typeof import('vscode-languageserver/node').Files.resolve} */
const mockResolve = async (packageName, globalPath, cwd, trace) => {
if (mockedResolutions[packageName]) {
const resolved = mockedResolutions[packageName](globalPath, cwd, trace);

if (resolved) {
return resolved;
}
}

throw Error(`Failed to resolve module: ${packageName}`);
};

nodeLSP.Files.resolve = jest.fn(mockResolve);

module.exports = nodeLSP;
14 changes: 14 additions & 0 deletions babel.config.js
@@ -0,0 +1,14 @@
'use strict';

const { engines } = require('./package.json');

// Get only the major and minor version
// e.g. ">=14.16.0" => "14.16"
const nodeVersion = engines.node.match(/(\d+\.\d+)(?:\.\d+)?/)?.[1];

/** @type {babel.TransformOptions} */
const config = {
presets: [['@babel/preset-env', { targets: { node: nodeVersion } }]],
};

module.exports = config;
4 changes: 2 additions & 2 deletions jest.config.js
Expand Up @@ -3,9 +3,9 @@
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
projects: [
'<rootDir>/test/lib/jest.config.js',
'<rootDir>/test/unit/jest.config.js',
'<rootDir>/test/workspace/jest.config.js',
'<rootDir>/test/integration/jest.config.js',
'<rootDir>/test/e2e/jest.config.js',
],
};

Expand Down