check-lexer-functions.test.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var fs = require('fs');
  2. var acorn = require('acorn');
  3. var walk = require('acorn/dist/walk');
  4. var hadErrors = false;
  5. var lexerFunctions = {
  6. advance: true,
  7. append: true,
  8. attributesBlock: true,
  9. attrs: true,
  10. blank: true,
  11. block: true,
  12. blockCode: true,
  13. call: true,
  14. case: true,
  15. className: true,
  16. code: true,
  17. colon: true,
  18. comment: true,
  19. conditional: true,
  20. default: true,
  21. doctype: true,
  22. dot: true,
  23. each: true,
  24. eos: true,
  25. endInterpolation: true,
  26. extends: true,
  27. filter: true,
  28. id: true,
  29. include: true,
  30. indent: true,
  31. interpolation: true,
  32. isExpression: true,
  33. mixin: true,
  34. mixinBlock: true,
  35. path: true,
  36. pipelessText: true,
  37. prepend: true,
  38. slash: true,
  39. tag: true,
  40. text: true,
  41. textHtml: true,
  42. when: true,
  43. while: true,
  44. yield: true,
  45. };
  46. function checkDirectCalls (node) {
  47. var callee = node.callee;
  48. if (callee.type !== 'MemberExpression') return;
  49. if (callee.object.type !== 'ThisExpression') return;
  50. var property = callee.property;
  51. var func;
  52. if (callee.computed) {
  53. if (property.type !== 'Literal') return;
  54. func = property.value;
  55. } else {
  56. func = property.name;
  57. }
  58. if (!lexerFunctions[func]) return;
  59. console.log('index.js:' + node.loc.start.line + ':' + node.loc.start.column + ': Lexer function ' + func + ' called directly');
  60. hadErrors = true;
  61. }
  62. function checkMissingLexerFunction (node) {
  63. var callee = node.callee;
  64. if (callee.type !== 'MemberExpression') return;
  65. if (callee.object.type !== 'ThisExpression') return;
  66. var property = callee.property;
  67. var func;
  68. if (callee.computed) {
  69. if (property.type !== 'Literal') return;
  70. func = property.value;
  71. } else {
  72. func = property.name;
  73. }
  74. if (func !== 'callLexerFunction') return;
  75. if (!node.arguments.length) return;
  76. if (node.arguments[0].type !== 'Literal') return;
  77. func = node.arguments[0].value;
  78. if (lexerFunctions[func]) return;
  79. console.log('index.js:' + node.loc.start.line + ':' + node.loc.start.column + ': Lexer function ' + func + ' not in lexerFunctions list');
  80. hadErrors = true;
  81. }
  82. test('lexer functions', () => {
  83. var str = fs.readFileSync(__dirname + '/../index.js', 'utf8');
  84. var ast = acorn.parse(str, {locations: true});
  85. walk.simple(ast, {
  86. CallExpression: function (node) {
  87. checkDirectCalls(node);
  88. checkMissingLexerFunction(node);
  89. }
  90. });
  91. if (hadErrors) {
  92. throw new Error('Problem with lexer functions detected');
  93. }
  94. });