Skip to content

Commit

Permalink
chore: make eslint happy
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Oct 30, 2024
1 parent 5652174 commit 630dbce
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 35 deletions.
17 changes: 10 additions & 7 deletions src/rules/export.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils'
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
import type { TSESTree } from '@typescript-eslint/utils'

import {
ExportMap,
recursivePatternCapture,
createRule,
getValue,
} from '../utils'
import type { type } from 'node:os';

/*
Notes on TypeScript namespaces aka TSModuleDeclaration:
Expand Down Expand Up @@ -40,12 +40,15 @@ const tsTypePrefix = 'type:'
* ```
*/
function removeTypescriptFunctionOverloads(nodes: Set<TSESTree.Node>) {
nodes.forEach((node) => {
const declType = node.type === AST_NODE_TYPES.ExportDefaultDeclaration ? node.declaration.type : node.parent?.type;
for (const node of nodes) {
const declType =
node.type === AST_NODE_TYPES.ExportDefaultDeclaration
? node.declaration.type
: node.parent?.type
if (declType === AST_NODE_TYPES.TSDeclareFunction) {
nodes.delete(node);
nodes.delete(node)
}
});
}
}

/**
Expand Down Expand Up @@ -266,7 +269,7 @@ export = createRule<[], MessageId>({
continue
}

removeTypescriptFunctionOverloads(nodes);
removeTypescriptFunctionOverloads(nodes)

if (nodes.size <= 1) {
continue
Expand Down
28 changes: 14 additions & 14 deletions src/utils/export-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class ExportMap {
// If the visitor keys were not populated, then we shouldn't save anything to the cache,
// since the parse results may not be reliable.
if (exportMap.visitorKeys) {
exportCache.set(cacheKey, exportMap);
exportCache.set(cacheKey, exportMap)
}

return exportMap
Expand All @@ -153,7 +153,7 @@ export class ExportMap {
let ast: TSESTree.Program
let visitorKeys: TSESLint.SourceCode.VisitorKeys | null
try {
; ({ ast, visitorKeys } = parse(filepath, content, context))
;({ ast, visitorKeys } = parse(filepath, content, context))
} catch (error) {
m.errors.push(error as ParseError)
return m // can't continue
Expand Down Expand Up @@ -532,17 +532,17 @@ export class ExportMap {
const exportedName =
n.type === 'TSNamespaceExportDeclaration'
? (
n.id ||
// @ts-expect-error - legacy parser type
n.name
).name
n.id ||
// @ts-expect-error - legacy parser type
n.name
).name
: ('expression' in n &&
n.expression &&
(('name' in n.expression && n.expression.name) ||
('id' in n.expression &&
n.expression.id &&
n.expression.id.name))) ||
null
n.expression &&
(('name' in n.expression && n.expression.name) ||
('id' in n.expression &&
n.expression.id &&
n.expression.id.name))) ||
null

const getRoot = (
node: TSESTree.TSQualifiedName,
Expand All @@ -561,7 +561,7 @@ export class ExportMap {
('name' in node.id
? node.id.name === exportedName
: 'left' in node.id &&
getRoot(node.id).name === exportedName)) ||
getRoot(node.id).name === exportedName)) ||
('declarations' in node &&
node.declarations.find(
d => 'name' in d.id && d.id.name === exportedName,
Expand Down Expand Up @@ -730,7 +730,7 @@ export class ExportMap {

declare doc: Annotation | undefined

constructor(public path: string) { }
constructor(public path: string) {}

get hasDefault() {
return this.get('default') != null
Expand Down
8 changes: 6 additions & 2 deletions src/utils/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function withoutProjectParserOptions(
const log = debug('eslint-plugin-import-x:parse')

function keysFromParser(
parserPath: string | TSESLint.Parser.ParserModule,
_parserPath: string | TSESLint.Parser.ParserModule,
parserInstance: TSESLint.Parser.ParserModule,
parsedResult?: TSESLint.Parser.ParseResult,
) {
Expand All @@ -40,7 +40,11 @@ function keysFromParser(

// The espree parser doesn't have the `parseForESLint` function, so we don't ended up with a
// `parsedResult` here, but it does expose the visitor keys on the parser instance that we can use.
if (parserInstance && 'VisitorKeys' in parserInstance && parserInstance.VisitorKeys) {
if (
parserInstance &&
'VisitorKeys' in parserInstance &&
parserInstance.VisitorKeys
) {
return parserInstance.VisitorKeys as TSESLint.SourceCode.VisitorKeys
}
return null
Expand Down
11 changes: 4 additions & 7 deletions test/rules/export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ ruleTester.run('export', rule, {
export default function foo(param: string, param1?: number): boolean {
return param && param1;
}
`
}
`,
},
],

invalid: [
Expand Down Expand Up @@ -175,11 +175,8 @@ ruleTester.run('export', rule, {
export default function a() {}
export { x as default };
`,
errors: [
'Multiple default exports.',
'Multiple default exports.',
],
})
errors: ['Multiple default exports.', 'Multiple default exports.'],
}),
],
})

Expand Down
2 changes: 1 addition & 1 deletion test/rules/no-default-export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ruleTester.run('no-default-export', rule, {
parserOptions: {
sourceType: 'script',
},
}
},
}),
test({
code: 'module.exports = function foo() {}',
Expand Down
2 changes: 1 addition & 1 deletion test/rules/no-named-export.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ruleTester.run('no-named-export', rule, {
parserOptions: {
sourceType: 'script',
},
}
},
}),
test({
code: 'export default function bar() {};',
Expand Down
8 changes: 5 additions & 3 deletions test/utils/export-map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ describe('ExportMap', () => {
const mockContext = {
...fakeContext,
parserPath: 'not-real',
};
expect(ExportMap.get('./named-exports', mockContext)).toBeDefined();
expect(ExportMap.get('./named-exports', mockContext)).not.toBe(ExportMap.get('./named-exports', mockContext));
}
expect(ExportMap.get('./named-exports', mockContext)).toBeDefined()
expect(ExportMap.get('./named-exports', mockContext)).not.toBe(
ExportMap.get('./named-exports', mockContext),
)
})

it('does not return a cached copy after modification', done => {
Expand Down

0 comments on commit 630dbce

Please sign in to comment.