oxc/crates/oxc_parser/src
overlookmotel eb2966c512
fix(parser): fix incorrectly identified directives (#1885)
Parser incorrectly identifies string literals as directives if they
follow after `import`s, `export`s, or decorators.

In all of these cases, `'use strict'` produces a directive in the AST,
where it should be parsed as an `ExpressionStatement` containing a
`StringLiteral`:

```js
import x from 'foo';
'use strict';
```

```js
export {x};
'use strict';
```

```js
@foo
'use strict';
```


[Playground](https://oxc-project.github.io/oxc/playground/?code=3YCAAIC0gICAgICAgIC0G8rnONK89ITJ3zrK%2FUP7OmSZPgHQzStr3yMtwFTU%2BD1WPt09JgqZJLoYooydbGsM5vGcf34BnIA%3D)

This PR should fix that.

I'm not sure about the decorator case, though. I assume it's not a
directive. But is prefixing a string literal with a decorator even legal
syntax anyway?

And a side nit: If I'm reading it right, I don't think the `continue`
statement in the decorator arm of the match does anything. Do I have
that right?

Last question: Where does one go about putting a test? I guess these
silly cases aren't covered by Babel etc's tests.

---------

Co-authored-by: Boshen <boshenc@gmail.com>
2024-01-04 13:39:15 +00:00
..
js fix(parser): fix incorrectly identified directives (#1885) 2024-01-04 13:39:15 +00:00
jsx fix(parser): await in jsx expression 2023-12-19 20:23:16 +08:00
lexer fix(parser): error on source larger than 4 GiB (#1860) 2024-01-02 11:05:28 +08:00
ts chore: upgrade rustc toolchain to stable 1.75.0 (#1853) 2023-12-29 12:20:51 +08:00
context.rs fix(parser): false postive for "Missing initializer in const declaration" in declare + namespace (#1724) 2023-12-18 17:03:42 +08:00
cursor.rs chore(clippy): enable undocumented_unsafe_blocks 2023-10-16 15:18:14 +08:00
diagnostics.rs fix(parser): error on source larger than 4 GiB (#1860) 2024-01-02 11:05:28 +08:00
lib.rs fix(parser): fix incorrectly identified directives (#1885) 2024-01-04 13:39:15 +00:00
list.rs fix(parser): fix crashing on empty ParenthesizedExpression with comments (#263) 2023-04-06 17:16:15 +08:00
state.rs refactor(ast): change Option<Vec> to Vec for decorators (#84) 2023-03-02 15:52:46 +08:00