-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
149 lines (117 loc) · 3.6 KB
/
index.js
File metadata and controls
149 lines (117 loc) · 3.6 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
'use strict'
const isOptions = require('is-options')
const { pascalCase } = require('pascal-case')
const esprima = require('esprima')
const ATTRIBUTE_RE = /(^|\r?\n)\s*\[\s*assembly\s*:[^]+?\]/
module.exports = function (source, opts) {
if (isOptions(source)) {
opts = source
source = ''
} else if (source == null) {
source = ''
} else if (Buffer.isBuffer(source)) {
source = source.toString()
} else if (typeof source !== 'string') {
throw new TypeError('The source code, if provided, must be a string or buffer')
}
if (!opts) opts = {}
const attributes = {}
const chunks = []
const language = opts.language
let match
let remainder = source
let enableSuffix = language === 'jscript'
while ((match = remainder.match(ATTRIBUTE_RE)) !== null) {
const raw = match[0]
const expr = raw.trim()
// Keep indentation
const start = match.index + raw.indexOf('[')
const end = match.index + raw.length
chunks.push(remainder.slice(0, start))
chunks.push(expr)
const { key, value } = parseAttribute(expr)
remainder = remainder.slice(end)
addAttribute(key, value, chunks.length - 1)
}
function addAttribute (fqk, value, index) {
const isSuffixed = fqk.slice(-9) === 'Attribute'
const key = pascalCase(isSuffixed ? fqk.slice(0, -9) : fqk)
enableSuffix = enableSuffix || isSuffixed
fqk = enableSuffix ? key + 'Attribute' : key
return (attributes[key] = {
set (newValue) {
value = newValue
chunks[index] = `[assembly: ${fqk}(${literal(value)})]`
},
get key () { return key },
get value () { return value },
get source () { return chunks[index] }
})
}
if (remainder) {
chunks.push(remainder)
}
return {
get (key) {
key = pascalCase(key)
return attributes[key] ? attributes[key].value : null
},
set (key, value) {
key = pascalCase(key)
if (attributes[key]) {
attributes[key].set(value)
} else {
if (chunks.length && !/\n\s*$/.test(chunks[chunks.length - 1])) {
chunks.push('\n')
}
const index = chunks.push('') - 1
addAttribute(key, value, index).set(value)
}
},
attributes () {
const res = {}
for (const k in attributes) {
res[k] = attributes[k].value
}
return res
},
toSource (opts) {
if (!opts) opts = {}
let src = chunks.join('')
if (language === 'jscript' && opts.preamble !== false && src !== '') {
if (!/import\s+System\.Reflection/.test(src)) {
src = 'import System.Reflection;\n' + src
}
}
return src
}
}
}
function literal (value) {
const t = typeof value
if (t === 'boolean' || t === 'string' || t === 'number') {
return JSON.stringify(value)
} else {
throw new TypeError('Unsupported type: ' + t)
}
}
function parseAttribute (src) {
const js = '{' + src.slice(1, -1) + '}'
const body = esprima.parseScript(js).body
const stmt = body[0].body[0].body || {}
const { type, callee, arguments: args, name } = stmt.expression || {}
// E.g. [assembly: SuppressIldasm]
if (stmt.type === 'ExpressionStatement' && type === 'Identifier') {
return { key: name, value: null }
}
if (stmt.type !== 'ExpressionStatement' || type !== 'CallExpression') {
throw new Error('expected ExpressionStatement with CallExpression')
}
if (callee.type !== 'Identifier' || args.length !== 1 || args[0].type !== 'Literal') {
throw new Error('expected Identifier callee and one Literal argument')
}
return {
key: callee.name,
value: args[0].value
}
}