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

Custom Lint Rule no-css-modules #114

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions config/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ module.exports = {
'no-cond-assign': [WARNING, 'always'],
'no-const-assign': WARNING,
'no-control-regex': WARNING,
'no-css-modules': WARNING,
'no-delete-var': WARNING,
'no-dupe-args': WARNING,
'no-dupe-class-members': WARNING,
Expand Down
32 changes: 32 additions & 0 deletions config/rules/no-css-modules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

module.exports = {
create: function (context) {
return {
ImportDeclaration: function(node) {
if (node) {
Copy link

Choose a reason for hiding this comment

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

I wouldn't think this check is necessary right?

Copy link
Author

Choose a reason for hiding this comment

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

Its guarded in one of the eslint rules with ImportDeclaration, happy to remove it if you dont think its necessary

var specifiers = node.specifiers || [];
var value = node.source && node.source.value;

if (value && value.indexOf('.css') !== -1 && specifiers.length) {
for (var i = 0; i < specifiers.length; i++) {
var specifier = specifiers[i];

context.report(specifier, 'CSS modules import is restricted. ' +
'Please remove the \'{{importName}}\' portion of the import.', {
importName: specifier.imported ? specifier.imported.name : specifier.local.name
});
}
}
}
}
};
}
};
3 changes: 2 additions & 1 deletion config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ module.exports = {
},
eslint: {
configFile: path.join(__dirname, 'eslint.js'),
useEslintrc: false
useEslintrc: false,
rulesdir: 'config/rules'
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this still work after #149?

},
postcss: function() {
return [autoprefixer];
Expand Down
3 changes: 2 additions & 1 deletion config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ module.exports = {
// TODO: consider separate config for production,
// e.g. to enable no-console and no-debugger only in prod.
configFile: path.join(__dirname, 'eslint.js'),
useEslintrc: false
useEslintrc: false,
rulesdir: 'config/rules'
},
postcss: function() {
return [autoprefixer];
Expand Down
6 changes: 6 additions & 0 deletions scripts/eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ prompt('Are you sure you want to eject? This action is permanent. [y/N]', functi
});
delete hostPackage.scripts['eject'];

// explicitly specify ESLint config path for editor plugins
hostPackage.eslintConfig = {
extends: './config/eslint.js',
rulesdir: './config/rules',
};

console.log('Writing package.json');
fs.writeFileSync(
path.join(hostPath, 'package.json'),
Expand Down
6 changes: 6 additions & 0 deletions scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ module.exports = function(hostPath, appName, verbose) {
hostPackage.scripts[command] = 'react-scripts ' + command;
});

// explicitly specify ESLint config path for editor plugins
hostPackage.eslintConfig = {
extends: './node_modules/react-scripts/config/eslint.js',
rulesdir: './config/rules',
};

fs.writeFileSync(
path.join(hostPath, 'package.json'),
JSON.stringify(hostPackage, null, 2)
Expand Down
2 changes: 1 addition & 1 deletion tasks/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ npm install
scripts_path=$PWD/`npm pack`

# lint
./node_modules/.bin/eslint --ignore-path .gitignore ./
./node_modules/.bin/eslint --rulesdir ./config/rules --ignore-path .gitignore ./

# Test local start command
npm start -- --smoke-test
Expand Down