Skip to content

Commit

Permalink
feat: detect class instantiation with NewExpression and type inference (
Browse files Browse the repository at this point in the history
  • Loading branch information
MengLinMaker authored Nov 4, 2024
2 parents e49fbd3 + 688813c commit 4c90e13
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 44 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"@commitlint/config-conventional": "19.4.1",
"@eslint/js": "^9.9.1",
"@types/eslint": "^9.6.1",
"@types/node": "^22.5.2",
"@typescript-eslint/rule-tester": "8.12.2",
"@vitest/coverage-v8": "^2.0.5",
"cz-git": "1.10.1",
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 10 additions & 18 deletions src/rules/runtime-compat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Identifier } from 'estree'
import { ESLintUtils } from '@typescript-eslint/utils'
import data from 'runtime-compat-data'
import type { RuleConfig } from '../types'
import { compatErrorMessage } from '../utils/compatErrorMessage'
Expand All @@ -24,13 +24,15 @@ export const runtimeCompatRule = (filterRuntimes: data.RuntimeName[], ruleConfig
},
defaultOptions: [],
create: (context) => {
const services = ESLintUtils.getParserServices(context)

const unsupportedApis = filterSupportCompatData(
mapCompatData(data),
filterRuntimes,
ruleConfig,
)

const reportError = (node: Identifier, unsupportesApiId: string) => {
const reportError = <T>(node: T, unsupportesApiId: string) => {
const apiInfo = unsupportedApis[unsupportesApiId]
if (!apiInfo) return
const message = compatErrorMessage(unsupportesApiId, apiInfo)
Expand All @@ -39,22 +41,12 @@ export const runtimeCompatRule = (filterRuntimes: data.RuntimeName[], ruleConfig
}

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)
}
}
NewExpression: (node) => {
const type = services.getTypeAtLocation(node)
const checker = services.program.getTypeChecker()
const className = checker.typeToString(type)
const unsupportesApiId = JSON.stringify([className])
reportError(node, unsupportesApiId)
},
}
},
Expand Down
31 changes: 9 additions & 22 deletions tests/rules/runtimeCompatRule.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { runtimeCompatRule } from '../../src/rules/runtime-compat'
import { ruleTester } from './setup'

const filterRuntimes: RuntimeName[] = ['node']
const cacheErrorMsg =
const cacheInstantiationError =
"'Cache' - Unsupported API in node.\nDocs - https://developer.mozilla.org/docs/Web/API/Cache"

ruleTester.run(
Expand All @@ -13,37 +13,24 @@ ruleTester.run(
experimental: false,
}),
{
valid: [
{
code: 'fetch("https://www.google.com")',
},
],
valid: ['fetch("https://www.google.com")'],
invalid: [
{
// Detect unsupported API constructor call.
code: /*javascript*/ `
const a = new Cache()
let b = new Cache()
b = new Cache()
const _Cache = Cache
let cache = new _Cache()
cache.add("test.html")
`,
errors: [
{ message: cacheErrorMsg },
{ message: cacheErrorMsg },
{ message: cacheErrorMsg },
],
errors: [{ message: cacheInstantiationError }],
},
{
// Detect unsupported API variable assignment.
code: /*javascript*/ `
const n = Cache
let b = new Cache
b = new Cache
const cache = new Cache()
cache.add("test.html")
`,
errors: [
{ message: cacheErrorMsg },
{ message: cacheErrorMsg },
{ message: cacheErrorMsg },
],
errors: [{ message: cacheInstantiationError }],
},
],
},
Expand Down

0 comments on commit 4c90e13

Please sign in to comment.