-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
166 lines (149 loc) · 4.35 KB
/
Copy pathparser.js
File metadata and controls
166 lines (149 loc) · 4.35 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import fs from 'node:fs'
import path from 'node:path'
import crypto from 'node:crypto'
import { createRequire } from 'node:module'
import * as acorn from 'acorn'
import MagicString from 'magic-string'
import { UnsupportedSourceError, MissingPeerError, ReflectionError } from './errors.js'
import { resolveSourceFile } from './source-path.js'
const require = createRequire(import.meta.url)
let tsLib = null
function loadTypeScript() {
if (tsLib) return tsLib
try {
tsLib = require('typescript')
return tsLib
} catch (cause) {
throw new MissingPeerError(
'TypeScript support requires installing the "typescript" package as a peer dependency',
{ cause },
)
}
}
export function __resetTypeScriptCache() {
tsLib = null
}
const JS_EXTS = new Set(['.js', '.mjs', '.cjs'])
const TS_EXTS = new Set(['.ts', '.tsx', '.mts', '.cts'])
const FEATURE_EXTS = new Set(['.feature'])
function detectEngine(filePath) {
const ext = path.extname(filePath).toLowerCase()
if (JS_EXTS.has(ext)) return 'acorn'
if (TS_EXTS.has(ext)) return 'typescript'
if (FEATURE_EXTS.has(ext)) return 'gherkin'
return null
}
function stripBOM(str) {
if (str.charCodeAt(0) === 0xfeff) return str.slice(1)
return str
}
function detectEOL(source) {
const firstLF = source.indexOf('\n')
if (firstLF === -1) return '\n'
return source[firstLF - 1] === '\r' ? '\r\n' : '\n'
}
export class Parser {
constructor() {
this.cache = new Map()
}
parseFile(filePath) {
const resolved = resolveSourceFile(filePath)
const cached = this.cache.get(resolved)
let stat
try {
stat = fs.statSync(resolved)
} catch (cause) {
throw new ReflectionError(`Cannot stat source file: ${resolved}`, { filePath: resolved, cause })
}
if (cached && cached.mtimeMs === stat.mtimeMs && cached.size === stat.size) {
return cached
}
const engine = detectEngine(resolved)
if (engine === null) {
throw new UnsupportedSourceError(
`Unsupported source file extension: ${path.extname(resolved) || '(none)'}`,
{ filePath: resolved },
)
}
if (engine === 'gherkin') {
throw new UnsupportedSourceError(
'Gherkin .feature files are not supported. Reflect the step definition file instead.',
{ filePath: resolved },
)
}
const raw = fs.readFileSync(resolved, 'utf8')
const hasBOM = raw.charCodeAt(0) === 0xfeff
const source = stripBOM(raw)
const hash = crypto.createHash('sha1').update(raw).digest('hex')
const eol = detectEOL(source)
let ast
if (engine === 'acorn') {
try {
ast = acorn.parse(source, {
ecmaVersion: 'latest',
sourceType: 'unambiguous',
locations: true,
ranges: true,
allowHashBang: true,
allowAwaitOutsideFunction: true,
allowReturnOutsideFunction: true,
allowImportExportEverywhere: true,
})
} catch (cause) {
throw new ReflectionError(`Failed to parse JavaScript file: ${resolved} — ${cause.message}`, {
filePath: resolved,
cause,
})
}
} else {
const ts = loadTypeScript()
const ext = path.extname(resolved).toLowerCase()
const kind = ext === '.tsx' ? ts.ScriptKind.TSX : ts.ScriptKind.TS
try {
ast = ts.createSourceFile(resolved, source, ts.ScriptTarget.Latest, true, kind)
} catch (cause) {
throw new ReflectionError(`Failed to parse TypeScript file: ${resolved} — ${cause.message}`, {
filePath: resolved,
cause,
})
}
}
const magicString = new MagicString(source)
const entry = {
filePath: resolved,
originalPath: filePath,
source,
rawSource: raw,
ast,
magicString,
engine,
hash,
mtimeMs: stat.mtimeMs,
size: stat.size,
hasBOM,
eol,
}
this.cache.set(resolved, entry)
return entry
}
invalidate(filePath) {
const resolved = resolveSourceFile(filePath)
this.cache.delete(resolved)
}
clear() {
this.cache.clear()
}
}
const defaultParser = new Parser()
export function parseFile(filePath) {
return defaultParser.parseFile(filePath)
}
export function invalidateFile(filePath) {
defaultParser.invalidate(filePath)
}
export function clearCache() {
defaultParser.clear()
}
export function getDefaultParser() {
return defaultParser
}