generated from MengLinMaker/npm-library-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: switch to @typescript-eslint for type inferencing (#43)
- Loading branch information
Showing
9 changed files
with
316 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ESLintUtils } from '@typescript-eslint/utils' | ||
|
||
export const createRule = ESLintUtils.RuleCreator(() => '') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,61 @@ | ||
import type { Rule } from 'eslint' | ||
import type { Identifier } from 'estree' | ||
import type { RuntimeName } from 'runtime-compat-data' | ||
import data from 'runtime-compat-data' | ||
import type { RuleConfig } from '../types' | ||
import { compatErrorMessage } from '../utils/compatErrorMessage' | ||
import { filterSupportCompatData } from '../utils/filterSupportCompatData' | ||
import { mapCompatData } from '../utils/mapCompatData' | ||
import { createRule } from './createRule' | ||
|
||
/** | ||
* Creates a runtime-compat rule. | ||
* @param filterRuntimes - List of runtimes to check. | ||
* @returns ESLint rule. | ||
*/ | ||
export const runtimeCompatRule = ( | ||
filterRuntimes: RuntimeName[], | ||
ruleConfig: RuleConfig, | ||
): Rule.RuleModule => ({ | ||
meta: { | ||
docs: { | ||
description: 'Ensure cross-runtime API compatibility', | ||
export const runtimeCompatRule = (filterRuntimes: data.RuntimeName[], ruleConfig: RuleConfig) => | ||
createRule({ | ||
name: 'runtime-compat', | ||
meta: { | ||
docs: { | ||
description: 'Ensure cross-runtime API compatibility', | ||
}, | ||
type: 'problem', | ||
schema: [], | ||
messages: {}, | ||
}, | ||
type: 'problem', | ||
}, | ||
create: (context) => { | ||
const unsupportedApis = filterSupportCompatData(mapCompatData(data), filterRuntimes, ruleConfig) | ||
defaultOptions: [], | ||
create: (context) => { | ||
const unsupportedApis = filterSupportCompatData( | ||
mapCompatData(data), | ||
filterRuntimes, | ||
ruleConfig, | ||
) | ||
|
||
const reportError = (node: Identifier, unsupportesApiId: string) => { | ||
const apiInfo = unsupportedApis[unsupportesApiId] | ||
if (!apiInfo) return | ||
const message = compatErrorMessage(unsupportesApiId, apiInfo) | ||
context.report({ node, message }) | ||
} | ||
const reportError = (node: Identifier, unsupportesApiId: string) => { | ||
const apiInfo = unsupportedApis[unsupportesApiId] | ||
if (!apiInfo) return | ||
const message = compatErrorMessage(unsupportesApiId, apiInfo) | ||
// @ts-expect-error using typescript-eslint | ||
context.report({ node, message }) | ||
} | ||
|
||
return { | ||
Identifier: (identifierNode) => { | ||
// Detect a class constructor | ||
if (identifierNode.parent.type === 'NewExpression') { | ||
const unsupportesApiId = JSON.stringify([identifierNode.name]) | ||
reportError(identifierNode, unsupportesApiId) | ||
} | ||
// Detect variable assignment from class | ||
if (identifierNode.parent.type === 'VariableDeclarator') { | ||
if (identifierNode.parent.init === identifierNode) { | ||
console.log(identifierNode.name) | ||
return { | ||
TSInterfaceDeclaration: (node) => { | ||
console.log(node) | ||
}, | ||
Identifier: (identifierNode) => { | ||
// Detect a class constructor | ||
if (identifierNode.parent.type === 'NewExpression') { | ||
const unsupportesApiId = JSON.stringify([identifierNode.name]) | ||
reportError(identifierNode, unsupportesApiId) | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
}) | ||
// Detect variable assignment from class | ||
if (identifierNode.parent.type === 'VariableDeclarator') { | ||
if (identifierNode.parent.init === identifierNode) { | ||
const unsupportesApiId = JSON.stringify([identifierNode.name]) | ||
reportError(identifierNode, unsupportesApiId) | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
needed for eslint rule tests | ||
https://typescript-eslint.io/developers/custom-rules#testing | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,50 @@ | ||
import { RuleTester } from 'eslint' | ||
import type { RuntimeName } from 'runtime-compat-data' | ||
import { it } from 'vitest' | ||
import { runtimeCompatRule } from '../../src/rules/runtime-compat' | ||
import { ruleTester } from './setup' | ||
|
||
const ruleTester = new RuleTester({ | ||
languageOptions: { ecmaVersion: 2015, sourceType: 'module' }, | ||
}) | ||
const filterRuntimes: RuntimeName[] = ['node'] | ||
const cacheErrorMsg = | ||
"'Cache' - Unsupported API in node.\nDocs - https://developer.mozilla.org/docs/Web/API/Cache" | ||
|
||
it('should pass eslint "runtime-compat" test', () => { | ||
ruleTester.run( | ||
'runtime-compat', | ||
runtimeCompatRule(filterRuntimes, { | ||
deprecated: false, | ||
experimental: false, | ||
}), | ||
{ | ||
valid: [{ code: 'fetch("https://www.google.com")' }], | ||
invalid: [ | ||
{ | ||
// Detect unsupported API constructor call. | ||
code: 'const a = new Cache(); let b = new Cache(); b = new Cache()', | ||
errors: [ | ||
{ message: cacheErrorMsg }, | ||
{ message: cacheErrorMsg }, | ||
{ message: cacheErrorMsg }, | ||
], | ||
}, | ||
{ | ||
// Detect unsupported API variable assignment. | ||
code: 'const n = Cache; let b = new Cache; b = new Cache', | ||
errors: [ | ||
{ message: cacheErrorMsg }, | ||
{ message: cacheErrorMsg }, | ||
{ message: cacheErrorMsg }, | ||
], | ||
}, | ||
], | ||
}, | ||
) | ||
}) | ||
ruleTester.run( | ||
'runtime-compat', | ||
runtimeCompatRule(filterRuntimes, { | ||
deprecated: false, | ||
experimental: false, | ||
}), | ||
{ | ||
valid: [ | ||
{ | ||
code: 'fetch("https://www.google.com")', | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
// Detect unsupported API constructor call. | ||
code: /*javascript*/ ` | ||
const a = new Cache() | ||
let b = new Cache() | ||
b = new Cache() | ||
`, | ||
errors: [ | ||
{ message: cacheErrorMsg }, | ||
{ message: cacheErrorMsg }, | ||
{ message: cacheErrorMsg }, | ||
], | ||
}, | ||
{ | ||
// Detect unsupported API variable assignment. | ||
code: /*javascript*/ ` | ||
const n = Cache | ||
let b = new Cache | ||
b = new Cache | ||
`, | ||
errors: [ | ||
{ message: cacheErrorMsg }, | ||
{ message: cacheErrorMsg }, | ||
{ message: cacheErrorMsg }, | ||
], | ||
}, | ||
], | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as path from 'node:path' | ||
import { RuleTester } from '@typescript-eslint/rule-tester' | ||
import { afterAll, describe, it } from 'vitest' | ||
|
||
RuleTester.afterAll = afterAll | ||
RuleTester.it = it | ||
RuleTester.itOnly = it.only | ||
RuleTester.describe = describe | ||
|
||
export const ruleTester = new RuleTester({ | ||
languageOptions: { | ||
parserOptions: { | ||
project: path.resolve(__dirname, 'resources', 'tsconfig.json'), | ||
tsconfigRootDir: path.resolve(__dirname, 'resources'), | ||
}, | ||
}, | ||
}) |