Skip to content

Commit c6740d0

Browse files
committed
added syntax battery to test utils for future use + fixed #281
1 parent 2539fb3 commit c6740d0

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).
55

6+
## Unreleased
7+
### Fixed
8+
- [`no-named-as-default-member`] don't crash on rest props. ([#281], thanks [@SimenB])
9+
610
## resolvers/webpack: Unreleased
711
### Changed
812
- automatically find webpack config with `interpret`-able extensions ([#287], thanks [@taion])
@@ -194,6 +198,7 @@ for info on changes for earlier releases.
194198
[#164]: https://github.com/benmosher/eslint-plugin-import/pull/164
195199

196200
[#286]: https://github.com/benmosher/eslint-plugin-import/issues/286
201+
[#281]: https://github.com/benmosher/eslint-plugin-import/issues/281
197202
[#216]: https://github.com/benmosher/eslint-plugin-import/issues/216
198203
[#214]: https://github.com/benmosher/eslint-plugin-import/issues/214
199204
[#210]: https://github.com/benmosher/eslint-plugin-import/issues/210

src/rules/no-named-as-default-member.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ module.exports = function(context) {
4949
}
5050

5151
function handleDestructuringAssignment(node) {
52-
if (!node.init) return
53-
5452
const isDestructure = (
55-
node.id.type === 'ObjectPattern' && node.init.type === 'Identifier'
53+
node.id.type === 'ObjectPattern' &&
54+
node.init != null &&
55+
node.init.type === 'Identifier'
5656
)
5757
if (!isDestructure) return
5858

5959
const objectName = node.init.name
60-
for (const {key} of node.id.properties) {
60+
for (const { key } of node.id.properties) {
61+
if (key == null) continue // true for rest properties
6162
storePropertyLookup(objectName, key.name, key)
6263
}
6364
}

tests/src/rules/no-named-as-default-member.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {test} from '../utils'
1+
import { test, SYNTAX_CASES } from '../utils'
22
import {RuleTester} from 'eslint'
33
import rule from 'rules/no-named-as-default-member'
44

@@ -11,8 +11,7 @@ ruleTester.run('no-named-as-default-member', rule, {
1111
test({code: 'import {foo} from "./bar"; const baz = foo.baz;'}),
1212
test({code: 'import * as named from "./named-exports"; const a = named.a'}),
1313

14-
// issue #249
15-
test({code: 'for (const {foo, bar} of baz) {}'}),
14+
...SYNTAX_CASES,
1615
],
1716

1817
invalid: [

tests/src/utils.js

+30
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,33 @@ export function testContext(settings) {
2828
export function getFilename(file) {
2929
return path.join(__dirname, '..', 'files', file || 'foo.js')
3030
}
31+
32+
/**
33+
* to be added as valid cases just to ensure no nullable fields are going
34+
* to crash at runtinme
35+
* @type {Array}
36+
*/
37+
export const SYNTAX_CASES = [
38+
39+
test({ code: 'for (let { foo, bar } of baz) {}' }),
40+
test({ code: 'for (let [ foo, bar ] of baz) {}' }),
41+
42+
test({ code: 'const { x, y } = bar' }),
43+
test({ code: 'const { x, y, ...z } = bar', parser: 'babel-eslint' }),
44+
45+
// all the exports
46+
test({ code: 'export { x }' }),
47+
test({ code: 'export { x as y }' }),
48+
49+
// not sure about these since they reference a file
50+
// test({ code: 'export { x } from "./y.js"'}),
51+
// test({ code: 'export * as y from "./y.js"', parser: 'babel-eslint'}),
52+
53+
test({ code: 'export const x = null' }),
54+
test({ code: 'export var x = null' }),
55+
test({ code: 'export let x = null' }),
56+
57+
test({ code: 'export default x' }),
58+
test({ code: 'export default class x {}' }),
59+
60+
]

0 commit comments

Comments
 (0)