Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(type-utils): fixed TypeOrValueSpecifier not accounting for scoped DT packages#6780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 8 commits intotypescript-eslint:v6frommarekdedic:type-or-value-specifier-scoped-packages
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
8 commits
Select commitHold shift + click to select a range
31b4856
test(type-utils): using last node instead of first for TypeOrValueSpe…
marekdedicMar 27, 2023
0da275e
test(type-utils): added TypeOrValueSPecifier tests for package specif…
marekdedicMar 27, 2023
d68d428
test(type-utils): added TypeOrValueSPecifier tests for DT package
marekdedicMar 27, 2023
cffd1b2
test(type-utils): added a failing TypeOrValueSpecifier test for an or…
marekdedicMar 27, 2023
a29d60d
fix(type-utils): fixed TypeOrValueSpecifier not accounting for scoped…
marekdedicMar 27, 2023
bbd4662
fix(type-utils): fixed using String.prototype.replaceAll where it act…
marekdedicMar 27, 2023
d0b5e15
chore(type-utils) refactored scoped package name handling
marekdedicApr 17, 2023
e97628f
chore(type-utils) refactored scoped package matching to do only one p…
marekdedicApr 17, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletionspackages/type-utils/src/TypeOrValueSpecifier.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -146,6 +146,21 @@ function typeDeclaredInFile(
);
}

function typeDeclaredInPackage(
packageName: string,
declarationFiles: ts.SourceFile[],
): boolean {
// Handle scoped packages - if the name starts with @, remove it and replace / with __
const typesPackageName =
'@types/' + packageName.replace(/^@([^/]+)\//, '$1__');
const matcher = new RegExp(
`node_modules/(?:${packageName}|${typesPackageName})/`,
);
return declarationFiles.some(declaration =>
matcher.test(declaration.fileName),
);
}

export function typeMatchesSpecifier(
type: ts.Type,
specifier: TypeOrValueSpecifier,
Expand All@@ -170,12 +185,6 @@ export function typeMatchesSpecifier(
program.isSourceFileDefaultLibrary(declaration),
);
case 'package':
return declarationFiles.some(
declaration =>
declaration.fileName.includes(`node_modules/${specifier.package}/`) ||
declaration.fileName.includes(
`node_modules/@types/${specifier.package}/`,
),
);
return typeDeclaredInPackage(specifier.package, declarationFiles);
}
}
87 changes: 86 additions & 1 deletionpackages/type-utils/tests/TypeOrValueSpecifier.test.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -139,7 +139,8 @@ describe('TypeOrValueSpecifier', () => {
.program!.getTypeChecker()
.getTypeAtLocation(
services.esTreeNodeToTSNodeMap.get(
(ast.body[0] as TSESTree.TSTypeAliasDeclaration).id,
(ast.body[ast.body.length - 1] as TSESTree.TSTypeAliasDeclaration)
.id,
Comment on lines +142 to +143
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I actually think this fixes a minor issue that is probably present in more tests - I have taken this directly out of some other tests in this repo

),
);
expect(typeMatchesSpecifier(type, specifier, services.program!)).toBe(
Expand DownExpand Up@@ -232,6 +233,90 @@ describe('TypeOrValueSpecifier', () => {
['type Test = RegExp;', { from: 'lib', name: ['BigInt', 'Date'] }],
])("doesn't match a mismatched lib specifier: %s", runTestNegative);

it.each<[string, TypeOrValueSpecifier]>([
[
'import type {Node} from "typescript"; type Test = Node;',
{ from: 'package', name: 'Node', package: 'typescript' },
],
[
'import type {Node} from "typescript"; type Test = Node;',
{ from: 'package', name: ['Node', 'Symbol'], package: 'typescript' },
],
[
'import {Node} from "typescript"; type Test = Node;',
{ from: 'package', name: 'Node', package: 'typescript' },
],
[
'import {Node} from "typescript"; type Test = Node;',
{ from: 'package', name: ['Node', 'Symbol'], package: 'typescript' },
],
[
'import * as ts from "typescript"; type Test = ts.Node;',
{ from: 'package', name: 'Node', package: 'typescript' },
],
[
'import * as ts from "typescript"; type Test = ts.Node;',
{ from: 'package', name: ['Node', 'Symbol'], package: 'typescript' },
],
[
'import type * as ts from "typescript"; type Test = ts.Node;',
{ from: 'package', name: 'Node', package: 'typescript' },
],
[
'import type * as ts from "typescript"; type Test = ts.Node;',
{ from: 'package', name: ['Node', 'Symbol'], package: 'typescript' },
],
[
'import type {Node as TsNode} from "typescript"; type Test = TsNode;',
{ from: 'package', name: 'Node', package: 'typescript' },
],
[
'import type {Node as TsNode} from "typescript"; type Test = TsNode;',
{ from: 'package', name: ['Node', 'Symbol'], package: 'typescript' },
],
// The following type is available from the @types/semver package.
[
'import {SemVer} from "semver"; type Test = SemVer;',
{ from: 'package', name: 'SemVer', package: 'semver' },
],
// The following type is available from the scoped @types/babel__code-frame package.
[
'import {BabelCodeFrameOptions} from "@babel/code-frame"; type Test = BabelCodeFrameOptions;',
{
from: 'package',
name: 'BabelCodeFrameOptions',
package: '@babel/code-frame',
},
],
])('matches a matching package specifier: %s', runTestPositive);

it.each<[string, TypeOrValueSpecifier]>([
[
'import type {Node} from "typescript"; type Test = Node;',
{ from: 'package', name: 'Symbol', package: 'typescript' },
],
[
'import type {Node} from "typescript"; type Test = Node;',
{ from: 'package', name: ['Symbol', 'Checker'], package: 'typescript' },
],
[
'import type {Node} from "typescript"; type Test = Node;',
{ from: 'package', name: 'Node', package: 'other-package' },
],
[
'import type {Node} from "typescript"; type Test = Node;',
{ from: 'package', name: ['Node', 'Symbol'], package: 'other-package' },
],
[
'interface Node {prop: string}; type Test = Node;',
{ from: 'package', name: 'Node', package: 'typescript' },
],
[
'import type {Node as TsNode} from "typescript"; type Test = TsNode;',
{ from: 'package', name: 'TsNode', package: 'typescript' },
],
])("doesn't match a mismatched lib specifier: %s", runTestNegative);

it.each<[string, TypeOrValueSpecifier]>([
[
'interface Foo {prop: string}; type Test = Foo;',
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp