-
Notifications
You must be signed in to change notification settings - Fork 45
#377 - Automatically support test
and expect
aliases with variable references
#386
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { Rule } from 'eslint' | ||
import { describe, expect, it } from 'vitest' | ||
import { getImportedAliases } from './ast.js' | ||
|
||
describe('getImportedAliases', () => { | ||
it('supports ImportSpecifier.imported as Literal', () => { | ||
// Intentionally construct a nonstandard/synthetic AST: in valid JS parsed by | ||
// standard ESTree parsers, `ImportSpecifier.imported` is an Identifier. | ||
// Here we use a Literal ('test') to ensure `getImportedAliases` gracefully | ||
// handles such shapes and still treats `import { test as it }` as aliasing. | ||
const program: any = { | ||
body: [ | ||
{ | ||
source: { | ||
raw: "'@playwright/test'", | ||
type: 'Literal', | ||
value: '@playwright/test', | ||
}, | ||
specifiers: [ | ||
{ | ||
imported: { raw: "'test'", type: 'Literal', value: 'test' }, | ||
local: { name: 'it', type: 'Identifier' }, | ||
type: 'ImportSpecifier', | ||
}, | ||
], | ||
type: 'ImportDeclaration', | ||
}, | ||
], | ||
sourceType: 'module', | ||
type: 'Program', | ||
} | ||
|
||
const context = { | ||
sourceCode: { ast: program }, | ||
} as unknown as Rule.RuleContext | ||
const aliases = getImportedAliases(context, 'test') | ||
expect(aliases).toEqual(['it']) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,3 +247,50 @@ export function dereference( | |
|
||
return expr?.right ?? decl?.init | ||
} | ||
|
||
/** | ||
* Returns local alias names imported from `@playwright/test` for a given named | ||
* import. | ||
* | ||
* For example, for `import { test as foo } from '@playwright/test'`, | ||
* `getImportedAliases(context, 'test')` will return `['foo']`. | ||
*/ | ||
export function getImportedAliases( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't tell the full story. What if for example you want to do this:
which is very common in Playwright. With this method, that wouldn't be caught. We shouldn't just use AST parsing, we should find any |
||
context: Rule.RuleContext, | ||
importedName: string, | ||
): string[] { | ||
const program = context.sourceCode.ast | ||
const aliases = new Set<string>() | ||
|
||
if (program.type !== 'Program') return [] | ||
|
||
for (const stmt of program.body) { | ||
if (stmt.type !== 'ImportDeclaration') continue | ||
if ( | ||
!isStringLiteral(stmt.source) || | ||
stmt.source.value !== '@playwright/test' | ||
) | ||
continue | ||
if ((stmt as any).importKind === 'type') continue | ||
|
||
for (const spec of stmt.specifiers) { | ||
if (spec.type !== 'ImportSpecifier') continue | ||
if ((spec as any).importKind === 'type') continue | ||
const importedNode = spec.imported as ESTree.Identifier | ESTree.Literal | ||
const imported = | ||
importedNode.type === 'Identifier' | ||
? importedNode.name | ||
: typeof importedNode.value === 'string' | ||
? importedNode.value | ||
: undefined | ||
if (imported !== importedName) continue | ||
|
||
const localName = spec.local.name | ||
if (localName !== imported) { | ||
aliases.add(localName) | ||
} | ||
} | ||
} | ||
|
||
return [...aliases] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test isn't really helpful, let's remove it.