Skip to content

Commit

Permalink
Merge f53e872 into 63d2a3f
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 9, 2020
2 parents 63d2a3f + f53e872 commit d78c75a
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 56 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Expand Up @@ -6,6 +6,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel

## [Unreleased]

## [2.21.2] - 2020-06-09
### Fixed
- [`order`]: avoid a crash on TypeScript’s `export import` syntax ([#1808], thanks [@ljharb])
- [`newline-after-import`]: consider TypeScript `import =` syntax' ([#1811], thanks [@ljharb])
- [`no-internal-modules`]: avoid a crash on a named export declaration ([#1814], thanks [@ljharb])

## [2.21.1] - 2020-06-07
### Fixed
- TypeScript: [`import/named`]: avoid requiring `typescript` when not using TS ([#1805], thanks [@ljharb])
Expand Down Expand Up @@ -897,6 +903,10 @@ for info on changes for earlier releases.
[#211]: https://github.com/benmosher/eslint-plugin-import/pull/211
[#164]: https://github.com/benmosher/eslint-plugin-import/pull/164
[#157]: https://github.com/benmosher/eslint-plugin-import/pull/157
[#1814]: https://github.com/benmosher/eslint-plugin-import/issues/1814
[#1811]: https://github.com/benmosher/eslint-plugin-import/issues/1811
[#1808]: https://github.com/benmosher/eslint-plugin-import/issues/1808
[#1805]: https://github.com/benmosher/eslint-plugin-import/issues/1805
[#1565]: https://github.com/benmosher/eslint-plugin-import/issues/1565
[#1366]: https://github.com/benmosher/eslint-plugin-import/issues/1366
[#1334]: https://github.com/benmosher/eslint-plugin-import/issues/1334
Expand Down Expand Up @@ -982,7 +992,8 @@ for info on changes for earlier releases.
[#119]: https://github.com/benmosher/eslint-plugin-import/issues/119
[#89]: https://github.com/benmosher/eslint-plugin-import/issues/89

[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.1...HEAD
[Unreleased]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.2...HEAD
[2.21.2]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.1...v2.21.2
[2.21.1]: https://github.com/benmosher/eslint-plugin-import/compare/v2.21.0...v2.21.1
[2.21.0]: https://github.com/benmosher/eslint-plugin-import/compare/v2.20.2...v2.21.0
[2.20.1]: https://github.com/benmosher/eslint-plugin-import/compare/v2.20.1...v2.20.2
Expand Down
6 changes: 3 additions & 3 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-import",
"version": "2.21.1",
"version": "2.21.2",
"description": "Import with sanity.",
"engines": {
"node": ">=4"
Expand Down Expand Up @@ -80,7 +80,7 @@
"eslint-plugin-json": "^2.1.1",
"fs-copy-file-sync": "^1.1.1",
"glob": "^7.1.6",
"in-publish": "^2.0.0",
"in-publish": "^2.0.1",
"linklocal": "^2.8.2",
"lodash.isarray": "^4.0.0",
"mocha": "^3.5.3",
Expand All @@ -90,7 +90,7 @@
"rimraf": "^2.7.1",
"semver": "^6.3.0",
"sinon": "^2.4.1",
"typescript": "~3.8.3",
"typescript": "~3.9.5",
"typescript-eslint-parser": "^22.0.0"
},
"peerDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/importType.js
Expand Up @@ -12,7 +12,7 @@ function baseModule(name) {
}

export function isAbsolute(name) {
return name.indexOf('/') === 0
return name && name.startsWith('/')
}

// path is defined only when a resolver resolves to a non-standard path
Expand Down
11 changes: 7 additions & 4 deletions src/rules/newline-after-import.js
Expand Up @@ -115,16 +115,19 @@ after ${type} statement not followed by another ${type}.`,
level--
}

return {
ImportDeclaration: function (node) {
function checkImport(node) {
const { parent } = node
const nodePosition = parent.body.indexOf(node)
const nextNode = parent.body[nodePosition + 1]

if (nextNode && nextNode.type !== 'ImportDeclaration') {
if (nextNode && nextNode.type !== 'ImportDeclaration' && nextNode.type !== 'TSImportEqualsDeclaration') {
checkForNewLine(node, nextNode, 'import')
}
},
}

return {
ImportDeclaration: checkImport,
TSImportEqualsDeclaration: checkImport,
CallExpression: function(node) {
if (isStaticRequire(node) && level === 0) {
requireCalls.push(node)
Expand Down
4 changes: 3 additions & 1 deletion src/rules/no-internal-modules.js
Expand Up @@ -95,7 +95,9 @@ module.exports = {
checkImportForReaching(node.source.value, node.source)
},
ExportNamedDeclaration(node) {
checkImportForReaching(node.source.value, node.source)
if (node.source) {
checkImportForReaching(node.source.value, node.source)
}
},
CallExpression(node) {
if (isStaticRequire(node)) {
Expand Down
2 changes: 2 additions & 0 deletions src/rules/order.js
Expand Up @@ -611,6 +611,8 @@ module.exports = {
let name
if (node.moduleReference.type === 'TSExternalModuleReference') {
name = node.moduleReference.expression.value
} else if (node.isExport) {
name = node.moduleReference.name
} else {
name = null
}
Expand Down
39 changes: 39 additions & 0 deletions tests/src/rules/newline-after-import.js
@@ -1,4 +1,7 @@
import { RuleTester } from 'eslint'
import flatMap from 'array.prototype.flatmap'

import { getTSParsers } from '../utils'

const IMPORT_ERROR_MESSAGE = 'Expected 1 empty line after import statement not followed by another import.'
const IMPORT_ERROR_MESSAGE_MULTIPLE = (count) => {
Expand Down Expand Up @@ -175,6 +178,42 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { sourceType: 'module' },
parser: require.resolve('babel-eslint'),
},
...flatMap(getTSParsers(), (parser) => [
{
code: `
import { ExecaReturnValue } from 'execa';
import execa = require('execa');
`,
parser: parser,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `
import execa = require('execa');
import { ExecaReturnValue } from 'execa';
`,
parser: parser,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `
import { ExecaReturnValue } from 'execa';
import execa = require('execa');
import { ExecbReturnValue } from 'execb';
`,
parser: parser,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
code: `
import execa = require('execa');
import { ExecaReturnValue } from 'execa';
import execb = require('execb');
`,
parser: parser,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
]),
],

invalid: [
Expand Down
24 changes: 23 additions & 1 deletion tests/src/rules/no-internal-modules.js
@@ -1,7 +1,8 @@
import { RuleTester } from 'eslint'
import flatMap from 'array.prototype.flatmap'
import rule from 'rules/no-internal-modules'

import { test, testFilePath } from '../utils'
import { test, testFilePath, getTSParsers } from '../utils'

const ruleTester = new RuleTester()

Expand Down Expand Up @@ -92,6 +93,27 @@ ruleTester.run('no-internal-modules', rule, {
allow: [ '**/index{.js,}' ],
} ],
}),
test({
code: `
export class AuthHelper {
static checkAuth(auth) {
}
}
`,
}),
...flatMap(getTSParsers(), (parser) => [
test({
code: `
export class AuthHelper {
public static checkAuth(auth?: string): boolean {
}
}
`,
parser: parser,
}),
]),
],

invalid: [
Expand Down
96 changes: 51 additions & 45 deletions tests/src/rules/order.js
Expand Up @@ -3,6 +3,7 @@ import { test, getTSParsers, getNonDefaultParsers } from '../utils'
import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver'
import flatMap from 'array.prototype.flatmap'

const ruleTester = new RuleTester()
, rule = require('rules/order')
Expand Down Expand Up @@ -167,8 +168,8 @@ ruleTester.run('order', rule, {
var index = require('./');
`,
}),
// Export equals expressions should be on top alongside with ordinary import-statements.
...getTSParsers().map(parser => (
...flatMap(getTSParsers(), parser => [
// Export equals expressions should be on top alongside with ordinary import-statements.
test({
code: `
import async, {foo1} from 'async';
Expand All @@ -181,8 +182,15 @@ ruleTester.run('order', rule, {
var index = require('./');
`,
parser,
})
)),
}),

test({
code: `
export import CreateSomething = _CreateSomething;
`,
parser,
}),
]),
// Adding unknown import types (e.g. using a resolver alias via babel) to the groups.
test({
code: `
Expand Down Expand Up @@ -1158,7 +1166,7 @@ ruleTester.run('order', rule, {
message: '`fs` import should occur after import of `../foo/bar`',
}],
}),
...getTSParsers().map(parser => (
...flatMap(getTSParsers(), parser => [
test({
code: `
var fs = require('fs');
Expand All @@ -1174,8 +1182,44 @@ ruleTester.run('order', rule, {
errors: [{
message: '`fs` import should occur after import of `../foo/bar`',
}],
})
)),
}),
{
code: `
var async = require('async');
var fs = require('fs');
`,
output: `
var fs = require('fs');
var async = require('async');
`,
parser,
errors: [{
message: '`fs` import should occur before import of `async`',
}],
},
test({
code: `
import sync = require('sync');
import async, {foo1} from 'async';
import index from './';
`,
output: `
import async, {foo1} from 'async';
import sync = require('sync');
import index from './';
`,
options: [{
groups: ['external', 'index'],
alphabetize: {order: 'asc'},
}],
parser,
errors: [{
message: '`async` import should occur before import of `sync`',
}],
}),
]),
// Default order using import with custom import alias
test({
code: `
Expand Down Expand Up @@ -1909,20 +1953,6 @@ ruleTester.run('order', rule, {
message: '`fs` import should occur before import of `async`',
}],
})),
...getTSParsers().map(parser => ({
code: `
var async = require('async');
var fs = require('fs');
`,
output: `
var fs = require('fs');
var async = require('async');
`,
parser,
errors: [{
message: '`fs` import should occur before import of `async`',
}],
})),
// Option alphabetize: {order: 'asc'}
test({
code: `
Expand All @@ -1947,30 +1977,6 @@ ruleTester.run('order', rule, {
message: '`Bar` import should occur before import of `bar`',
}],
}),
...getTSParsers().map(parser => (
test({
code: `
import sync = require('sync');
import async, {foo1} from 'async';
import index from './';
`,
output: `
import async, {foo1} from 'async';
import sync = require('sync');
import index from './';
`,
options: [{
groups: ['external', 'index'],
alphabetize: {order: 'asc'},
}],
parser,
errors: [{
message: '`async` import should occur before import of `sync`',
}],
})
)),
// Option alphabetize: {order: 'desc'}
test({
code: `
Expand Down

0 comments on commit d78c75a

Please sign in to comment.