Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(type-utils): file variant of TypeOrValueSpecifier uses canonical filenames instead of lowercasing #6781

Merged
merged 4 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
13 changes: 7 additions & 6 deletions packages/type-utils/src/TypeOrValueSpecifier.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getCanonicalFileName } from '@typescript-eslint/typescript-estree';
Copy link
Member

Choose a reason for hiding this comment

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

@bradzacher just confirming on architecture things - is there a better dependency setup we can use here?

Copy link
Member

Choose a reason for hiding this comment

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

No this is probably the best way to do it and ensures we keep things in sync with the parser's logic.

import path from 'path';
import type * as ts from 'typescript';

Expand Down Expand Up @@ -133,16 +134,16 @@ function typeDeclaredInFile(
program: ts.Program,
): boolean {
if (relativePath === undefined) {
const cwd = program.getCurrentDirectory().toLowerCase();
const cwd = getCanonicalFileName(program.getCurrentDirectory());
return declarationFiles.some(declaration =>
declaration.fileName.toLowerCase().startsWith(cwd),
getCanonicalFileName(declaration.fileName).startsWith(cwd),
);
}
const absolutePath = path
.join(program.getCurrentDirectory(), relativePath)
.toLowerCase();
const absolutePath = getCanonicalFileName(
path.join(program.getCurrentDirectory(), relativePath),
);
return declarationFiles.some(
declaration => declaration.fileName.toLowerCase() === absolutePath,
declaration => getCanonicalFileName(declaration.fileName) === absolutePath,
);
}

Expand Down
92 changes: 57 additions & 35 deletions packages/type-utils/tests/TypeOrValueSpecifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,42 +176,64 @@ describe('TypeOrValueSpecifier', () => {
runTestNegative,
);

it.each<[string, TypeOrValueSpecifier]>(
it.each<[string, TypeOrValueSpecifier]>([
[
'interface Foo {prop: string}; type Test = Foo;',
{ from: 'file', name: 'Foo' },
],
[
'type Foo = {prop: string}; type Test = Foo;',
{ from: 'file', name: 'Foo' },
],
[
'interface Foo {prop: string}; type Test = Foo;',
{ from: 'file', name: ['Foo', 'Bar'] },
],
[
'type Foo = {prop: string}; type Test = Foo;',
{ from: 'file', name: ['Foo', 'Bar'] },
],
[
[
'interface Foo {prop: string}; type Test = Foo;',
'type Foo = {prop: string}; type Test = Foo;',
].map((test): [string, TypeOrValueSpecifier] => [
test,
{ from: 'file', name: 'Foo' },
]),
[
'interface Foo {prop: string}; type Test = Foo;',
'type Foo = {prop: string}; type Test = Foo;',
].map((test): [string, TypeOrValueSpecifier] => [
test,
{ from: 'file', name: ['Foo', 'Bar'] },
]),
[
'interface Foo {prop: string}; type Test = Foo;',
'type Foo = {prop: string}; type Test = Foo;',
].map((test): [string, TypeOrValueSpecifier] => [
test,
{ from: 'file', name: 'Foo', path: 'tests/fixtures/file.ts' },
]),
[
'interface Foo {prop: string}; type Test = Foo;',
'type Foo = {prop: string}; type Test = Foo;',
].map((test): [string, TypeOrValueSpecifier] => [
test,
{
from: 'file',
name: ['Foo', 'Bar'],
path: 'tests/fixtures/file.ts',
},
]),
].flat(),
)('matches a matching file specifier: %s', runTestPositive);
'interface Foo {prop: string}; type Test = Foo;',
{ from: 'file', name: 'Foo', path: 'tests/fixtures/file.ts' },
],
[
'type Foo = {prop: string}; type Test = Foo;',
{ from: 'file', name: 'Foo', path: 'tests/fixtures/file.ts' },
],
[
'interface Foo {prop: string}; type Test = Foo;',
{
from: 'file',
name: 'Foo',
path: 'tests/../tests/fixtures/////file.ts',
},
],
[
'type Foo = {prop: string}; type Test = Foo;',
{
from: 'file',
name: 'Foo',
path: 'tests/../tests/fixtures/////file.ts',
},
],
[
'interface Foo {prop: string}; type Test = Foo;',
{
from: 'file',
name: ['Foo', 'Bar'],
path: 'tests/fixtures/file.ts',
},
],
[
'type Foo = {prop: string}; type Test = Foo;',
{
from: 'file',
name: ['Foo', 'Bar'],
path: 'tests/fixtures/file.ts',
},
],
])('matches a matching file specifier: %s', runTestPositive);

it.each<[string, TypeOrValueSpecifier]>([
[
Expand Down
1 change: 1 addition & 0 deletions packages/typescript-estree/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export { simpleTraverse } from './simple-traverse';
export * from './ts-estree';
export { createProgramFromConfigFile as createProgram } from './create-program/useProvidedPrograms';
export * from './create-program/getScriptKind';
export { getCanonicalFileName } from './create-program/shared';
marekdedic marked this conversation as resolved.
Show resolved Hide resolved
export { typescriptVersionIsAtLeast } from './version-check';
export * from './getModifiers';
export { TSError } from './node-utils';
Expand Down