Skip to content

Commit

Permalink
fix: plugin ignore exports (#6500)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Nov 6, 2024
1 parent cfbb309 commit 637260e
Show file tree
Hide file tree
Showing 23 changed files with 279 additions and 76 deletions.
2 changes: 1 addition & 1 deletion packages/cspell-eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ interface Options {
*/
generateSuggestions: boolean;
/**
* Ignore import and require names
* Ignore import, require names, and export from names
* @default true
*/
ignoreImports?: boolean;
Expand Down
16 changes: 5 additions & 11 deletions packages/cspell-eslint-plugin/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ts-check
import eslint from '@eslint/js';
import nodePlugin from 'eslint-plugin-n';
import simpleImportSort from 'eslint-plugin-simple-import-sort';
Expand Down Expand Up @@ -75,21 +76,14 @@ export default tsEslint.config(
},
],
'node/no-unsupported-features/es-syntax': 'off',
// 'import/no-unresolved': 'off',
'import/no-unresolved': 'off',
'@typescript-eslint/consistent-type-imports': 'error',
},
},
{
files: ['**/*.ts', '**/*.mts', '**/*.cts'],
rules: {
'node/no-missing-import': [
'off',
'n/no-missing-import': [
'error',
{
tryExtensions: ['.js', '.d.ts', '.ts'],
allowModules: ['estree'],
},
],
'node/no-unsupported-features/es-syntax': 'off',
'import/no-unresolved': 'off',
},
},
{
Expand Down
33 changes: 33 additions & 0 deletions packages/cspell-eslint-plugin/fixtures/simple/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// ts-check
// import eslint from '@eslint/js';
import cspellRecommended from '@cspell/eslint-plugin/recommended';
import tsESLint from 'typescript-eslint';
import Path from 'node:path';

/**
* @type { import("eslint").Linter.Config[] }
*/
export default tsESLint.config(
// eslint.configs.recommended,
{
files: ['**/*.*ts'],
plugins: {
'@typescript-eslint': tsESLint.plugin,
},
languageOptions: {
parser: tsESLint.parser,
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
rules: {},
},
cspellRecommended,
{
files: ['**/*.*js', '**/*.*ts'],
rules: {
'@cspell/spellchecker': ['error'],
},
},
);
20 changes: 20 additions & 0 deletions packages/cspell-eslint-plugin/fixtures/simple/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@internal/eslint-plugin-fixtures-simple",
"version": "1.0.0",
"description": "",
"type": "module",
"private": true,
"main": "index.js",
"scripts": {
"test": "pnpm eslint ."
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@cspell/eslint-plugin": "workspace:*",
"@eslint/js": "^9.14.0",
"eslint": "^9.14.0",
"typescript-eslint": "^8.13.0"
}
}
19 changes: 19 additions & 0 deletions packages/cspell-eslint-plugin/fixtures/simple/sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict"

const fs = require('fs');
const commander = require('commander');

export const help = `
This is some help.
`;

/**
* run code
*/
export function command(cmd) {
const prompt = "Enter your name:";

return prompt;
}

command(commander);
62 changes: 62 additions & 0 deletions packages/cspell-eslint-plugin/fixtures/simple/sample.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import type { Rule } from 'eslint';
import { join, relative as rel } from 'path';
import { promises as fs } from 'fs';
import type { Literal, TemplateElement, Identifier, Node, Comment } from 'estree';
import { format } from 'util';
import path from 'path/posix';

export { configg } from 'prettier-config-cityssm';

const _utils = {
path,
format,
};

let p = _utils.path.join.name;

interface PluginRules {
['cspell']: Rule.RuleModule;
}

const meta: Rule.RuleMetaData = {
docs: {
description: 'CSpell',
},
};

/**
* This is my Sample class.
*/
export class MyClass {
constructor(readonly name: string) {}

get length() {
return this.name.length;
}

getName() {
return this.name;
}

asFilename() {
return createFilename(this.name, '.txt');
}

asRelative(toDir: string) {
return rel(this.asFilename(), toDir);
}

async read(): Promise<string> {
const content = await fs.readFile(this.asFilename(), 'utf-8');
return content;
}
}

function createFilename(name: string, ext: string): string {
return join('./', name + ext);
}
export class AppError extends Error {
constructor(readonly msg: string) {
super(msg);
}
}
21 changes: 21 additions & 0 deletions packages/cspell-eslint-plugin/fixtures/simple/sampleESM.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { promises as fs } from 'fs';

export { configg } from 'prettier-config-cityssm';

export const LITERAL_NUM = 42;
export const LITERAL_STR_SINGLE = 'Guide to the Galaxy';
export const LITERAL_STR_DOUBLE = 'To Infinity and Beyond';
export const LITERAL_COOKED = 'cafe\u0301';
export const UNDEFINED = undefined;
export const NULL = null;
export const BIG_INT = 1n;

function mapDir(dir) {
return `type: ${dir.isFile ? 'F' : ' '}${dir.isDirectory() ? 'D' : ' '} name: ${dir.name}`;
}

export async function listFiles() {
const dirs = await fs.readdir('.', { withFileTypes: true });
const entries = dirs.map(mapDir);
console.log(entries.join('\n'));
}
10 changes: 10 additions & 0 deletions packages/cspell-eslint-plugin/fixtures/simple/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@tsconfig/node18/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"rootDir": ".",
"outDir": "dist"
},
"include": ["."],
"ignore": ["dist"]
}
4 changes: 3 additions & 1 deletion packages/cspell-eslint-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
"coverage": "echo coverage",
"test-watch": "pnpm run test -- --watch",
"test-yaml": "npx mocha --timeout 10000 \"dist/**/yaml.test.mjs\"",
"test": "npx mocha --timeout 10000 \"dist/**/*.test.mjs\""
"test-eslint": "npx mocha --timeout 10000 \"dist/**/*.test.mjs\"",
"test-samples": "pnpm -r --filter @internal/eslint-plugin-fixtures-simple test",
"test": "pnpm run test-eslint && pnpm run test-samples"
},
"repository": {
"type": "git",
Expand Down
3 changes: 1 addition & 2 deletions packages/cspell-eslint-plugin/src/common/logger.cts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ export class Logger {
if (!this.enabled) return;
if (!this.logToFile) return console.log(...p);
const message = new Date().toISOString() + ' ' + prefixLines(format(...p), ' ') + '\n';
this.useAsync
return this.useAsync
? fs.appendFile(this.logFile, message, (err) => err && console.error(err))
: fs.appendFileSync(this.logFile, message);
return;
}

log = this._log.bind(this);
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-eslint-plugin/src/plugin/configs.cts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Linter } from 'eslint';
import { plugin } from './cspell-eslint-plugin.cjs';
export * as recommended from './recommended.cjs';

export const debug: Linter.FlatConfig = {
export const debug: Linter.Config = {
plugins: {
'@cspell': plugin,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Program } from 'estree';
import { createSyncFn } from 'synckit';

import { getDefaultLogger } from '../common/logger.cjs';
import type { Issue, SpellCheckFn } from '../worker/types.cjs';
import type { Issue, SpellCheckFn } from '../worker/types.mjs';
import { normalizeOptions } from './defaultCheckOptions.cjs';

type ESlintPlugin = ESLint.Plugin;
Expand All @@ -16,7 +16,6 @@ const optionsSchema = JSON.parse(readFileSync(pathJoin(__dirname, '../../assets/

const schema = optionsSchema as unknown as Rule.RuleMetaData['schema'];

// eslint-disable-next-line n/no-missing-require
const spellCheck = createSyncFn<SpellCheckFn>(require.resolve('../worker/worker.mjs'));

interface ExtendedSuggestion {
Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-eslint-plugin/src/plugin/recommended.cts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Linter } from 'eslint';

import { plugin } from './cspell-eslint-plugin.cjs';

const config: Linter.FlatConfig = {
const config: Linter.Config = {
plugins: {
'@cspell': plugin,
},
Expand Down
11 changes: 11 additions & 0 deletions packages/cspell-eslint-plugin/src/test/index.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const parsers: Record<string, Linter.Parser | undefined> = {
'.ts': typeScriptParser,
};

// cspell:ignore configg cityssm

type ValidTestCase = RuleTester.ValidTestCase;
type Options = Partial<RuleOptions>;

Expand All @@ -32,6 +34,8 @@ const KnownErrors: TestCaseError[] = [
ce('Unknown word: "bestbusiness"', 0),
ce('Unknown word: "muawhahaha"', 0),
ce('Unknown word: "uuuug"', 0),
ce('Unknown word: "configg"', 8),
ce('Unknown word: "cityssm"', 8),
ce('Unknown word: "grrr"', 8),
ce('Unknown word: "GRRRRRR"', 1),
ce('Unknown word: "UUUUUG"', 3),
Expand Down Expand Up @@ -59,6 +63,8 @@ ruleTester.run('cspell', Rule.rules.spellchecker, {
readSample('sample.js'),
readSample('sample.ts'),
readSample('sampleESM.mjs'),
readFix('simple/sample.ts'),
readFix('simple/sampleESM.mjs'),
readFix('with-errors/strings.ts', { checkStrings: false, checkStringTemplates: false }),
readFix('with-errors/imports.ts'),
readFix('with-errors/sampleESM.mjs', {
Expand Down Expand Up @@ -200,6 +206,11 @@ ruleTester.run('cspell', Rule.rules.spellchecker, {
readInvalid('issue-4870/sample.js', ['Unknown word: "friendz"'], {
cspell: { allowCompoundWords: true },
}),
readInvalid(
'simple/sample.ts',
['Unknown word: "configg"', 'Unknown word: "configg"', 'Unknown word: "cityssm"'],
{ ignoreImports: false },
),
],
});

Expand Down
2 changes: 1 addition & 1 deletion packages/cspell-eslint-plugin/src/worker/ASTPath.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ASTNode } from './ASTNode.cjs';
import type { ASTNode } from './ASTNode.mjs';

export type Key = string | number | symbol | null | undefined;

Expand Down
Loading

0 comments on commit 637260e

Please sign in to comment.