Skip to content

Enhancement: [no-require-imports] Add option allowJson #5314

@remcohaszing

Description

@remcohaszing

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/no-require-imports/

Description

If module in tsconfig.json is set to Node16, TypeScript 4.7 will convert import … = require('…') statements to require calls created using _createRequire.

As long as import assertions are not allowed in Node.js, I believe this is a nice syntax to read package.json files.

I propose to add an option named allowJson to no-require-imports that allows require imports of JSON files.

Fail

import lodash = require('lodash')
import lib = require('./lib')

Pass

These are currently also errored on


import lodashPkg = require('lodash/package.json')
import pkg = require('./package.json')

Additional Info

In:

import pkg = require('./package.json')

console.log(pkg.version)

Out:

import { createRequire as _createRequire } from "module";
const __require = _createRequire(import.meta.url);
const pkg = __require("./package.json");
console.log(pkg.version);

TypeScript playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy