`assertions` property is always array when `importAssertions` parser plugin is enabled
See original GitHub issueBug Report
- I would like to work on a fix!
If this behaviour is intentional, I want to know the reason.
Current behavior
assertions
property of ImportDeclaration
(and other nodes for import/export) is empty array when assert
does not exist if importAssertions
parser plugin is enabled.
AST
<div>{
"type": "ImportDeclaration",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start": 7,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 10
}
},
"local": {
"type": "Identifier",
"start": 7,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 10
},
"identifierName": "foo"
},
"name": "foo"
}
}
],
"source": {
"type": "StringLiteral",
"start": 16,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 28
}
},
"extra": {
"rawValue": "./foo.json",
"raw": "\"./foo.json\""
},
"value": "./foo.json"
},
"assertions": []
}
</div>Input Code
import foo from "./foo.json";
Expected behavior
The assertions
property should be undefined if the assert does not exist, even if the importAssertions
plugin is enabled.
We want to distinguish between import foo from ". /foo.json"
and import foo from "foo.json" assert {}
.
AST
<div>{
"type": "ImportDeclaration",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"specifiers": [
{
"type": "ImportDefaultSpecifier",
"start": 7,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 10
}
},
"local": {
"type": "Identifier",
"start": 7,
"end": 10,
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 10
},
"identifierName": "foo"
},
"name": "foo"
}
}
],
"source": {
"type": "StringLiteral",
"start": 16,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 28
}
},
"extra": {
"rawValue": "./foo.json",
"raw": "\"./foo.json\""
},
"value": "./foo.json"
},
- "assertions": []
}
</div>Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
- Filename:
babel.config.js
const { parse } = require("@babel/parser");
parse(`import foo from "./foo.json"`, {
sourceType: "module",
plugins: ["importAssertions"],
});
Environment
Possible Solution
Additional context
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Enable import assertions for Babel in node.js - Stack Overflow
Add @babel/plugin-syntax-import-assertions (https://git.io/JUbkv) to the 'plugins' section of your Babel config to enable parsing.
Read more >@babel/plugin-syntax-import-assertions - Package Manager
@babel/plugin-syntax-import-assertions. owner babel37.4mMIT7.20.0. Allow parsing of the module assertion attributes in the import statement. babel-plugin ...
Read more >babel/parser
The Babel parser (previously Babylon) is a JavaScript parser used in Babel. ... plugins: Array containing the plugins that you want to enable....
Read more >SystemVerilog Assertions - ChipVerify
What are properties of a design ? If a property of the design that is being checked for by an assertion does not...
Read more >rollup.js
Rollup can import existing CommonJS modules through a plugin. ... file to simplify command line usage and enable advanced Rollup functionality.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s a bit off topic, I found a bug like this because the regex can’t detect comments😄 https://github.com/prettier/prettier/issues/9645
@sosukesuzuki We could develop a
prettier
parser plugin that tries to differentiate these cases but eventually prettier should work on a CST to preserve/format styles.