Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

referencer.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright
  6. notice, this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright
  8. notice, this list of conditions and the following disclaimer in the
  9. documentation and/or other materials provided with the distribution.
  10. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  11. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  12. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  13. ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  14. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  15. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  16. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  17. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  18. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  19. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  20. */
  21. "use strict";
  22. /* eslint-disable no-underscore-dangle */
  23. /* eslint-disable no-undefined */
  24. const Syntax = require("estraverse").Syntax;
  25. const esrecurse = require("esrecurse");
  26. const Reference = require("./reference");
  27. const Variable = require("./variable");
  28. const PatternVisitor = require("./pattern-visitor");
  29. const definition = require("./definition");
  30. const assert = require("assert");
  31. const ParameterDefinition = definition.ParameterDefinition;
  32. const Definition = definition.Definition;
  33. /**
  34. * Traverse identifier in pattern
  35. * @param {Object} options - options
  36. * @param {pattern} rootPattern - root pattern
  37. * @param {Refencer} referencer - referencer
  38. * @param {callback} callback - callback
  39. * @returns {void}
  40. */
  41. function traverseIdentifierInPattern(options, rootPattern, referencer, callback) {
  42. // Call the callback at left hand identifier nodes, and Collect right hand nodes.
  43. const visitor = new PatternVisitor(options, rootPattern, callback);
  44. visitor.visit(rootPattern);
  45. // Process the right hand nodes recursively.
  46. if (referencer !== null && referencer !== undefined) {
  47. visitor.rightHandNodes.forEach(referencer.visit, referencer);
  48. }
  49. }
  50. // Importing ImportDeclaration.
  51. // http://people.mozilla.org/~jorendorff/es6-draft.html#sec-moduledeclarationinstantiation
  52. // https://github.com/estree/estree/blob/master/es6.md#importdeclaration
  53. // FIXME: Now, we don't create module environment, because the context is
  54. // implementation dependent.
  55. class Importer extends esrecurse.Visitor {
  56. constructor(declaration, referencer) {
  57. super(null, referencer.options);
  58. this.declaration = declaration;
  59. this.referencer = referencer;
  60. }
  61. visitImport(id, specifier) {
  62. this.referencer.visitPattern(id, pattern => {
  63. this.referencer.currentScope().__define(pattern,
  64. new Definition(
  65. Variable.ImportBinding,
  66. pattern,
  67. specifier,
  68. this.declaration,
  69. null,
  70. null
  71. ));
  72. });
  73. }
  74. ImportNamespaceSpecifier(node) {
  75. const local = (node.local || node.id);
  76. if (local) {
  77. this.visitImport(local, node);
  78. }
  79. }
  80. ImportDefaultSpecifier(node) {
  81. const local = (node.local || node.id);
  82. this.visitImport(local, node);
  83. }
  84. ImportSpecifier(node) {
  85. const local = (node.local || node.id);
  86. if (node.name) {
  87. this.visitImport(node.name, node);
  88. } else {
  89. this.visitImport(local, node);
  90. }
  91. }
  92. }
  93. // Referencing variables and creating bindings.
  94. class Referencer extends esrecurse.Visitor {
  95. constructor(options, scopeManager) {
  96. super(null, options);
  97. this.options = options;
  98. this.scopeManager = scopeManager;
  99. this.parent = null;
  100. this.isInnerMethodDefinition = false;
  101. }
  102. currentScope() {
  103. return this.scopeManager.__currentScope;
  104. }
  105. close(node) {
  106. while (this.currentScope() && node === this.currentScope().block) {
  107. this.scopeManager.__currentScope = this.currentScope().__close(this.scopeManager);
  108. }
  109. }
  110. pushInnerMethodDefinition(isInnerMethodDefinition) {
  111. const previous = this.isInnerMethodDefinition;
  112. this.isInnerMethodDefinition = isInnerMethodDefinition;
  113. return previous;
  114. }
  115. popInnerMethodDefinition(isInnerMethodDefinition) {
  116. this.isInnerMethodDefinition = isInnerMethodDefinition;
  117. }
  118. referencingDefaultValue(pattern, assignments, maybeImplicitGlobal, init) {
  119. const scope = this.currentScope();
  120. assignments.forEach(assignment => {
  121. scope.__referencing(
  122. pattern,
  123. Reference.WRITE,
  124. assignment.right,
  125. maybeImplicitGlobal,
  126. pattern !== assignment.left,
  127. init
  128. );
  129. });
  130. }
  131. visitPattern(node, options, callback) {
  132. let visitPatternOptions = options;
  133. let visitPatternCallback = callback;
  134. if (typeof options === "function") {
  135. visitPatternCallback = options;
  136. visitPatternOptions = { processRightHandNodes: false };
  137. }
  138. traverseIdentifierInPattern(
  139. this.options,
  140. node,
  141. visitPatternOptions.processRightHandNodes ? this : null,
  142. visitPatternCallback
  143. );
  144. }
  145. visitFunction(node) {
  146. let i, iz;
  147. // FunctionDeclaration name is defined in upper scope
  148. // NOTE: Not referring variableScope. It is intended.
  149. // Since
  150. // in ES5, FunctionDeclaration should be in FunctionBody.
  151. // in ES6, FunctionDeclaration should be block scoped.
  152. if (node.type === Syntax.FunctionDeclaration) {
  153. // id is defined in upper scope
  154. this.currentScope().__define(node.id,
  155. new Definition(
  156. Variable.FunctionName,
  157. node.id,
  158. node,
  159. null,
  160. null,
  161. null
  162. ));
  163. }
  164. // FunctionExpression with name creates its special scope;
  165. // FunctionExpressionNameScope.
  166. if (node.type === Syntax.FunctionExpression && node.id) {
  167. this.scopeManager.__nestFunctionExpressionNameScope(node);
  168. }
  169. // Consider this function is in the MethodDefinition.
  170. this.scopeManager.__nestFunctionScope(node, this.isInnerMethodDefinition);
  171. const that = this;
  172. /**
  173. * Visit pattern callback
  174. * @param {pattern} pattern - pattern
  175. * @param {Object} info - info
  176. * @returns {void}
  177. */
  178. function visitPatternCallback(pattern, info) {
  179. that.currentScope().__define(pattern,
  180. new ParameterDefinition(
  181. pattern,
  182. node,
  183. i,
  184. info.rest
  185. ));
  186. that.referencingDefaultValue(pattern, info.assignments, null, true);
  187. }
  188. // Process parameter declarations.
  189. for (i = 0, iz = node.params.length; i < iz; ++i) {
  190. this.visitPattern(node.params[i], { processRightHandNodes: true }, visitPatternCallback);
  191. }
  192. // if there's a rest argument, add that
  193. if (node.rest) {
  194. this.visitPattern({
  195. type: "RestElement",
  196. argument: node.rest
  197. }, pattern => {
  198. this.currentScope().__define(pattern,
  199. new ParameterDefinition(
  200. pattern,
  201. node,
  202. node.params.length,
  203. true
  204. ));
  205. });
  206. }
  207. // In TypeScript there are a number of function-like constructs which have no body,
  208. // so check it exists before traversing
  209. if (node.body) {
  210. // Skip BlockStatement to prevent creating BlockStatement scope.
  211. if (node.body.type === Syntax.BlockStatement) {
  212. this.visitChildren(node.body);
  213. } else {
  214. this.visit(node.body);
  215. }
  216. }
  217. this.close(node);
  218. }
  219. visitClass(node) {
  220. if (node.type === Syntax.ClassDeclaration) {
  221. this.currentScope().__define(node.id,
  222. new Definition(
  223. Variable.ClassName,
  224. node.id,
  225. node,
  226. null,
  227. null,
  228. null
  229. ));
  230. }
  231. this.visit(node.superClass);
  232. this.scopeManager.__nestClassScope(node);
  233. if (node.id) {
  234. this.currentScope().__define(node.id,
  235. new Definition(
  236. Variable.ClassName,
  237. node.id,
  238. node
  239. ));
  240. }
  241. this.visit(node.body);
  242. this.close(node);
  243. }
  244. visitProperty(node) {
  245. let previous;
  246. if (node.computed) {
  247. this.visit(node.key);
  248. }
  249. const isMethodDefinition = node.type === Syntax.MethodDefinition;
  250. if (isMethodDefinition) {
  251. previous = this.pushInnerMethodDefinition(true);
  252. }
  253. this.visit(node.value);
  254. if (isMethodDefinition) {
  255. this.popInnerMethodDefinition(previous);
  256. }
  257. }
  258. visitForIn(node) {
  259. if (node.left.type === Syntax.VariableDeclaration && node.left.kind !== "var") {
  260. this.scopeManager.__nestForScope(node);
  261. }
  262. if (node.left.type === Syntax.VariableDeclaration) {
  263. this.visit(node.left);
  264. this.visitPattern(node.left.declarations[0].id, pattern => {
  265. this.currentScope().__referencing(pattern, Reference.WRITE, node.right, null, true, true);
  266. });
  267. } else {
  268. this.visitPattern(node.left, { processRightHandNodes: true }, (pattern, info) => {
  269. let maybeImplicitGlobal = null;
  270. if (!this.currentScope().isStrict) {
  271. maybeImplicitGlobal = {
  272. pattern,
  273. node
  274. };
  275. }
  276. this.referencingDefaultValue(pattern, info.assignments, maybeImplicitGlobal, false);
  277. this.currentScope().__referencing(pattern, Reference.WRITE, node.right, maybeImplicitGlobal, true, false);
  278. });
  279. }
  280. this.visit(node.right);
  281. this.visit(node.body);
  282. this.close(node);
  283. }
  284. visitVariableDeclaration(variableTargetScope, type, node, index) {
  285. const decl = node.declarations[index];
  286. const init = decl.init;
  287. this.visitPattern(decl.id, { processRightHandNodes: true }, (pattern, info) => {
  288. variableTargetScope.__define(
  289. pattern,
  290. new Definition(
  291. type,
  292. pattern,
  293. decl,
  294. node,
  295. index,
  296. node.kind
  297. )
  298. );
  299. this.referencingDefaultValue(pattern, info.assignments, null, true);
  300. if (init) {
  301. this.currentScope().__referencing(pattern, Reference.WRITE, init, null, !info.topLevel, true);
  302. }
  303. });
  304. }
  305. AssignmentExpression(node) {
  306. if (PatternVisitor.isPattern(node.left)) {
  307. if (node.operator === "=") {
  308. this.visitPattern(node.left, { processRightHandNodes: true }, (pattern, info) => {
  309. let maybeImplicitGlobal = null;
  310. if (!this.currentScope().isStrict) {
  311. maybeImplicitGlobal = {
  312. pattern,
  313. node
  314. };
  315. }
  316. this.referencingDefaultValue(pattern, info.assignments, maybeImplicitGlobal, false);
  317. this.currentScope().__referencing(pattern, Reference.WRITE, node.right, maybeImplicitGlobal, !info.topLevel, false);
  318. });
  319. } else {
  320. this.currentScope().__referencing(node.left, Reference.RW, node.right);
  321. }
  322. } else {
  323. this.visit(node.left);
  324. }
  325. this.visit(node.right);
  326. }
  327. CatchClause(node) {
  328. this.scopeManager.__nestCatchScope(node);
  329. this.visitPattern(node.param, { processRightHandNodes: true }, (pattern, info) => {
  330. this.currentScope().__define(pattern,
  331. new Definition(
  332. Variable.CatchClause,
  333. node.param,
  334. node,
  335. null,
  336. null,
  337. null
  338. ));
  339. this.referencingDefaultValue(pattern, info.assignments, null, true);
  340. });
  341. this.visit(node.body);
  342. this.close(node);
  343. }
  344. Program(node) {
  345. this.scopeManager.__nestGlobalScope(node);
  346. if (this.scopeManager.__isNodejsScope()) {
  347. // Force strictness of GlobalScope to false when using node.js scope.
  348. this.currentScope().isStrict = false;
  349. this.scopeManager.__nestFunctionScope(node, false);
  350. }
  351. if (this.scopeManager.__isES6() && this.scopeManager.isModule()) {
  352. this.scopeManager.__nestModuleScope(node);
  353. }
  354. if (this.scopeManager.isStrictModeSupported() && this.scopeManager.isImpliedStrict()) {
  355. this.currentScope().isStrict = true;
  356. }
  357. this.visitChildren(node);
  358. this.close(node);
  359. }
  360. Identifier(node) {
  361. this.currentScope().__referencing(node);
  362. }
  363. UpdateExpression(node) {
  364. if (PatternVisitor.isPattern(node.argument)) {
  365. this.currentScope().__referencing(node.argument, Reference.RW, null);
  366. } else {
  367. this.visitChildren(node);
  368. }
  369. }
  370. MemberExpression(node) {
  371. this.visit(node.object);
  372. if (node.computed) {
  373. this.visit(node.property);
  374. }
  375. }
  376. Property(node) {
  377. this.visitProperty(node);
  378. }
  379. MethodDefinition(node) {
  380. this.visitProperty(node);
  381. }
  382. BreakStatement() {} // eslint-disable-line class-methods-use-this
  383. ContinueStatement() {} // eslint-disable-line class-methods-use-this
  384. LabeledStatement(node) {
  385. this.visit(node.body);
  386. }
  387. ForStatement(node) {
  388. // Create ForStatement declaration.
  389. // NOTE: In ES6, ForStatement dynamically generates
  390. // per iteration environment. However, escope is
  391. // a static analyzer, we only generate one scope for ForStatement.
  392. if (node.init && node.init.type === Syntax.VariableDeclaration && node.init.kind !== "var") {
  393. this.scopeManager.__nestForScope(node);
  394. }
  395. this.visitChildren(node);
  396. this.close(node);
  397. }
  398. ClassExpression(node) {
  399. this.visitClass(node);
  400. }
  401. ClassDeclaration(node) {
  402. this.visitClass(node);
  403. }
  404. CallExpression(node) {
  405. // Check this is direct call to eval
  406. if (!this.scopeManager.__ignoreEval() && node.callee.type === Syntax.Identifier && node.callee.name === "eval") {
  407. // NOTE: This should be `variableScope`. Since direct eval call always creates Lexical environment and
  408. // let / const should be enclosed into it. Only VariableDeclaration affects on the caller's environment.
  409. this.currentScope().variableScope.__detectEval();
  410. }
  411. this.visitChildren(node);
  412. }
  413. BlockStatement(node) {
  414. if (this.scopeManager.__isES6()) {
  415. this.scopeManager.__nestBlockScope(node);
  416. }
  417. this.visitChildren(node);
  418. this.close(node);
  419. }
  420. ThisExpression() {
  421. this.currentScope().variableScope.__detectThis();
  422. }
  423. WithStatement(node) {
  424. this.visit(node.object);
  425. // Then nest scope for WithStatement.
  426. this.scopeManager.__nestWithScope(node);
  427. this.visit(node.body);
  428. this.close(node);
  429. }
  430. VariableDeclaration(node) {
  431. const variableTargetScope = (node.kind === "var") ? this.currentScope().variableScope : this.currentScope();
  432. for (let i = 0, iz = node.declarations.length; i < iz; ++i) {
  433. const decl = node.declarations[i];
  434. this.visitVariableDeclaration(variableTargetScope, Variable.Variable, node, i);
  435. if (decl.init) {
  436. this.visit(decl.init);
  437. }
  438. }
  439. }
  440. // sec 13.11.8
  441. SwitchStatement(node) {
  442. this.visit(node.discriminant);
  443. if (this.scopeManager.__isES6()) {
  444. this.scopeManager.__nestSwitchScope(node);
  445. }
  446. for (let i = 0, iz = node.cases.length; i < iz; ++i) {
  447. this.visit(node.cases[i]);
  448. }
  449. this.close(node);
  450. }
  451. FunctionDeclaration(node) {
  452. this.visitFunction(node);
  453. }
  454. FunctionExpression(node) {
  455. this.visitFunction(node);
  456. }
  457. ForOfStatement(node) {
  458. this.visitForIn(node);
  459. }
  460. ForInStatement(node) {
  461. this.visitForIn(node);
  462. }
  463. ArrowFunctionExpression(node) {
  464. this.visitFunction(node);
  465. }
  466. ImportDeclaration(node) {
  467. assert(this.scopeManager.__isES6() && this.scopeManager.isModule(), "ImportDeclaration should appear when the mode is ES6 and in the module context.");
  468. const importer = new Importer(node, this);
  469. importer.visit(node);
  470. }
  471. visitExportDeclaration(node) {
  472. if (node.source) {
  473. return;
  474. }
  475. if (node.declaration) {
  476. this.visit(node.declaration);
  477. return;
  478. }
  479. this.visitChildren(node);
  480. }
  481. // TODO: ExportDeclaration doesn't exist. for bc?
  482. ExportDeclaration(node) {
  483. this.visitExportDeclaration(node);
  484. }
  485. ExportAllDeclaration(node) {
  486. this.visitExportDeclaration(node);
  487. }
  488. ExportDefaultDeclaration(node) {
  489. this.visitExportDeclaration(node);
  490. }
  491. ExportNamedDeclaration(node) {
  492. this.visitExportDeclaration(node);
  493. }
  494. ExportSpecifier(node) {
  495. // TODO: `node.id` doesn't exist. for bc?
  496. const local = (node.id || node.local);
  497. this.visit(local);
  498. }
  499. MetaProperty() { // eslint-disable-line class-methods-use-this
  500. // do nothing.
  501. }
  502. }
  503. module.exports = Referencer;
  504. /* vim: set sw=4 ts=4 et tw=80 : */