google-code-export / esprima

Automatically exported from code.google.com/p/esprima
BSD 2-Clause "Simplified" License
1 stars 0 forks source link

Harmony: tolerate non-top-level import and export declarations #619

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The current ES6 spec (and esprima's "harmony" branch) only allow `import`s at 
the top-level of a module (i.e. not nested inside another block, such as a 
function body, `try`, etc.).

However, when using ES6 in transpiled-to-ES5 context, it's useful to loosen 
this constraint and just treat `import`s as a regular statement that's allowed 
anywhere. Therefore, I'd like to request the addition of a parser option 
(defaulting to false) that would allow `import`s anywhere.

For comparison, Acorn has an `allowImportExportEverywhere` option; see 
https://github.com/marijnh/acorn/issues/174

Original issue reported on code.google.com by cvreb...@gmail.com on 4 Jan 2015 at 11:23

GoogleCodeExporter commented 9 years ago
Esprima favors a single flag for tolerating certain syntax violation (for 
maintenance reason). I will include the illegal import and export declarations 
in that tolerant mode.

Original comment by ariya.hi...@gmail.com on 12 Jan 2015 at 3:20

GoogleCodeExporter commented 9 years ago
With the change, the example output will be as follows:

require('./esprima').parse('function f() { import "jquery" }', { tolerant: true 
})
{ type: 'Program',
  body: 
   [ { type: 'FunctionDeclaration',
       id: [Object],
       params: [],
       defaults: [],
       body: [Object],
       rest: null,
       generator: false,
       expression: false } ],
  errors: 
   [ { [Error: Line 1: Illegal import declaration]
       index: 14,
       lineNumber: 1,
       column: 15,
       description: 'Illegal import declaration' } ] }

Note that there might be other invalid syntax tolerated by tolerant=true. If 
you want to stop if there are other syntax errors, check the errors array and 
make sure it only contains the "Illegal import declaration".

Original comment by ariya.hi...@gmail.com on 12 Jan 2015 at 3:24

GoogleCodeExporter commented 9 years ago
Tolerate illegal import and export declarations.
https://github.com/ariya/esprima/commit/b1dd29e985

Original comment by ariya.hi...@gmail.com on 12 Jan 2015 at 3:26