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

[bugfix] Re: #1332 - skip warning on a type declaration or alias. #1377

Merged
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
9 changes: 8 additions & 1 deletion src/rules/prefer-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ module.exports = {
// if there are specifiers, node.declaration should be null
if (!node.declaration) return

// don't count flow types exports
// don't warn on single type aliases or declarations
if (node.exportKind === 'type') return

if (
node.declaration.type === 'TSTypeAliasDeclaration' ||
node.declaration.type === 'TypeAlias'
Copy link
Contributor

Choose a reason for hiding this comment

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

@sharmilajesupaul @brieb should this also ignore interfaces? I believe the Babel/TypeScript AST type is InterfaceDeclaration and the typescript eslint parser type is TSInterfaceDeclaration.

Copy link
Contributor

Choose a reason for hiding this comment

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

) {
return
}

if (node.declaration.declarations) {
node.declaration.declarations.forEach(function(declaration) {
captureDeclaration(declaration.id)
Expand Down
17 changes: 2 additions & 15 deletions tests/src/rules/export.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { test, SYNTAX_CASES } from '../utils'
import { test, SYNTAX_CASES, getTSParsers } from '../utils'

import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver'

var ruleTester = new RuleTester()
, rule = require('rules/export')
Expand Down Expand Up @@ -111,18 +109,7 @@ ruleTester.run('export', rule, {


context('Typescript', function () {
// Typescript
const parsers = []

if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
parsers.push(require.resolve('@typescript-eslint/parser'))
}

if (semver.satisfies(eslintPkg.version, '>=4.0.0 <6.0.0')) {
parsers.push(require.resolve('typescript-eslint-parser'))
}

parsers.forEach((parser) => {
getTSParsers().forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
Expand Down
17 changes: 2 additions & 15 deletions tests/src/rules/named.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { test, SYNTAX_CASES } from '../utils'
import { test, SYNTAX_CASES, getTSParsers } from '../utils'
import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver'

import { CASE_SENSITIVE_FS } from 'eslint-module-utils/resolve'

Expand Down Expand Up @@ -285,18 +283,7 @@ ruleTester.run('named (export *)', rule, {


context('Typescript', function () {
// Typescript
const parsers = []

if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
parsers.push(require.resolve('@typescript-eslint/parser'))
}

if (semver.satisfies(eslintPkg.version, '<6.0.0')) {
parsers.push(require.resolve('typescript-eslint-parser'))
}

parsers.forEach((parser) => {
getTSParsers().forEach((parser) => {
['typescript', 'typescript-declare', 'typescript-export-assign'].forEach((source) => {
ruleTester.run(`named`, rule, {
valid: [
Expand Down
19 changes: 2 additions & 17 deletions tests/src/rules/no-deprecated.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { test, SYNTAX_CASES } from '../utils'
import { test, SYNTAX_CASES, getTSParsers } from '../utils'

import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver'


const ruleTester = new RuleTester()
, rule = require('rules/no-deprecated')
Expand Down Expand Up @@ -202,19 +199,7 @@ ruleTester.run('no-deprecated: hoisting', rule, {
})

describe('Typescript', function () {
// Typescript
const parsers = []

if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
parsers.push(require.resolve('@typescript-eslint/parser'))
}

// typescript-eslint-parser doesn't support this rule on ESLint <4 for some reason
if (semver.satisfies(eslintPkg.version, '>=4.0.0 <6.0.0')) {
parsers.push(require.resolve('typescript-eslint-parser'))
}

parsers.forEach((parser) => {
getTSParsers().forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
Expand Down
23 changes: 3 additions & 20 deletions tests/src/rules/order.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test, testVersion } from '../utils'
import { test, testVersion, getTSParsers } from '../utils'

import { RuleTester } from 'eslint'

Expand Down Expand Up @@ -1372,8 +1372,7 @@ ruleTester.run('order', rule, {
message: '`fs` import should occur before import of `async`',
}],
})),
// fix incorrect order with typescript-eslint-parser
testVersion('<6.0.0', () => ({
...getTSParsers().map(parser => ({
code: `
var async = require('async');
var fs = require('fs');
Expand All @@ -1382,23 +1381,7 @@ ruleTester.run('order', rule, {
var fs = require('fs');
var async = require('async');
`,
parser: require.resolve('typescript-eslint-parser'),
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `async`',
}],
})),
// fix incorrect order with @typescript-eslint/parser
testVersion('>5.0.0', () => ({
code: `
var async = require('async');
var fs = require('fs');
`,
output: `
var fs = require('fs');
var async = require('async');
`,
parser: require.resolve('@typescript-eslint/parser'),
parser,
errors: [{
ruleId: 'order',
message: '`fs` import should occur before import of `async`',
Expand Down
56 changes: 53 additions & 3 deletions tests/src/rules/prefer-default-export.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { test } from '../utils'
import { test, getNonDefaultParsers } from '../utils'

import { RuleTester } from 'eslint'

Expand Down Expand Up @@ -83,7 +83,6 @@ ruleTester.run('prefer-default-export', rule, {
code: 'export { a, b } from "foo.js"',
parser: require.resolve('babel-eslint'),
}),

// ...SYNTAX_CASES,
],
invalid: [
Expand Down Expand Up @@ -129,4 +128,55 @@ ruleTester.run('prefer-default-export', rule, {
}],
}),
],
})
});

context('Typescript', function() {
getNonDefaultParsers().forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
};

ruleTester.run('prefer-default-export', rule, {
valid: [
// Exporting types
test(
{
code: `
export type foo = string;
export type bar = number;`,
parser,
},
parserConfig,
),
test(
{
code: `
export type foo = string;
export type bar = number;`,
parser,
},
parserConfig,
),
test(
{
code: 'export type foo = string',
parser,
},
parserConfig,
),
test(
{
code: 'export type foo = string',
parser,
},
parserConfig,
),
],
invalid: [],
});
});
});
16 changes: 16 additions & 0 deletions tests/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ export function testFilePath(relativePath) {
return path.join(process.cwd(), './tests/files', relativePath)
}

export function getTSParsers() {
const parsers = [];
if (semver.satisfies(eslintPkg.version, '>=4.0.0 <6.0.0')) {
parsers.push(require.resolve('typescript-eslint-parser'));
}

if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
parsers.push(require.resolve('@typescript-eslint/parser'));
}
return parsers;
}

export function getNonDefaultParsers() {
return getTSParsers().concat(require.resolve('babel-eslint'));
}

export const FILENAME = testFilePath('foo.js')

export function testVersion(specifier, t) {
Expand Down