Skip to content

Commit

Permalink
[Fix] default: typescript-eslint-parser: avoid a crash on exporti…
Browse files Browse the repository at this point in the history
…ng as namespace

 - test on more versions of `typescript-eslint-parser`
  • Loading branch information
ljharb committed Jan 28, 2022
1 parent fc98de2 commit ad18a62
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,10 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
- [`no-named-default`, `no-default-export`, `prefer-default-export`, `no-named-export`, `export`, `named`, `namespace`, `no-unused-modules`]: support arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])
- [`no-dynamic-require`]: support dynamic import with espree ([#2371], thanks [@sosukesuzuki])


### Fixed
- [`default`]: `typescript-eslint-parser`: avoid a crash on exporting as namespace (thanks [@ljharb])

### Changed
- [Tests] `no-nodejs-modules`: add tests for node protocol URL ([#2367], thanks [@sosukesuzuki])
- [Tests] `default`, `no-anonymous-default-export`, `no-mutable-exports`, `no-named-as-default-member`, `no-named-as-default`: add tests for arbitrary module namespace names ([#2358], thanks [@sosukesuzuki])
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -93,7 +93,7 @@
"semver": "^6.3.0",
"sinon": "^2.4.1",
"typescript": "^2.8.1 || ~3.9.5",
"typescript-eslint-parser": "^15 || ^22.0.0"
"typescript-eslint-parser": "^15 || ^20 || ^22"
},
"peerDependencies": {
"eslint": "^2 || ^3 || ^4 || ^5 || ^6 || ^7.2.0 || ^8"
Expand Down
2 changes: 1 addition & 1 deletion src/ExportMap.js
Expand Up @@ -645,7 +645,7 @@ ExportMap.parse = function (path, content, context) {
// This doesn't declare anything, but changes what's being exported.
if (includes(exports, n.type)) {
const exportedName = n.type === 'TSNamespaceExportDeclaration'
? n.id.name
? (n.id || n.name).name
: (n.expression && n.expression.name || (n.expression.id && n.expression.id.name) || null);
const declTypes = [
'VariableDeclaration',
Expand Down
3 changes: 3 additions & 0 deletions tests/dep-time-travel.sh
Expand Up @@ -32,6 +32,9 @@ if [[ "$ESLINT_VERSION" -lt "4" ]]; then

echo "Downgrading TypeScript dependencies..."
npm i --no-save typescript-eslint-parser@15 typescript@2.8.1
elif [[ "$ESLINT_VERSION" -lt "7" ]]; then
echo "Downgrading TypeScript dependencies..."
npm i --no-save typescript-eslint-parser@20
fi

# typescript-eslint-parser 1.1.1+ is not compatible with node 6
Expand Down
10 changes: 6 additions & 4 deletions tests/src/rules/default.js
@@ -1,6 +1,8 @@
import path from 'path';
import { test, testVersion, SYNTAX_CASES, getTSParsers, parsers } from '../utils';
import { RuleTester } from 'eslint';
import semver from 'semver';
import { version as tsEslintVersion } from 'typescript-eslint-parser/package.json';

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

Expand Down Expand Up @@ -165,7 +167,7 @@ if (!CASE_SENSITIVE_FS) {
context('TypeScript', function () {
getTSParsers().forEach((parser) => {
ruleTester.run(`default`, rule, {
valid: [
valid: [].concat(
test({
code: `import foobar from "./typescript-default"`,
parser,
Expand All @@ -190,14 +192,14 @@ context('TypeScript', function () {
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
}),
test({
semver.satisfies(tsEslintVersion, '>= 22') ? test({
code: `import foobar from "./typescript-export-assign-mixed"`,
parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
}),
}) : [],
test({
code: `import foobar from "./typescript-export-assign-default-reexport"`,
parser,
Expand Down Expand Up @@ -258,7 +260,7 @@ context('TypeScript', function () {
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
}),
],
),

invalid: [
test({
Expand Down
17 changes: 9 additions & 8 deletions tests/src/rules/export.js
Expand Up @@ -3,6 +3,7 @@ import { test, testFilePath, SYNTAX_CASES, getTSParsers, testVersion } from '../
import { RuleTester } from 'eslint';
import eslintPkg from 'eslint/package.json';
import semver from 'semver';
import { version as tsEslintVersion } from 'typescript-eslint-parser/package.json';

const ruleTester = new RuleTester();
const rule = require('rules/export');
Expand Down Expand Up @@ -149,7 +150,7 @@ context('TypeScript', function () {
};

ruleTester.run('export', rule, {
valid: [
valid: [].concat(
// type/value name clash
test(Object.assign({
code: `
Expand All @@ -164,20 +165,20 @@ context('TypeScript', function () {
`,
}, parserConfig)),

test(Object.assign({
semver.satisfies(tsEslintVersion, '>= 22') ? test(Object.assign({
code: `
export function fff(a: string);
export function fff(a: number);
`,
}, parserConfig)),
}, parserConfig)) : [],

test(Object.assign({
semver.satisfies(tsEslintVersion, '>= 22') ? test(Object.assign({
code: `
export function fff(a: string);
export function fff(a: number);
export function fff(a: string|number) {};
`,
}, parserConfig)),
}, parserConfig)) : [],

// namespace
test(Object.assign({
Expand Down Expand Up @@ -224,7 +225,7 @@ context('TypeScript', function () {
filename: testFilePath('typescript-d-ts/file-2.ts'),
}, parserConfig)),

...(semver.satisfies(eslintPkg.version, '< 6') ? [] : [
(semver.satisfies(eslintPkg.version, '< 6') ? [] : [
test({
code: `
export * as A from './named-export-collision/a';
Expand Down Expand Up @@ -258,7 +259,7 @@ context('TypeScript', function () {
`,
}, parserConfig)),

...(semver.satisfies(process.version, '< 8') && semver.satisfies(eslintPkg.version, '< 6') ? [] : test({
(semver.satisfies(process.version, '< 8') && semver.satisfies(eslintPkg.version, '< 6') ? [] : test({
...parserConfig,
code: `
export * from './module';
Expand All @@ -269,7 +270,7 @@ context('TypeScript', function () {
'import/extensions': ['.js', '.ts', '.jsx'],
},
})),
],
),
invalid: [
// type/value name clash
test(Object.assign({
Expand Down
18 changes: 10 additions & 8 deletions tests/src/rules/newline-after-import.js
@@ -1,5 +1,7 @@
import { RuleTester } from 'eslint';
import flatMap from 'array.prototype.flatmap';
import semver from 'semver';
import { version as tsEslintVersion } from 'typescript-eslint-parser/package.json';

import { getTSParsers, parsers, testVersion } from '../utils';

Expand All @@ -12,7 +14,7 @@ const REQUIRE_ERROR_MESSAGE = 'Expected 1 empty line after require statement not
const ruleTester = new RuleTester();

ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
valid: [
valid: [].concat(
`var path = require('path');\nvar foo = require('foo');\n`,
`require('foo');`,
`switch ('foo') { case 'bar': require('baz'); }`,
Expand Down Expand Up @@ -178,7 +180,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parserOptions: { sourceType: 'module' },
parser: parsers.BABEL_OLD,
},
...flatMap(getTSParsers(), (parser) => [
flatMap(getTSParsers(), (parser) => [].concat(
{
code: `
import { ExecaReturnValue } from 'execa';
Expand Down Expand Up @@ -213,14 +215,14 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parser,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
parser !== parsers.TS_OLD || semver.satisfies(tsEslintVersion, '>= 22') ? {
code: `
export import a = obj;\nf(a);
`,
parser,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
{
} : [],
parser !== parsers.TS_OLD || semver.satisfies(tsEslintVersion, '>= 22') ? {
code: `
import { a } from "./a";
Expand All @@ -230,7 +232,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
}`,
parser,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
} : [],
{
code: `
import stub from './stub';
Expand All @@ -242,7 +244,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
parser,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
]),
)),
{
code: `
import stub from './stub';
Expand All @@ -253,7 +255,7 @@ ruleTester.run('newline-after-import', require('rules/newline-after-import'), {
`,
parserOptions: { ecmaVersion: 2015, sourceType: 'module' },
},
],
),

invalid: [].concat(
{
Expand Down
18 changes: 8 additions & 10 deletions tests/src/rules/prefer-default-export.js
@@ -1,6 +1,8 @@
import { test, testVersion, getNonDefaultParsers, parsers } from '../utils';

import { RuleTester } from 'eslint';
import semver from 'semver';
import { version as tsEslintVersion } from 'typescript-eslint-parser/package.json';

const ruleTester = new RuleTester();
const rule = require('../../../src/rules/prefer-default-export');
Expand Down Expand Up @@ -164,28 +166,24 @@ context('TypeScript', function () {
};

ruleTester.run('prefer-default-export', rule, {
valid: [
valid: [].concat(
// Exporting types
test({
semver.satisfies(tsEslintVersion, '>= 22') ? test({
code: `
export type foo = string;
export type bar = number;`,
...parserConfig,
}),
}) : [],
test({
code: `
export type foo = string;
export type bar = number;`,
...parserConfig,
}),
test({
code: 'export type foo = string',
...parserConfig,
}),
test({
semver.satisfies(tsEslintVersion, '>= 22') ? test({
code: 'export type foo = string',
...parserConfig,
}),
}) : [],
test({
code: 'export interface foo { bar: string; }',
...parserConfig,
Expand All @@ -194,7 +192,7 @@ context('TypeScript', function () {
code: 'export interface foo { bar: string; }; export function goo() {}',
...parserConfig,
}),
],
),
invalid: [],
});
});
Expand Down

0 comments on commit ad18a62

Please sign in to comment.