File size: 10,388 Bytes
bc20498 |
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseConfig = parseConfig;
const traverse_1 = require("../traverse");
const espree_1 = require("../parser/espree");
const eslint_scope_1 = require("eslint-scope");
const scope_1 = require("../scope");
function parseConfig(code) {
const espree = (0, espree_1.getEspree)();
const ast = espree.parse(code, {
range: true,
loc: true,
ecmaVersion: espree.latestEcmaVersion,
sourceType: "module",
});
// Set parent nodes.
(0, traverse_1.traverseNodes)(ast, {
enterNode(node, parent) {
node.parent = parent;
},
leaveNode() {
/* do nothing */
},
});
// Analyze scopes.
const scopeManager = (0, eslint_scope_1.analyze)(ast, {
ignoreEval: true,
nodejsScope: false,
ecmaVersion: espree.latestEcmaVersion,
sourceType: "module",
fallback: traverse_1.getFallbackKeys,
});
return parseAst(ast, scopeManager);
}
function parseAst(ast, scopeManager) {
const edd = ast.body.find((node) => node.type === "ExportDefaultDeclaration");
if (!edd)
return {};
const decl = edd.declaration;
if (decl.type === "ClassDeclaration" || decl.type === "FunctionDeclaration")
return {};
return parseSvelteConfigExpression(decl, scopeManager);
}
function parseSvelteConfigExpression(node, scopeManager) {
var _a, _b;
const evaluated = evaluateExpression(node, scopeManager);
if ((evaluated === null || evaluated === void 0 ? void 0 : evaluated.type) !== 1 /* EvaluatedType.object */)
return {};
const result = {};
// Returns only known properties.
const compilerOptions = evaluated.getProperty("compilerOptions");
if ((compilerOptions === null || compilerOptions === void 0 ? void 0 : compilerOptions.type) === 1 /* EvaluatedType.object */) {
result.compilerOptions = {};
const runes = (_a = compilerOptions.getProperty("runes")) === null || _a === void 0 ? void 0 : _a.getStatic();
if (runes) {
result.compilerOptions.runes = Boolean(runes.value);
}
}
const kit = evaluated.getProperty("kit");
if ((kit === null || kit === void 0 ? void 0 : kit.type) === 1 /* EvaluatedType.object */) {
result.kit = {};
const files = (_b = kit.getProperty("files")) === null || _b === void 0 ? void 0 : _b.getStatic();
if (files)
result.kit.files = files.value;
}
return result;
}
class EvaluatedLiteral {
constructor(value) {
this.type = 0 /* EvaluatedType.literal */;
this.value = value;
}
getStatic() {
return this;
}
}
/** Evaluating an object expression. */
class EvaluatedObject {
constructor(node, parseExpression) {
this.type = 1 /* EvaluatedType.object */;
this.cached = new Map();
this.node = node;
this.parseExpression = parseExpression;
}
/** Gets the evaluated value of the property with the given name. */
getProperty(key) {
return this.withCache(key, () => {
let unknown = false;
for (const prop of [...this.node.properties].reverse()) {
if (prop.type === "Property") {
const name = this.getKey(prop);
if (name === key)
return this.parseExpression(prop.value);
if (name == null)
unknown = true;
}
else if (prop.type === "SpreadElement") {
const evaluated = this.parseExpression(prop.argument);
if ((evaluated === null || evaluated === void 0 ? void 0 : evaluated.type) === 1 /* EvaluatedType.object */) {
const value = evaluated.getProperty(key);
if (value)
return value;
}
unknown = true;
}
}
return unknown ? null : new EvaluatedLiteral(undefined);
});
}
getStatic() {
var _a, _b;
const object = {};
for (const prop of this.node.properties) {
if (prop.type === "Property") {
const name = this.getKey(prop);
if (name == null)
return null;
const evaluated = (_a = this.withCache(name, () => this.parseExpression(prop.value))) === null || _a === void 0 ? void 0 : _a.getStatic();
if (!evaluated)
return null;
object[name] = evaluated.value;
}
else if (prop.type === "SpreadElement") {
const evaluated = (_b = this.parseExpression(prop.argument)) === null || _b === void 0 ? void 0 : _b.getStatic();
if (!evaluated)
return null;
Object.assign(object, evaluated.value);
}
}
return { value: object };
}
withCache(key, parse) {
if (this.cached.has(key))
return this.cached.get(key) || null;
const evaluated = parse();
this.cached.set(key, evaluated);
return evaluated;
}
getKey(node) {
var _a;
if (!node.computed && node.key.type === "Identifier")
return node.key.name;
const evaluatedKey = (_a = this.parseExpression(node.key)) === null || _a === void 0 ? void 0 : _a.getStatic();
if (evaluatedKey)
return String(evaluatedKey.value);
return null;
}
}
function evaluateExpression(node, scopeManager) {
const tracked = new Map();
return parseExpression(node);
function parseExpression(node) {
if (node.type === "Literal") {
return new EvaluatedLiteral(node.value);
}
if (node.type === "Identifier") {
return parseIdentifier(node);
}
if (node.type === "ObjectExpression") {
return new EvaluatedObject(node, parseExpression);
}
return null;
}
function parseIdentifier(node) {
const defs = getIdentifierDefinitions(node);
if (defs.length !== 1) {
if (defs.length === 0 && node.name === "undefined")
return new EvaluatedLiteral(undefined);
return null;
}
const def = defs[0];
if (def.type !== "Variable")
return null;
if (def.parent.kind !== "const" || !def.node.init)
return null;
const evaluated = parseExpression(def.node.init);
if (!evaluated)
return null;
const assigns = parsePatternAssign(def.name, def.node.id);
let result = evaluated;
while (assigns.length) {
const assign = assigns.shift();
if (assign.type === "member") {
if (result.type !== 1 /* EvaluatedType.object */)
return null;
const next = result.getProperty(assign.name);
if (!next)
return null;
result = next;
}
else if (assign.type === "assignment") {
if (result.type === 0 /* EvaluatedType.literal */ &&
result.value === undefined) {
const next = parseExpression(assign.node.right);
if (!next)
return null;
result = next;
}
}
}
return result;
}
function getIdentifierDefinitions(node) {
var _a, _b;
if (tracked.has(node))
return tracked.get(node);
tracked.set(node, []);
const defs = (_a = (0, scope_1.findVariable)(scopeManager, node)) === null || _a === void 0 ? void 0 : _a.defs;
if (!defs)
return [];
tracked.set(node, defs);
if (defs.length !== 1) {
const def = defs[0];
if (def.type === "Variable" &&
def.parent.kind === "const" &&
def.node.id.type === "Identifier" &&
((_b = def.node.init) === null || _b === void 0 ? void 0 : _b.type) === "Identifier") {
const newDef = getIdentifierDefinitions(def.node.init);
tracked.set(node, newDef);
return newDef;
}
}
return defs;
}
}
/**
* Returns the assignment path.
* For example,
* `let {a: {target}} = {}`
* -> `[{type: "member", name: 'a'}, {type: "member", name: 'target'}]`.
* `let {a: {target} = foo} = {}`
* -> `[{type: "member", name: 'a'}, {type: "assignment"}, {type: "member", name: 'target'}]`.
*/
function parsePatternAssign(node, root) {
return parse(root) || [];
function parse(target) {
if (node === target) {
return [];
}
if (target.type === "Identifier") {
return null;
}
if (target.type === "AssignmentPattern") {
const left = parse(target.left);
if (!left)
return null;
return [{ type: "assignment", node: target }, ...left];
}
if (target.type === "ObjectPattern") {
for (const prop of target.properties) {
if (prop.type === "Property") {
const name = !prop.computed && prop.key.type === "Identifier"
? prop.key.name
: prop.key.type === "Literal"
? String(prop.key.value)
: null;
if (!name)
continue;
const value = parse(prop.value);
if (!value)
return null;
return [{ type: "member", name }, ...value];
}
}
return null;
}
if (target.type === "ArrayPattern") {
for (const [index, element] of target.elements.entries()) {
if (!element)
continue;
const value = parse(element);
if (!value)
return null;
return [{ type: "member", name: String(index) }, ...value];
}
return null;
}
return null;
}
}
|