-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidentifier.ts
More file actions
56 lines (49 loc) · 1.71 KB
/
identifier.ts
File metadata and controls
56 lines (49 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import MagicString from 'magic-string'
import type { Node, IdentifierName } from 'oxc-parser'
import type { FormatterOptions, ExportsMeta } from '../types.js'
import { exportsRename } from '#utils/exports.js'
import { identifier as ident } from '#helpers/identifier.js'
type IdentifierArg = {
node: IdentifierName
ancestors: Node[]
code: MagicString
opts: FormatterOptions
meta: ExportsMeta
}
export const identifier = ({ node, ancestors, code, opts, meta }: IdentifierArg) => {
if (opts.target === 'module') {
const { start, end, name } = node
switch (name) {
case '__filename':
code.update(start, end, 'import.meta.url')
break
case '__dirname':
code.update(start, end, 'import.meta.dirname')
break
case 'exports':
{
const parent = ancestors[ancestors.length - 2]
if (opts.transformSyntax) {
if (parent.type === 'AssignmentExpression' && parent.left === node) {
// The code is reassigning `exports` to something else.
meta.hasExportsBeenReassigned = true
code.update(parent.left.start, parent.left.end, exportsRename)
}
if (
ident.isModuleScope(ancestors) &&
!ident.isFunctionExpressionId(ancestors) &&
!ident.isExportSpecifierAlias(ancestors) &&
!ident.isClassPropertyKey(ancestors) &&
!ident.isMethodDefinitionKey(ancestors) &&
!ident.isMemberKey(ancestors) &&
!ident.isPropertyKey(ancestors) &&
!ident.isIife(ancestors)
) {
code.update(start, end, exportsRename)
}
}
}
break
}
}
}