mysticatea / eslint-plugin-node

Additional ESLint's rules for Node.js
MIT License
958 stars 167 forks source link

RegEx pattern '^(?:@[a-zA-Z0-9_\-.]+/)?[a-zA-Z0-9_\-.]+$' does not match absolute paths #356

Closed vladimirdoroch closed 3 months ago

vladimirdoroch commented 3 months ago

I deploy AWS Lambda + AWS Lambda Layers.

Lambda Layers are mounted using an absolute path as /opt/* which is matched by node/no-unplished-import.

import { foo } from '/opt/foo/bar'; // matches node/no-unplished-import

I tried to set an exception for this in config .eslintrc.json as follows:

{
"rules": {
    "node/no-unpublished-import": ["error", {
        "allowModules": [
          "/opt/foo/bar/"
        ]
    }]
}

This did not work out because the regEx used to match the strings in the "allowModules" block uses regEx Pattern ^(?:@[a-zA-Z0-9_\-.]+/)?[a-zA-Z0-9_\-.]+$ which basically does not match strings starting with a slash /.

This could be fixed by adding slash to the 2nd capture group ^(?:@[a-zA-Z0-9_\-.]+/)?[a-zA-Z0-9_\-/.]+$

This did not work so I disabled the rule by inline config.

scagood commented 3 months ago

:thinking: absolute paths will not work for resolving imports when the package is used?

Can you provide a minimal example?

I have a feeling you don't want to use no-unpublished-import if you're not going to publish the package to a registry.

vladimirdoroch commented 3 months ago

Example:

Lambda Layer

Code that would be deployed by Lambda Layer in file foo.ts

// mount point for Lambdas would be "/opt/foo"
export const bar = "";

Lambda Function

Code that would be deployed as AWS Lambda Function and be attached to Lambda Layer above.

import { Handler, Context } from 'aws-lambda';
import { bar } from '/opt/foo'; // violates node/no-unpublished-import

ESLint config

{
"rules": {
    "node/no-unpublished-import": ["error", {
        "allowModules": [
          "/opt/foo/bar/"
        ]
    }]
}

Eslint returns the following exception:

Error: .eslintrc.json: Configuration for rule "node/no-unpublished-import" is invalid: Value "/opt/foo" should match pattern "^(?:@[a-zA-Z0-9-.]+/)?[a-zA-Z0-9-.]+$".

Turning off the rule in general is not a desired solution since we use it in a larger mono-repo with some lambdas in sub-directories. Would rather like to add an exception in the .eslintrc.json as proposed but this does not work since the regEx pattern used does not work.

scagood commented 3 months ago

:thinking: I see, can you raise an issue in our maintained fork over at https://github.com/eslint-community/eslint-plugin-n please?

vladimirdoroch commented 3 months ago

Will raise it as proposed. ty