Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce file extension in import #380

Merged
merged 3 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changeset/four-cooks-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@cloudfour/eslint-plugin': major
---

Enabled [`@cloudfour/n/file-extension-in-import`](https://github.com/weiran-zsd/eslint-plugin-node/blob/HEAD/docs/rules/file-extension-in-import.md).

❌ `require('./foo')` → ✅ `require('./foo.js')`
❌ `import * as foo from './foo'` → ✅ `import * as foo from './foo.js'`

If the file that you are importing is a `.ts` file, you must import it as `.js`, because of a [decision that the TypeScript team made](https://github.com/microsoft/TypeScript/issues/16577#issuecomment-754941937).

It is auto-fixable.
2 changes: 1 addition & 1 deletion fixtures/downshift/src/hooks/useCombobox/reducer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getHighlightedIndexOnOpen, getDefaultValue } from '../utils';
import { getHighlightedIndexOnOpen, getDefaultValue } from '../utils.js';
import { getNextWrappingIndex, getNextNonDisabledIndex } from '../../utils';
import * as stateChangeTypes from './stateChangeTypes';

Expand Down
2 changes: 1 addition & 1 deletion fixtures/downshift/src/hooks/useCombobox/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
getElementIds as getElementIdsCommon,
defaultProps as defaultPropsCommon,
getInitialState as getInitialStateCommon,
} from '../utils';
} from '../utils.js';

function getElementIds({ id, inputId, ...rest }) {
const uniqueId = id === undefined ? `downshift-${generateId()}` : id;
Expand Down
4 changes: 2 additions & 2 deletions fixtures/downshift/src/hooks/useSelect/reducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getNextWrappingIndex, getNextNonDisabledIndex } from '../../utils';
import { getHighlightedIndexOnOpen, getDefaultValue } from '../utils';
import { getItemIndexByCharacterKey } from './utils';
import { getHighlightedIndexOnOpen, getDefaultValue } from '../utils.js';
import { getItemIndexByCharacterKey } from './utils.js';
Comment on lines 1 to +3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why does the following not have the .js extension?

import { getNextWrappingIndex, getNextNonDisabledIndex } from '../../utils';

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it only enforces it for files which it can find/actually exist.

So in this case, the file doesn't exist (since this file is just one of the fixtures files picked out of the downshift project, and other files in that project weren't included).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting.

import * as stateChangeTypes from './stateChangeTypes';

/* eslint-disable complexity */
Expand Down
2 changes: 1 addition & 1 deletion fixtures/downshift/src/hooks/useSelect/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import PropTypes from 'prop-types';
import { defaultProps as commonDefaultProps } from '../utils';
import { defaultProps as commonDefaultProps } from '../utils.js';

// eslint-disable-next-line max-params
function getItemIndexByCharacterKey(
Expand Down
2 changes: 1 addition & 1 deletion fixtures/rollup/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import addCliEntry from './build-plugins/add-cli-entry.js';
import conditionalFsEventsImport from './build-plugins/conditional-fsevents-import';
import emitModulePackageFile from './build-plugins/emit-module-package-file.js';
import esmDynamicImport from './build-plugins/esm-dynamic-import.js';
import getLicenseHandler from './build-plugins/generate-license-file';
import getLicenseHandler from './build-plugins/generate-license-file.js';
import pkg from './package.json';

const commitHash = (function () {
Expand Down
88 changes: 55 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@typescript-eslint/parser": "^5.0.0",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jsdoc": "^39.0.0",
"eslint-plugin-n": "^15.0.1",
"eslint-plugin-n": "^15.2.1",
"eslint-plugin-promise": "^6.0.0",
"eslint-plugin-unicorn": "^42.0.0"
},
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ module.exports.configs = {
'n/no-unsupported-features/es-syntax': 'off', // Does not account for transpilation
'n/no-unpublished-require': 'off', // Does not account for "build" scripts
'n/shebang': 'off', // Tons of false positives
'n/file-extension-in-import': ['error', 'always'], // Don't allow extension-less relative imports (e.g. use ./foo.js instead of ./foo)

'unicorn/import-style': 'off', // It doesn't seem useful to force people to use named, default, or namespace imports
'unicorn/prevent-abbreviations': 'off', // Causes more issues than it's worth
Expand Down