Skip to content

Commit

Permalink
feat: no-unpublished-import supports ignoreTypeImport (fixes mysticat…
Browse files Browse the repository at this point in the history
…ea#78) (mysticatea#79)

* fix: skip import type nodes in no-unpublished-import rule (mysticatea#78)

* feat: no-unpublished-import supports ignoreTypeImport

---------

Co-authored-by: 唯然 <weiran.zsd@outlook.com>
  • Loading branch information
Toilal and aladdin-add authored Mar 3, 2023
1 parent e43d4ea commit 1fc0bf6
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 5 deletions.
20 changes: 20 additions & 0 deletions docs/rules/no-unpublished-import.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,26 @@ In addition, we can specify glob patterns to exclude files.

TODO

### ignoreTypeImport

If using typescript, you may want to ignore type imports. This option allows you to do that.

```json
{
"rules": {
"n/no-unpublished-import": ["error", {
"ignoreTypeImport": true
}]
}
}
```

In this way, the following code will not be reported:

```ts
import type foo from "foo";
```

### Shared Settings

The following options can be set by [shared settings](http://eslint.org/docs/user-guide/configuring.html#adding-shared-settings).
Expand Down
9 changes: 8 additions & 1 deletion lib/rules/no-unpublished-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module.exports = {
allowModules: getAllowModules.schema,
convertPath: getConvertPath.schema,
resolvePaths: getResolvePaths.schema,
ignoreTypeImport: { type: "boolean", default: false },
},
additionalProperties: false,
},
Expand All @@ -36,11 +37,17 @@ module.exports = {
},
create(context) {
const filePath = context.getFilename()
const options = context.options[0] || {}
const ignoreTypeImport =
options.ignoreTypeImport === void 0
? false
: options.ignoreTypeImport

if (filePath === "<input>") {
return {}
}

return visitImport(context, {}, targets => {
return visitImport(context, { ignoreTypeImport }, targets => {
checkPublish(context, filePath, targets)
})
},
Expand Down
2 changes: 1 addition & 1 deletion lib/util/check-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ exports.checkPublish = function checkPublish(context, filePath, targets) {
context.report({
node: target.node,
loc: target.node.loc,
message: '"{{name}}" is not published.',
messageId: "notPublished",
data: { name: target.moduleName || target.name },
})
}
Expand Down
12 changes: 11 additions & 1 deletion lib/util/visit-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ const stripImportPathParams = require("./strip-import-path-params")
* @param {Object} [options] - The flag to include core modules.
* @param {boolean} [options.includeCore] - The flag to include core modules.
* @param {number} [options.optionIndex] - The index of rule options.
* @param {boolean} [options.ignoreTypeImport] - The flag to ignore typescript type imports.
* @param {function(ImportTarget[]):void} callback The callback function to get result.
* @returns {ImportTarget[]} A list of found target's information.
*/
module.exports = function visitImport(
context,
{ includeCore = false, optionIndex = 0 } = {},
{ includeCore = false, optionIndex = 0, ignoreTypeImport = false } = {},
callback
) {
const targets = []
Expand All @@ -52,6 +53,15 @@ module.exports = function visitImport(
return
}

// skip `import type { foo } from 'bar'` (for eslint-typescript)
if (
ignoreTypeImport &&
node.type === "ImportDeclaration" &&
node.importKind === "type"
) {
return
}

const name = sourceNode && stripImportPathParams(sourceNode.value)
// Note: "999" arbitrary to check current/future Node.js version
if (name && (includeCore || !isCoreModule(name, "999"))) {
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"semver": "^7.3.8"
},
"devDependencies": {
"@typescript-eslint/parser": "^5.51.0",
"codecov": "^3.3.0",
"esbuild": "^0.14.39",
"eslint": "^8.27.0",
Expand All @@ -43,7 +44,8 @@
"prettier": "^2.7.1",
"punycode": "^2.1.1",
"release-it": "^15.5.0",
"rimraf": "^3.0.2"
"rimraf": "^3.0.2",
"typescript": "^4.9.5"
},
"scripts": {
"build": "node scripts/update",
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/no-unpublished/1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"aaa": "0.0.0"
},
"devDependencies": {
"bbb": "0.0.0"
"bbb": "0.0.0",
"foo": "0.0.0"
}
}
33 changes: 33 additions & 0 deletions tests/lib/rules/no-unpublished-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ ruleTester.run("no-unpublished-import", rule, {
filename: fixture("private-package/index.js"),
code: "import bbb from 'bbb';",
},

// https://github.com/eslint-community/eslint-plugin-n/issues/78
{
filename: fixture("1/test.ts"),
parser: path.join(
__dirname,
"../../../node_modules/@typescript-eslint/parser"
),
code: "import type foo from 'foo';",
options: [{ ignoreTypeImport: true }],
},
],
invalid: [
{
Expand Down Expand Up @@ -272,5 +283,27 @@ ruleTester.run("no-unpublished-import", rule, {
},
]
: []),

// https://github.com/eslint-community/eslint-plugin-n/issues/78
{
filename: fixture("1/test.ts"),
parser: path.join(
__dirname,
"../../../node_modules/@typescript-eslint/parser"
),
code: "import type foo from 'foo';",
options: [{ ignoreTypeImport: false }],
errors: [{ messageId: "notPublished" }],
},

{
filename: fixture("1/test.ts"),
parser: path.join(
__dirname,
"../../../node_modules/@typescript-eslint/parser"
),
code: "import type foo from 'foo';",
errors: [{ messageId: "notPublished" }],
},
],
})

0 comments on commit 1fc0bf6

Please sign in to comment.