Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throw error when parse try catch block on parseExpressionAt #894

Closed
himself65 opened this issue Dec 11, 2019 · 3 comments
Closed

throw error when parse try catch block on parseExpressionAt #894

himself65 opened this issue Dec 11, 2019 · 3 comments

Comments

@himself65
Copy link

related issue nodejs/node#30872

const Acorn = require('acorn')
const Parser = Acorn.Parser
const parseExpressionAt = Parser.parseExpressionAt.bind(Parser)
const str = 'try {         assert(condition, message)\n' +
  '  } catch (e) {\n' +
  '    throw e\n' +
  '  }\n' +
  '\n' +
  '// Now run tests\n' +
  '\n' +
  'console.log(\'Call assert proxy with slightly minified whitespace\')\n' +
  'try {\n' +
  '  assertProxySlightlyMinified(0)\n' +
  '  console.error(`assertProxySlightlyMinified(0); failed to cause an exception`)\n' +
  '} catch (e) {\n' +
  '  console.log(`assertProxySlightlyMinified(0); generated exception: ${e}`)\n' +
  '}\n' +
  '\n' +
  'console.log(\'Finished tests\')'
const res = Parser.parseExpressionAt(str, 0)
console.log(res)
C:\Users\Himself65\Desktop\github\test>node index.js
C:\Users\Himself65\Desktop\github\test\node_modules\acorn\dist\acorn.js:2838
    throw err
    ^

SyntaxError: Unexpected token (1:0)
    at Parser.pp$4.raise (C:\Users\Himself65\Desktop\github\test\node_modules\?[4macorn?[24m\dist\acorn.js:2836:15)
    at Parser.pp.unexpected (C:\Users\Himself65\Desktop\github\test\node_modules\?[4macorn?[24m\dist\acorn.js:689:10)
    at Parser.pp$3.parseExprAtom (C:\Users\Himself65\Desktop\github\test\node_modules\?[4macorn?[24m\dist\acorn.js:2260:12)
    at Parser.pp$3.parseExprSubscripts (C:\Users\Himself65\Desktop\github\test\node_modules\?[4macorn?[24m\dist\acorn.js:2089:21)
    at Parser.pp$3.parseMaybeUnary (C:\Users\Himself65\Desktop\github\test\node_modules\?[4macorn?[24m\dist\acorn.js:2066:19)
    at Parser.pp$3.parseExprOps (C:\Users\Himself65\Desktop\github\test\node_modules\?[4macorn?[24m\dist\acorn.js:2010:21)
    at Parser.pp$3.parseMaybeConditional (C:\Users\Himself65\Desktop\github\test\node_modules\?[4macorn?[24m\dist\acorn.js:1993:21)
    at Parser.pp$3.parseMaybeAssign (C:\Users\Himself65\Desktop\github\test\node_modules\?[4macorn?[24m\dist\acorn.js:1968:21)
    at Parser.pp$3.parseExpression (C:\Users\Himself65\Desktop\github\test\node_modules\?[4macorn?[24m\dist\acorn.js:1933:21)
    at Function.parseExpressionAt (C:\Users\Himself65\Desktop\github\test\node_modules\?[4macorn?[24m\dist\acorn.js:582:19) {
  pos: ?[33m0?[39m,
  loc: Position { line: ?[33m1?[39m, column: ?[33m0?[39m },
  raisedAt: ?[33m3?[39m
}

but it success when remove try {

const Acorn = require('acorn')
const Parser = Acorn.Parser
const parseExpressionAt = Parser.parseExpressionAt.bind(Parser)
const str = '        assert(condition, message)\n' +
  '  } catch (e) {\n' +
  '    throw e\n' +
  '  }\n' +
  '\n' +
  '// Now run tests\n' +
  '\n' +
  'console.log(\'Call assert proxy with slightly minified whitespace\')\n' +
  'try {\n' +
  '  assertProxySlightlyMinified(0)\n' +
  '  console.error(`assertProxySlightlyMinified(0); failed to cause an exception`)\n' +
  '} catch (e) {\n' +
  '  console.log(`assertProxySlightlyMinified(0); generated exception: ${e}`)\n' +
  '}\n' +
  '\n' +
  'console.log(\'Finished tests\')'
const res = Parser.parseExpressionAt(str, 0)
console.log(res)
C:\Users\Himself65\Desktop\github\test>node index.js
Node {
  type: 'CallExpression',
  start: 8,
  end: 34,
  callee: Node { type: 'Identifier', start: 8, end: 14, name: 'assert' },
  arguments: [
    Node { type: 'Identifier', start: 15, end: 24, name: 'condition' },
    Node { type: 'Identifier', start: 26, end: 33, name: 'message' }
  ]
}
@mysticatea
Copy link
Contributor

I think that it's correct behavior. Because TryStatement is a statement rather than an expression, and parseExpressionAt method parses an expression.

@marijnh
Copy link
Member

marijnh commented Dec 11, 2019

Agreed, this is working as documented.

@marijnh marijnh closed this as completed Dec 11, 2019
@himself65
Copy link
Author

thanks for helping

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants