diff --git a/packages/eslint-plugin-rxjs/src/index.ts b/packages/eslint-plugin-rxjs/src/index.ts index d39b905..da5f5fa 100644 --- a/packages/eslint-plugin-rxjs/src/index.ts +++ b/packages/eslint-plugin-rxjs/src/index.ts @@ -101,25 +101,27 @@ type ESLintPlugin = { configs: Record; }; -const plugin = { +const basePlugin = { meta, rules, - configs: {}, -} as ESLintPlugin; +}; + +const recommendedConfig = { + name: '@smarttools/rxjs/recommended', + plugins: { + '@smarttools/rxjs': basePlugin, + }, + rules: { + ...recommended.rules, + }, +}; -if (plugin.configs) { - Object.assign(plugin.configs, { - recommended: { - name: '@smarttools/rxjs/recommended', - plugins: { - '@smarttools/rxjs': plugin, - }, - rules: { - ...recommended.rules, - }, - }, +const plugin: ESLintPlugin = { + ...basePlugin, + configs: { + recommended: recommendedConfig, 'recommended-legacy': recommended, - }); -} + }, +}; export = plugin; diff --git a/packages/eslint-plugin-rxjs/src/lib/configs/recommended.ts b/packages/eslint-plugin-rxjs/src/lib/configs/recommended.ts index 0fcf8fc..d538bde 100644 --- a/packages/eslint-plugin-rxjs/src/lib/configs/recommended.ts +++ b/packages/eslint-plugin-rxjs/src/lib/configs/recommended.ts @@ -2,7 +2,6 @@ import { Linter } from '@typescript-eslint/utils/ts-eslint'; export = { plugins: ['@smarttools/rxjs'], - name: '@smarttools/rxjs/recommended-legacy', rules: { '@smarttools/rxjs/no-async-subscribe': 'error', '@smarttools/rxjs/no-create': 'error', diff --git a/test/package.json b/test/package.json deleted file mode 100644 index d82dd36..0000000 --- a/test/package.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "name": "eslint-rxjs-test", - "private": true, - "type": "module", - "dependencies": { - "@smarttools/eslint-plugin-rxjs": "link:../dist/packages/eslint-plugin-rxjs" - } -} diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..bf863c4 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,3 @@ +# Tests + +The tests in this directory tests all the ways that eslint can be used in combination with this plugin. diff --git a/tests/circular-reference/index.js b/tests/circular-reference/index.js new file mode 100644 index 0000000..269b539 --- /dev/null +++ b/tests/circular-reference/index.js @@ -0,0 +1,14 @@ +const util = require('util'); +const rxjs = require('@smarttools/eslint-plugin-rxjs'); + +const result =util.inspect(rxjs, { depth: null }); + +if (result.includes('[Circular')) { + console.log('Circular reference found'); + console.log(result); + process.exit(1); +} else { + console.log('No circular reference found'); + process.exit(0); +} + diff --git a/tests/circular-reference/test.sh b/tests/circular-reference/test.sh new file mode 100755 index 0000000..fd0bdaa --- /dev/null +++ b/tests/circular-reference/test.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +# Exit on any error +set -e + +# Get the directory where the script is located +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +ROOT_DIR="$SCRIPT_DIR/../.." + +# Define source and destination paths relative to root +SRC_DIR="$ROOT_DIR/dist/packages/eslint-plugin-rxjs" +DEST_DIR="$ROOT_DIR/node_modules/@smarttools/eslint-plugin-rxjs" + +# Check if source directory exists +if [ ! -d "$SRC_DIR" ]; then + echo "Error: Source directory $SRC_DIR does not exist" + exit 1 +fi + +# Create destination parent directory if it doesn't exist +mkdir -p "$(dirname "$DEST_DIR")" + +# Remove destination directory if it exists +if [ -d "$DEST_DIR" ]; then + rm -rf "$DEST_DIR" +fi + +# Copy the directory +if cp -r "$SRC_DIR" "$DEST_DIR"; then + echo "✓ Successfully copied $SRC_DIR to $DEST_DIR" +else + echo "✗ Failed to copy directory" + exit 1 +fi + +# Run the index.js file +echo "Running index.js..." +if node "$SCRIPT_DIR/index.js"; then + echo "✓ Tests completed successfully" + exit 0 +else + echo "✗ Tests failed" + exit 1 +fi + + diff --git a/tests/circular-reference/tsconfig.json b/tests/circular-reference/tsconfig.json new file mode 100644 index 0000000..9536a0f --- /dev/null +++ b/tests/circular-reference/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig.base.json" +} diff --git a/tests/extends-from/base.eslint.config.js b/tests/extends-from/base.eslint.config.js new file mode 100644 index 0000000..abb016a --- /dev/null +++ b/tests/extends-from/base.eslint.config.js @@ -0,0 +1,41 @@ +module.exports = { + extends: ['plugin:@smarttools/rxjs/recommended-legacy'], + rules: { + '@smarttools/rxjs/finnish': [ + 'warn', + { + functions: false, + methods: false, + names: { + '^(canActivate|canActivateChild|canDeactivate|canLoad|intercept|resolve|validate|store|_store)$': + false, + }, + parameters: false, + properties: false, + strict: false, + types: { + '^EventEmitter$': false, + }, + variables: true, + }, + ], + '@smarttools/rxjs/suffix-subjects': [ + 'warn', + { + parameters: false, + properties: false, + suffix: '$$', + variables: true, + }, + ], + '@smarttools/rxjs/no-nested-subscribe': ['warn'], + '@smarttools/rxjs/no-implicit-any-catch': ['off'], + '@smarttools/rxjs/no-unsafe-takeuntil': [ + 'warn', + { + alias: ['untilDestroyed'], + }, + ], + '@smarttools/rxjs/no-async-subscribe': 'warn', + }, +}; diff --git a/tests/extends-from/eslint.config.js b/tests/extends-from/eslint.config.js new file mode 100644 index 0000000..c8dd91c --- /dev/null +++ b/tests/extends-from/eslint.config.js @@ -0,0 +1,37 @@ +const { FlatCompat } = require('@eslint/eslintrc'); +const js = require('@eslint/js'); +const join = require('path').join; +const rxjs = require('@smarttools/eslint-plugin-rxjs'); + +const compat = new FlatCompat({ + baseDirectory: __dirname, + recommendedConfig: js.configs.recommended, +}); + +module.exports = [ + { + ignores: ['**/dist'], + }, + { + plugins: { + '@smarttools/rxjs': rxjs, + }, + }, + ...compat + .config({ + extends: ['./base.eslint.config.js'], + }) + .map((config) => ({ + ...config, + files: ['**/*.ts'], + languageOptions: { + parser: require('@typescript-eslint/parser'), + parserOptions: { + project: join(__dirname, './tsconfig.json'), + }, + }, + rules: { + ...config.rules, + }, + })), +]; diff --git a/tests/extends-from/package.json b/tests/extends-from/package.json new file mode 100644 index 0000000..5bbefff --- /dev/null +++ b/tests/extends-from/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/tests/extends-from/sample.ts b/tests/extends-from/sample.ts new file mode 100644 index 0000000..d0e4d4c --- /dev/null +++ b/tests/extends-from/sample.ts @@ -0,0 +1,5 @@ +// just a sample to run lint against + +import { from } from 'rxjs'; + +from([1, 2, 3]).subscribe((x) => console.log(x)); diff --git a/tests/extends-from/test.sh b/tests/extends-from/test.sh new file mode 100755 index 0000000..d7c876c --- /dev/null +++ b/tests/extends-from/test.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# Exit on error +set -e + +# Get the directory where the script is located +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +ROOT_DIR="$SCRIPT_DIR/../.." + +# Define source and destination paths relative to root +SRC_DIR="$ROOT_DIR/dist/packages/eslint-plugin-rxjs" +DEST_DIR="$ROOT_DIR/node_modules/@smarttools/eslint-plugin-rxjs" + +# Check if source directory exists +if [ ! -d "$SRC_DIR" ]; then + echo "Error: Source directory $SRC_DIR does not exist" + exit 1 +fi + +# Create destination parent directory if it doesn't exist +mkdir -p "$(dirname "$DEST_DIR")" + +# Remove destination directory if it exists +if [ -d "$DEST_DIR" ]; then + rm -rf "$DEST_DIR" +fi + +# Copy the directory +if cp -r "$SRC_DIR" "$DEST_DIR"; then + echo "✓ Successfully copied $SRC_DIR to $DEST_DIR" +else + echo "✗ Failed to copy directory" + exit 1 +fi + +# Run ESLint with the specific config and test file +if pnpm eslint --config $SCRIPT_DIR/eslint.config.js $SCRIPT_DIR/sample.ts; then + echo -e "\n✅ Linting PASSED" +else + echo -e "\n❌ Linting FAILED" + exit 1 +fi diff --git a/tests/extends-from/tsconfig.json b/tests/extends-from/tsconfig.json new file mode 100644 index 0000000..9536a0f --- /dev/null +++ b/tests/extends-from/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig.base.json" +} diff --git a/tests/test.sh b/tests/test.sh new file mode 100755 index 0000000..cbbfc3a --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +# Exit on error +set -e + +# Get the directory where the script is located +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +# Initialize counters +TOTAL_TESTS=4 +PASSED_TESTS=0 +FAILED_TESTS=0 +FAILED_NAMES=() + +# Function to run a test and track its result +function run_test() { + local test_name=$1 + local test_path=$2 + + echo -e "\n📋 Running test: $test_name" + if bash "$test_path"; then + echo "✅ $test_name passed" + PASSED_TESTS=$((PASSED_TESTS + 1)) + else + echo "❌ $test_name failed" + FAILED_TESTS=$((FAILED_TESTS + 1)) + FAILED_NAMES+=("$test_name") + fi +} + +# Run each test +run_test "ESM JS Tests" "$SCRIPT_DIR/esm-js/test.sh" +run_test "Using Recommended Tests" "$SCRIPT_DIR/using-recommended/test.sh" +run_test "Extends From Tests" "$SCRIPT_DIR/extends-from/test.sh" +run_test "Circular Reference Tests" "$SCRIPT_DIR/circular-reference/test.sh" + +# Print summary report +echo -e "\n📊 Test Summary Report" +echo "=======================" +echo "Total Tests: $TOTAL_TESTS" +echo "Passed: $PASSED_TESTS" +echo "Failed: $FAILED_TESTS" + +if [ ${#FAILED_NAMES[@]} -ne 0 ]; then + echo -e "\n❌ Failed Tests:" + for name in "${FAILED_NAMES[@]}"; do + echo "- $name" + done + exit 1 +fi + +echo -e "\n✨ All tests passed!" +exit 0 diff --git a/tests/using-recommended/eslint.config.js b/tests/using-recommended/eslint.config.js new file mode 100644 index 0000000..bd53d78 --- /dev/null +++ b/tests/using-recommended/eslint.config.js @@ -0,0 +1,21 @@ +const rxjs = require('@smarttools/eslint-plugin-rxjs'); +const typescriptEslintParser = require('@typescript-eslint/parser'); + +module.exports = [ + rxjs.configs.recommended, + { + files: ['**/*.ts'], + plugins: { + rxjs, + }, + }, + { + languageOptions: { + parser: typescriptEslintParser, + parserOptions: { + project: './tsconfig.json', + tsconfigRootDir: __dirname, + }, + }, + }, +]; diff --git a/tests/using-recommended/package.json b/tests/using-recommended/package.json new file mode 100644 index 0000000..5bbefff --- /dev/null +++ b/tests/using-recommended/package.json @@ -0,0 +1,3 @@ +{ + "type": "commonjs" +} diff --git a/tests/using-recommended/sample.ts b/tests/using-recommended/sample.ts new file mode 100644 index 0000000..d0e4d4c --- /dev/null +++ b/tests/using-recommended/sample.ts @@ -0,0 +1,5 @@ +// just a sample to run lint against + +import { from } from 'rxjs'; + +from([1, 2, 3]).subscribe((x) => console.log(x)); diff --git a/tests/using-recommended/test.sh b/tests/using-recommended/test.sh new file mode 100755 index 0000000..d7c876c --- /dev/null +++ b/tests/using-recommended/test.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +# Exit on error +set -e + +# Get the directory where the script is located +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +ROOT_DIR="$SCRIPT_DIR/../.." + +# Define source and destination paths relative to root +SRC_DIR="$ROOT_DIR/dist/packages/eslint-plugin-rxjs" +DEST_DIR="$ROOT_DIR/node_modules/@smarttools/eslint-plugin-rxjs" + +# Check if source directory exists +if [ ! -d "$SRC_DIR" ]; then + echo "Error: Source directory $SRC_DIR does not exist" + exit 1 +fi + +# Create destination parent directory if it doesn't exist +mkdir -p "$(dirname "$DEST_DIR")" + +# Remove destination directory if it exists +if [ -d "$DEST_DIR" ]; then + rm -rf "$DEST_DIR" +fi + +# Copy the directory +if cp -r "$SRC_DIR" "$DEST_DIR"; then + echo "✓ Successfully copied $SRC_DIR to $DEST_DIR" +else + echo "✗ Failed to copy directory" + exit 1 +fi + +# Run ESLint with the specific config and test file +if pnpm eslint --config $SCRIPT_DIR/eslint.config.js $SCRIPT_DIR/sample.ts; then + echo -e "\n✅ Linting PASSED" +else + echo -e "\n❌ Linting FAILED" + exit 1 +fi diff --git a/tests/using-recommended/tsconfig.json b/tests/using-recommended/tsconfig.json new file mode 100644 index 0000000..9536a0f --- /dev/null +++ b/tests/using-recommended/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tsconfig.base.json" +}