question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`assertions` property is always array when `importAssertions` parser plugin is enabled

See original GitHub issue

Bug 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:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
sosukesuzukicommented, Nov 12, 2020

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

1reaction
JLHwungcommented, Nov 11, 2020

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found