estools / esquery

ECMAScript AST query library.
http://estools.github.io/esquery/
Other
825 stars 91 forks source link

fix generate matcher #136

Closed binsee closed 1 year ago

binsee commented 1 year ago

Fixes #135

In 1.4.1, when optimizing generateMatcher, the check of parameters was missed, which resulted in the problem of reading prototype errors.


  function generateMatcher(selector) {
    switch (selector.type) {
      case 'wildcard':
        return function () {
          return true;
        };
      case 'identifier':
        {
          var value = selector.value.toLowerCase();
          return function (node) {
            // add debug output
            !node && console.warn('generateMatcher() selector.type == "identifier", node:', node, 'selector:', selector);
            return value === node.type.toLowerCase();
          };
        }
> @juzi/file-box@1.7.0 lint:es
> eslint --ignore-pattern fixtures/ "src/**/*.ts" "tests/**/*.ts"

generateMatcher() selector.type == "identifier", node: undefined selector: { type: 'identifier', value: 'JSXElement' }

Oops! Something went wrong! :(

ESLint: 8.34.0

TypeError: Cannot read properties of undefined (reading 'type')
Occurred while linting /Users/binsee/code/file-box/src/config.ts:2
    at /Users/binsee/code/file-box/node_modules/esquery/dist/esquery.js:3550:35
    at /Users/binsee/code/file-box/node_modules/esquery/dist/esquery.js:3631:22
    at Function.matches (/Users/binsee/code/file-box/node_modules/esquery/dist/esquery.js:3806:32)
    at NodeEventGenerator.applySelector (/Users/binsee/code/file-box/node_modules/eslint/lib/linter/node-event-generator.js:296:21)
    at NodeEventGenerator.applySelectors (/Users/binsee/code/file-box/node_modules/eslint/lib/linter/node-event-generator.js:324:22)
    at NodeEventGenerator.enterNode (/Users/binsee/code/file-box/node_modules/eslint/lib/linter/node-event-generator.js:340:14)
    at CodePathAnalyzer.enterNode (/Users/binsee/code/file-box/node_modules/eslint/lib/linter/code-path-analysis/code-path-analyzer.js:795:23)
    at /Users/binsee/code/file-box/node_modules/eslint/lib/linter/linter.js:1153:32
    at Array.forEach (<anonymous>)
    at runRules (/Users/binsee/code/file-box/node_modules/eslint/lib/linter/linter.js:1148:15)
michaelficarra commented 1 year ago

I've chosen to go with the more direct approach in #138 instead. Thanks anyway for the contribution.