Skip to content
This repository has been archived by the owner on Aug 18, 2020. It is now read-only.

Commit

Permalink
most things
Browse files Browse the repository at this point in the history
  • Loading branch information
cartogram authored and Matt Seccafien committed May 16, 2019
1 parent 89ba94e commit 7e88f34
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- `react/state-in-constructor` Enforce state initialization to be in a class property. ([256](https://github.com/Shopify/eslint-plugin-shopify/pull/246))

### Fixed

- `react-prefer-private-members` from incorrectly reporting the members of a parent class if a React class is defined within its constructor. ([258](https://github.com/Shopify/eslint-plugin-shopify/pull/258))

## [28.0.0] - 2019-04-26
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ This plugin provides the following custom rules, which are included as appropria
- [jsx-no-complex-expressions](docs/rules/jsx-no-complex-expressions.md): Disallow complex expressions embedded in in JSX.
- [jsx-no-hardcoded-content](docs/rules/jsx-no-hardcoded-content.md): Disallow hardcoded content in JSX.
- [jsx-prefer-fragment-wrappers](docs/rules/jsx-prefer-fragment-wrappers.md): Disallow useless wrapping elements in favour of fragment shorthand in JSX.
- [no-namespace-imports](docs/rules/no-namespace-imports.md): Prevent the usage of unnecessary computed properties.
- [no-useless-computed-properties](docs/rules/no-useless-computed-properties.md): Prevent the usage of unnecessary computed properties.
- [polaris-no-bare-stack-item](docs/rules/polaris-no-bare-stack-item.md): Disallow the use of Polaris’s `Stack.Item` without any custom props.
- [polaris-prefer-sectioned-prop](docs/rules/polaris-prefer-sectioned-prop.md): Prefer the use of the `sectioned` props in Polaris components instead of wrapping all contents in a `Section` component.
Expand Down
63 changes: 56 additions & 7 deletions docs/rules/no-namespace-imports.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# Prevent namespace import declarations. (no-namespace-imports)

Please describe the origin of the rule here.

Prior to TypeScript 2.7, CommonJS/AMD/UMD moduels were treated in the same way as ES6 modules. This was inconsistent with ECMAScript spec and resulted in namespace imports, such as `import * as foo from "foo"`, for a CommonJS/AMD/UMD module as equivalent to `const foo = require("foo")` (where `foo` can be more than just a non-callable object). TypeScript 2.7 and later adds `--esModuleInterop` which will change the generated output from TypeScript closer to that generated by Babel where namespace imports are properly flagged as uncallable and default imports work expected.

## Rule Details

This rule aims to...
This rule will report and change namespace imports to default imports on a predefined, but configurable, list of modules.

Examples of **incorrect** code for this rule:

Expand All @@ -23,14 +22,64 @@ import React from 'react';

```

### Options

If there are any options, describe them here. Otherwise, delete this section.
### `modules`

```json
{
"shopify/no-namespace-imports": [
"error",
{
"modules": ["react", "some-custom-module"]
}
]
}
```

This array options adds modules to the predefined list and this rule will report and fix their namespaced imports.

### `ignore`

```json
{
"shopify/no-namespace-imports": [
"error",
{
"ignore": ["react", "react-dom"]
}
]
}
```

This array option whitelists items in predefined `modules` list. Modules defined in this option will no have their namespace imports reported or fixed. There are eight possible values.

* `'@shopify/app-bridge'`
* `'@shopify/app-bridge/actions'`
* `'@shopify/app-bridge/validate/actions/navigation'`
* `'@shopify/app-bridge/validate/actions/resourcePicker'`
* `'@shopify/app-bridge/validate/actions'`
* `'@shopify/react-form-state'`
* `'@shopify/safe-redirect'`
* `'d3'`
* `'faker'`
* `'fs'`
* `'glob'`
* `'history'`
* `'http'`
* `'koa'`
* `'react-dom'`
* `'react-router'`
* `'redux'`
* `'topojson'`
* `'prop-types'`
* `'react'`
* `'url'`


## When Not To Use It

Give a short description of when it would be appropriate to turn off this rule.
If you are on a Typescript version that does not support the `--esModuleInterop` flag or do not wish to modify your namespace imports, you can safely disable this rule.

## Further Reading

If there are other links that describe the issue this rule addresses, please include them here in a bulleted list.
* [Typescript 2.7 Release Notes](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-from-commonjs-modules-with---esmoduleinterop)
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
'jsx-prefer-fragment-wrappers': require('./lib/rules/jsx-prefer-fragment-wrappers'),
'no-ancestor-directory-import': require('./lib/rules/no-ancestor-directory-import'),
'no-debugger': require('./lib/rules/no-debugger'),
'no-namespace-imports': require('./lib/rules/no-namespace-imports'),
'no-useless-computed-properties': require('./lib/rules/no-useless-computed-properties'),
'no-fully-static-classes': require('./lib/rules/no-fully-static-classes'),
'polaris-prefer-sectioned-prop': require('./lib/rules/polaris-prefer-sectioned-prop'),
Expand Down
2 changes: 2 additions & 0 deletions lib/config/rules/shopify.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module.exports = {
'shopify/no-ancestor-directory-import': 'off',
// Disallow the use of debugger (without fixer to prevent autofix on save in editors)
'shopify/no-debugger': 'error',
// Prevent namespace import declarations
'shopify/no-namespace-imports': 'error',
// Prevent the usage of unnecessary computed properties.
'shopify/no-useless-computed-properties': 'error',
// Prevent the declaration of classes consisting only of static members.
Expand Down
53 changes: 28 additions & 25 deletions lib/rules/no-namespace-imports.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
const {docsUrl} = require('../utilities');

const DEFAULT_MODULES = [
'@shopify/app-bridge',
'@shopify/app-bridge/actions',
'@shopify/app-bridge/validate/actions/navigation',
'@shopify/app-bridge/validate/actions/resourcePicker',
'@shopify/app-bridge/validate/actions',
'@shopify/react-form-state',
'@shopify/safe-redirect',
'd3',
'faker',
'fs',
'glob',
'history',
'http',
'koa',
'react-dom',
'react-router',
'redux',
'topojson',
'prop-types',
'react',
'url',
];

module.exports = {
meta: {
docs: {
Expand All @@ -18,7 +42,7 @@ module.exports = {
},
ignore: {
type: 'array',
items: {type: 'string'},
items: {enum: DEFAULT_MODULES},
},
},
additionalProperties: false,
Expand Down Expand Up @@ -84,30 +108,9 @@ function getNamespaceSpecifiers(node) {
}

function notCommonJsModule(node, modules) {
return ![
'@shopify/app-bridge',
'@shopify/app-bridge/actions',
'@shopify/app-bridge/validate/actions/navigation',
'@shopify/app-bridge/validate/actions/resourcePicker',
'@shopify/app-bridge/validate/actions',
'@shopify/react-form-state',
'@shopify/safe-redirect',
'd3',
'faker',
'fs',
'glob',
'history',
'http',
'koa',
'react-dom',
'react-router',
'redux',
'topojson',
'prop-types',
'react',
'url',
...modules,
].some((module) => module === node.source.value);
return ![...DEFAULT_MODULES, ...modules].some(
(module) => module === node.source.value,
);
}

function isIgnoredModule(node, ignored) {
Expand Down

0 comments on commit 7e88f34

Please sign in to comment.