Skip to content

Commit

Permalink
feat: types with intellisense (eslint-types#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimava committed Mar 22, 2023
1 parent b333fd8 commit a1bb4c2
Show file tree
Hide file tree
Showing 41 changed files with 1,033 additions and 82 deletions.
24 changes: 21 additions & 3 deletions scripts/generate-rule-files/src/rule-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class RuleFile {
const ruleConfigImportPath: string = `${'../'.repeat(
nestedDepth,
)}rule-config`;
this.content += `import type { RuleConfig } from '${ruleConfigImportPath}'\n\n`;
this.content += `import type { RuleConfig, RuleConfigExt } from '${ruleConfigImportPath}'\n\n`;
}

/**
Expand Down Expand Up @@ -94,12 +94,18 @@ export class RuleFile {
.output();
}

private readonly ruleSchemas: {
Setting?: string;
Config?: string;
Option?: string;
} = {};

/**
* Generate a type from a JSON schema and append it to the file content.
*/
private async appendJsonSchemaType(
schema: JSONSchema4,
comment: string,
comment: 'Setting' | 'Config' | 'Option',
): Promise<void> {
const type: string = await generateTypeFromSchema(
schema,
Expand All @@ -109,6 +115,12 @@ export class RuleFile {
const jsdoc: string = JsDocBuilder.build().add(`${comment}.`).output();
this.content += `\n${jsdoc}`;
this.content += `\n${type}\n`;
// FIXME: upper one didn't work properly so I made 2nd replace
this.ruleSchemas[comment] = type
.replace(/export (type|interface) =? ?\w+/, '')
.trim()
.replace(/^=/, '')
.trim();
}

/**
Expand Down Expand Up @@ -175,9 +187,15 @@ export class RuleFile {

this.content += this.generateTypeJsDoc() + '\n';

const ruleRuleRule: string = `RuleConfigExt<${
this.ruleSchemas.Option ?? 'never'
},${this.ruleSchemas.Config ?? 'never'},${
this.ruleSchemas.Setting ?? 'never'
}>`;

this.content += `export interface ${ruleName}Rule {`;
this.content += `${this.generateTypeJsDoc()}\n`;
this.content += `'${rulePrefix}${this.ruleName}': ${ruleName}RuleConfig;`;
this.content += `'${rulePrefix}${this.ruleName}': ${ruleRuleRule};`;
this.content += '}';
}

Expand Down
4 changes: 2 additions & 2 deletions src/rules/n/callback-return.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleConfig } from '../rule-config';
import type { RuleConfig, RuleConfigExt } from '../rule-config';

/**
* Option.
Expand Down Expand Up @@ -28,5 +28,5 @@ export interface CallbackReturnRule {
*
* @see [callback-return](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/callback-return.md)
*/
'n/callback-return': CallbackReturnRuleConfig;
'n/callback-return': RuleConfigExt<string[], never, never>;
}
10 changes: 8 additions & 2 deletions src/rules/n/exports-style.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleConfig } from '../rule-config';
import type { RuleConfig, RuleConfigExt } from '../rule-config';

/**
* Config.
Expand Down Expand Up @@ -35,5 +35,11 @@ export interface ExportsStyleRule {
*
* @see [exports-style](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/exports-style.md)
*/
'n/exports-style': ExportsStyleRuleConfig;
'n/exports-style': RuleConfigExt<
'module.exports' | 'exports',
{
allowBatchAssign?: boolean;
},
never
>;
}
10 changes: 8 additions & 2 deletions src/rules/n/file-extension-in-import.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleConfig } from '../rule-config';
import type { RuleConfig, RuleConfigExt } from '../rule-config';

/**
* Config.
Expand Down Expand Up @@ -39,5 +39,11 @@ export interface FileExtensionInImportRule {
*
* @see [file-extension-in-import](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/file-extension-in-import.md)
*/
'n/file-extension-in-import': FileExtensionInImportRuleConfig;
'n/file-extension-in-import': RuleConfigExt<
'always' | 'never',
{
[k: string]: 'always' | 'never';
},
never
>;
}
4 changes: 2 additions & 2 deletions src/rules/n/global-require.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleConfig } from '../rule-config';
import type { RuleConfig, RuleConfigExt } from '../rule-config';

/**
* Require `require()` calls to be placed at top-level module scope.
Expand All @@ -18,5 +18,5 @@ export interface GlobalRequireRule {
*
* @see [global-require](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/global-require.md)
*/
'n/global-require': GlobalRequireRuleConfig;
'n/global-require': RuleConfigExt<never, never, never>;
}
4 changes: 2 additions & 2 deletions src/rules/n/handle-callback-err.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleConfig } from '../rule-config';
import type { RuleConfig, RuleConfigExt } from '../rule-config';

/**
* Option.
Expand Down Expand Up @@ -28,5 +28,5 @@ export interface HandleCallbackErrRule {
*
* @see [handle-callback-err](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/handle-callback-err.md)
*/
'n/handle-callback-err': HandleCallbackErrRuleConfig;
'n/handle-callback-err': RuleConfigExt<string, never, never>;
}
4 changes: 2 additions & 2 deletions src/rules/n/no-callback-literal.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleConfig } from '../rule-config';
import type { RuleConfig, RuleConfigExt } from '../rule-config';

/**
* Enforce Node.js-style error-first callback pattern is followed.
Expand All @@ -18,5 +18,5 @@ export interface NoCallbackLiteralRule {
*
* @see [no-callback-literal](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/no-callback-literal.md)
*/
'n/no-callback-literal': NoCallbackLiteralRuleConfig;
'n/no-callback-literal': RuleConfigExt<never, never, never>;
}
117 changes: 115 additions & 2 deletions src/rules/n/no-deprecated-api.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleConfig } from '../rule-config';
import type { RuleConfig, RuleConfigExt } from '../rule-config';

/**
* Option.
Expand Down Expand Up @@ -137,5 +137,118 @@ export interface NoDeprecatedApiRule {
*
* @see [no-deprecated-api](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/no-deprecated-api.md)
*/
'n/no-deprecated-api': NoDeprecatedApiRuleConfig;
'n/no-deprecated-api': RuleConfigExt<
{
version?: string;
ignoreModuleItems?: (
| '_linklist'
| '_stream_wrap'
| 'async_hooks.currentId'
| 'async_hooks.triggerId'
| 'buffer.Buffer()'
| 'new buffer.Buffer()'
| 'buffer.SlowBuffer'
| 'constants'
| 'crypto._toBuf'
| 'crypto.Credentials'
| 'crypto.DEFAULT_ENCODING'
| 'crypto.createCipher'
| 'crypto.createCredentials'
| 'crypto.createDecipher'
| 'crypto.fips'
| 'crypto.prng'
| 'crypto.pseudoRandomBytes'
| 'crypto.rng'
| 'domain'
| 'events.EventEmitter.listenerCount'
| 'events.listenerCount'
| 'freelist'
| 'fs.SyncWriteStream'
| 'fs.exists'
| 'fs.lchmod'
| 'fs.lchmodSync'
| 'http.createClient'
| 'module.Module.createRequireFromPath'
| 'module.Module.requireRepl'
| 'module.Module._debug'
| 'module.createRequireFromPath'
| 'module.requireRepl'
| 'module._debug'
| 'net._setSimultaneousAccepts'
| 'os.getNetworkInterfaces'
| 'os.tmpDir'
| 'path._makeLong'
| 'process.EventEmitter'
| 'process.assert'
| 'process.binding'
| 'process.env.NODE_REPL_HISTORY_FILE'
| 'process.report.triggerReport'
| 'punycode'
| 'readline.codePointAt'
| 'readline.getStringWidth'
| 'readline.isFullWidthCodePoint'
| 'readline.stripVTControlCharacters'
| 'safe-buffer.Buffer()'
| 'new safe-buffer.Buffer()'
| 'safe-buffer.SlowBuffer'
| 'sys'
| 'timers.enroll'
| 'timers.unenroll'
| 'tls.CleartextStream'
| 'tls.CryptoStream'
| 'tls.SecurePair'
| 'tls.convertNPNProtocols'
| 'tls.createSecurePair'
| 'tls.parseCertString'
| 'tty.setRawMode'
| 'url.parse'
| 'url.resolve'
| 'util.debug'
| 'util.error'
| 'util.isArray'
| 'util.isBoolean'
| 'util.isBuffer'
| 'util.isDate'
| 'util.isError'
| 'util.isFunction'
| 'util.isNull'
| 'util.isNullOrUndefined'
| 'util.isNumber'
| 'util.isObject'
| 'util.isPrimitive'
| 'util.isRegExp'
| 'util.isString'
| 'util.isSymbol'
| 'util.isUndefined'
| 'util.log'
| 'util.print'
| 'util.pump'
| 'util.puts'
| 'util._extend'
| 'vm.runInDebugContext'
)[];
ignoreGlobalItems?: (
| 'Buffer()'
| 'new Buffer()'
| 'COUNTER_NET_SERVER_CONNECTION'
| 'COUNTER_NET_SERVER_CONNECTION_CLOSE'
| 'COUNTER_HTTP_SERVER_REQUEST'
| 'COUNTER_HTTP_SERVER_RESPONSE'
| 'COUNTER_HTTP_CLIENT_REQUEST'
| 'COUNTER_HTTP_CLIENT_RESPONSE'
| 'GLOBAL'
| 'Intl.v8BreakIterator'
| 'require.extensions'
| 'root'
| 'process.EventEmitter'
| 'process.assert'
| 'process.binding'
| 'process.env.NODE_REPL_HISTORY_FILE'
| 'process.report.triggerReport'
)[];
ignoreIndirectDependencies?: boolean;
},
never,
never
>;
}
4 changes: 2 additions & 2 deletions src/rules/n/no-exports-assign.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleConfig } from '../rule-config';
import type { RuleConfig, RuleConfigExt } from '../rule-config';

/**
* Disallow the assignment to `exports`.
Expand All @@ -18,5 +18,5 @@ export interface NoExportsAssignRule {
*
* @see [no-exports-assign](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/no-exports-assign.md)
*/
'n/no-exports-assign': NoExportsAssignRuleConfig;
'n/no-exports-assign': RuleConfigExt<never, never, never>;
}
46 changes: 44 additions & 2 deletions src/rules/n/no-extraneous-import.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleConfig } from '../rule-config';
import type { RuleConfig, RuleConfigExt } from '../rule-config';

/**
* Option.
Expand Down Expand Up @@ -67,5 +67,47 @@ export interface NoExtraneousImportRule {
*
* @see [no-extraneous-import](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/no-extraneous-import.md)
*/
'n/no-extraneous-import': NoExtraneousImportRuleConfig;
'n/no-extraneous-import': RuleConfigExt<
{
allowModules?: string[];
convertPath?:
| {
/**
* @minItems 2
* @maxItems 2
*
*/
[k: string]: [string, string];
}
| [
{
/**
* @minItems 1
*/
include: [string, ...string[]];
exclude?: string[];
/**
* @minItems 2
* @maxItems 2
*/
replace: [string, string];
},
...{
/**
* @minItems 1
*/
include: [string, ...string[]];
exclude?: string[];
/**
* @minItems 2
* @maxItems 2
*/
replace: [string, string];
}[],
];
resolvePaths?: string[];
},
never,
never
>;
}
47 changes: 45 additions & 2 deletions src/rules/n/no-extraneous-require.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { RuleConfig } from '../rule-config';
import type { RuleConfig, RuleConfigExt } from '../rule-config';

/**
* Option.
Expand Down Expand Up @@ -68,5 +68,48 @@ export interface NoExtraneousRequireRule {
*
* @see [no-extraneous-require](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/no-extraneous-require.md)
*/
'n/no-extraneous-require': NoExtraneousRequireRuleConfig;
'n/no-extraneous-require': RuleConfigExt<
{
allowModules?: string[];
convertPath?:
| {
/**
* @minItems 2
* @maxItems 2
*
*/
[k: string]: [string, string];
}
| [
{
/**
* @minItems 1
*/
include: [string, ...string[]];
exclude?: string[];
/**
* @minItems 2
* @maxItems 2
*/
replace: [string, string];
},
...{
/**
* @minItems 1
*/
include: [string, ...string[]];
exclude?: string[];
/**
* @minItems 2
* @maxItems 2
*/
replace: [string, string];
}[],
];
resolvePaths?: string[];
tryExtensions?: string[];
},
never,
never
>;
}
Loading

0 comments on commit a1bb4c2

Please sign in to comment.