@nx/eslint-plugin 21.3.0-beta.0 → 21.3.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nx/eslint-plugin",
3
- "version": "21.3.0-beta.0",
3
+ "version": "21.3.0-beta.2",
4
4
  "private": false,
5
5
  "description": "The eslint-plugin package is an ESLint plugin that contains a collection of recommended ESLint rule configurations which you can extend from in your own ESLint configs, as well as an Nx-specific lint rule called enforce-module-boundaries.",
6
6
  "repository": {
@@ -34,8 +34,9 @@
34
34
  }
35
35
  },
36
36
  "dependencies": {
37
- "@nx/devkit": "21.3.0-beta.0",
38
- "@nx/js": "21.3.0-beta.0",
37
+ "@nx/devkit": "21.3.0-beta.2",
38
+ "@nx/js": "21.3.0-beta.2",
39
+ "@phenomnomnominal/tsquery": "~5.0.1",
39
40
  "@typescript-eslint/type-utils": "^8.0.0",
40
41
  "@typescript-eslint/utils": "^8.0.0",
41
42
  "chalk": "^4.1.0",
@@ -24,3 +24,4 @@ export declare function validateEntry(baseNode: AST.JSONObjectExpression, key: s
24
24
  export declare function validateImplementationNode(implementationNode: AST.JSONProperty, key: string, context: TSESLint.RuleContext<MessageIds, Options>, options: NormalizedOptions): void;
25
25
  export declare function validatePackageGroup(baseNode: AST.JSONObjectExpression, context: TSESLint.RuleContext<MessageIds, Options>): void;
26
26
  export declare function validateVersionJsonExpression(node: AST.JSONExpression, context: TSESLint.RuleContext<MessageIds, Options>): string | boolean;
27
+ export declare function checkIfIdentifierIsFunction(filePath: string, identifier: string): boolean;
@@ -7,8 +7,11 @@ exports.validateEntry = validateEntry;
7
7
  exports.validateImplementationNode = validateImplementationNode;
8
8
  exports.validatePackageGroup = validatePackageGroup;
9
9
  exports.validateVersionJsonExpression = validateVersionJsonExpression;
10
+ exports.checkIfIdentifierIsFunction = checkIfIdentifierIsFunction;
10
11
  const tslib_1 = require("tslib");
11
12
  const utils_1 = require("@typescript-eslint/utils");
13
+ const fs_1 = require("fs");
14
+ const tsquery_1 = require("@phenomnomnominal/tsquery");
12
15
  const devkit_1 = require("@nx/devkit");
13
16
  const js_1 = require("@nx/js");
14
17
  const internal_1 = require("@nx/js/src/internal");
@@ -346,8 +349,7 @@ function validateImplementationNode(implementationNode, key, context, options) {
346
349
  }
347
350
  if (identifier) {
348
351
  try {
349
- const m = require(resolvedPath);
350
- if (!(identifier in m && typeof m[identifier] === 'function')) {
352
+ if (!checkIfIdentifierIsFunction(resolvedPath, identifier)) {
351
353
  context.report({
352
354
  messageId: 'invalidImplementationModule',
353
355
  node: implementationNode.value,
@@ -359,6 +361,7 @@ function validateImplementationNode(implementationNode, key, context, options) {
359
361
  }
360
362
  }
361
363
  catch {
364
+ // require can throw if the module is not found
362
365
  context.report({
363
366
  messageId: 'unableToReadImplementationExports',
364
367
  node: implementationNode.value,
@@ -429,3 +432,26 @@ function validateVersionJsonExpression(node, context) {
429
432
  ((0, semver_1.valid)(node.value) ||
430
433
  context.options[0]?.allowedVersionStrings.includes(node.value)));
431
434
  }
435
+ function checkIfIdentifierIsFunction(filePath, identifier) {
436
+ try {
437
+ const ts = require('typescript');
438
+ const sourceCode = (0, fs_1.readFileSync)(filePath, 'utf-8');
439
+ const sourceFile = ts.createSourceFile(filePath, sourceCode, ts.ScriptTarget.Latest, true);
440
+ const exportedFunctions = (0, tsquery_1.tsquery)(sourceFile, `
441
+ FunctionDeclaration[name.text="${identifier}"][modifiers],
442
+ ExportDeclaration > FunctionDeclaration[name.text="${identifier}"],
443
+ VariableStatement[modifiers] VariableDeclaration[name.text="${identifier}"] ArrowFunction,
444
+ VariableStatement[modifiers] VariableDeclaration[name.text="${identifier}"] FunctionExpression,
445
+ ExportDeclaration > VariableStatement VariableDeclaration[name.text="${identifier}"] ArrowFunction,
446
+ ExportDeclaration > VariableStatement VariableDeclaration[name.text="${identifier}"] FunctionExpression,
447
+ ExportDeclaration ExportSpecifier[name.text="${identifier}"]
448
+ `);
449
+ return exportedFunctions.length > 0;
450
+ }
451
+ catch {
452
+ // ignore
453
+ }
454
+ // Fallback to require()
455
+ const m = require(filePath);
456
+ return identifier in m && typeof m[identifier] === 'function';
457
+ }