Skip to content

Commit

Permalink
fix: a few types
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Nov 20, 2024
1 parent 8342026 commit 5d6cd07
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 27 deletions.
9 changes: 5 additions & 4 deletions src/rules/consistent-type-specifier-style.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TSESLint, TSESTree } from '@typescript-eslint/utils'

import { createRule } from '../utils'
import { createRule, getValue } from '../utils'

function isComma(token: TSESTree.Token): token is TSESTree.PunctuatorToken {
return token.type === 'Punctuator' && token.value === ','
Expand Down Expand Up @@ -34,10 +34,11 @@ function getImportText(
}

const names = specifiers.map(s => {
if (s.imported.name === s.local.name) {
return s.imported.name
const importedName = getValue(s.imported)
if (importedName === s.local.name) {
return importedName
}
return `${s.imported.name} as ${s.local.name}`
return `${importedName} as ${s.local.name}`
})
// insert a fresh top-level import
return `import ${kind} {${names.join(', ')}} from ${sourceString};`
Expand Down
10 changes: 3 additions & 7 deletions src/rules/no-default-export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRule } from '../utils'
import { createRule, getValue } from '../utils'
import sourceType from '../utils/source-type'

export = createRule({
Expand Down Expand Up @@ -37,11 +37,7 @@ export = createRule({

ExportNamedDeclaration(node) {
for (const specifier of node.specifiers.filter(
specifier =>
(specifier.exported.name ||
('value' in specifier.exported && specifier.exported.value)) ===
'default',
)) {
specifier => getValue(specifier.exported) === 'default')) {
const { loc } = sourceCode.getFirstTokens(node)[1] || {}
// @ts-expect-error - experimental parser type
if (specifier.type === 'ExportDefaultSpecifier') {
Expand All @@ -55,7 +51,7 @@ export = createRule({
node,
messageId: 'noAliasDefault',
data: {
local: specifier.local.name,
local: getValue(specifier.local),
},
loc,
})
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { TSESTree } from '@typescript-eslint/utils'
import type { Tag } from 'doctrine'

import type { ModuleNamespace } from '../utils'
import { ExportMap, createRule, declaredScope } from '../utils'
import { ExportMap, createRule, declaredScope, getValue } from '../utils'

function message(deprecation: Tag) {
return {
Expand Down Expand Up @@ -93,7 +93,7 @@ export = createRule({
}

case 'ImportSpecifier': {
imported = im.imported.name
imported = getValue(im.imported)
local = im.local.name
break
}
Expand Down
6 changes: 2 additions & 4 deletions src/rules/no-named-export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRule } from '../utils'
import { createRule, getValue } from '../utils'
import sourceType from '../utils/source-type'

export = createRule({
Expand Down Expand Up @@ -32,9 +32,7 @@ export = createRule({
}

const someNamed = node.specifiers.some(
specifier =>
(specifier.exported.name ||
('value' in specifier.exported && specifier.exported.value)) !==
specifier => getValue(specifier.exported) !==
'default',
)
if (someNamed) {
Expand Down
8 changes: 4 additions & 4 deletions src/rules/no-rename-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import path from 'node:path'

import type { TSESTree } from '@typescript-eslint/utils'

import { createRule, ExportMap } from '../utils'
import { createRule, ExportMap, getValue } from '../utils'
import type { ModuleOptions } from '../utils'

type Options = ModuleOptions & {
Expand Down Expand Up @@ -86,7 +86,7 @@ export = createRule<[Options?], MessageId>({
return
}
case 'ExportSpecifier': {
return targetNode.local.name
return getValue(targetNode.local)
}
case 'FunctionDeclaration': {
return targetNode.id?.name
Expand Down Expand Up @@ -171,7 +171,7 @@ export = createRule<[Options?], MessageId>({
return
}

if (node.imported.name !== 'default') {
if (getValue(node.imported) !== 'default') {
return
}

Expand Down Expand Up @@ -322,7 +322,7 @@ function getDefaultExportNode(
}
case 'ExportNamedDeclaration': {
return defaultExportNode.specifiers.find(
specifier => specifier.exported.name === 'default',
specifier => getValue(specifier.exported) === 'default',
)
}
default: {
Expand Down
5 changes: 2 additions & 3 deletions src/rules/prefer-default-export.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TSESTree } from '@typescript-eslint/utils'

import { createRule } from '../utils'
import { createRule, getValue } from '../utils'

type Options = {
target?: 'single' | 'any'
Expand Down Expand Up @@ -70,8 +70,7 @@ export = createRule<[Options?], MessageId>({

ExportSpecifier(node) {
if (
(node.exported.name ||
('value' in node.exported && node.exported.value)) === 'default'
getValue(node.exported) === 'default'
) {
hasDefaultExport = true
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/utils/export-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ export class ExportMap {
}
}

function addNamespace(object: object, identifier: TSESTree.Identifier) {
function addNamespace(object: object, identifier: TSESTree.Identifier | TSESTree.StringLiteral) {
const nsfn = getNamespace(getValue(identifier))
if (nsfn) {
Object.defineProperty(object, 'namespace', { get: nsfn })
Expand Down Expand Up @@ -308,7 +308,7 @@ export class ExportMap {
// else falls through
default: {
if ('local' in s) {
local = s.local.name
local = getValue(s.local)
} else {
throw new Error('Unknown export specifier type')
}
Expand All @@ -318,7 +318,7 @@ export class ExportMap {

if ('exported' in s) {
// todo: JSDoc
m.reexports.set(s.exported.name, {
m.reexports.set(getValue(s.exported), {
local,
getImport: () => resolveImport(nsource),
})
Expand Down

0 comments on commit 5d6cd07

Please sign in to comment.