Skip to content

Commit

Permalink
Merge pull request #7 from beerinho/dbeer/update-to-ember-template-li…
Browse files Browse the repository at this point in the history
  • Loading branch information
pichfl authored Mar 9, 2022
2 parents b8030db + 8740ad5 commit 87ddf1d
Show file tree
Hide file tree
Showing 11 changed files with 1,806 additions and 2,697 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module.exports = {
root: true,
parserOptions: {
ecmaVersion: '2019',
sourceType: 'module',
},
plugins: ['prettier'],
plugins: ['prettier'],
extends: ['eslint:recommended', 'prettier'],
env: {
node: true,
Expand Down
File renamed without changes.
File renamed without changes.
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module.exports = {
name: 'css-modules',
import StaticLocalClassRule from './rules/static-local-class.js';
import NoClassRule from './rules/no-class.js';

export default {
name: 'css-modules',
rules: {
'css-modules/static-local-class': require('./rules/static-local-class'),
'css-modules/no-class': require('./rules/no-class'),
'css-modules/static-local-class': StaticLocalClassRule,
'css-modules/no-class': NoClassRule,
},
};
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
{
"name": "ember-template-lint-plugin-css-modules",
"version": "0.1.1",
"type": "module",
"description": "ember-template-lint plugin for ember-css-modules",
"repository": "github:simplabs/ember-template-lint-plugin-css-modules",
"license": "MIT",
"author": "Tobias Bieniek <[email protected]>",
"main": "index.js",
"scripts": {
"lint": "eslint . --cache",
"lint": "eslint .",
"release": "release-it",
"test": "jest"
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules npx jest"
},
"devDependencies": {
"ember-template-lint": "^2.4.1",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-prettier": "^3.1.2",
"jest": "^25.2.7",
"prettier": "2.0.4",
"cross-env": "^7.0.3",
"ember-template-lint": "^4.2.0",
"eslint": "^8.10.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^27.5.1",
"prettier": "^2.5.1",
"release-it": "^14.12.5",
"release-it-lerna-changelog": "^4.0.1"
},
"peerDependencies": {
"ember-template-lint": "^2.4.1"
"ember-template-lint": "^4.2.0"
},
"engines": {
"node": "^12.22.0 || ^14.17.0 || >=16.0.0"
}
}
}
14 changes: 7 additions & 7 deletions rules/no-class.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
const { Rule } = require('ember-template-lint');
import { Rule } from 'ember-template-lint';

class NoClassRule extends Rule {
export function createErrorMessage(classNames) {
return `Found class="${classNames}". Use local-class attribute.`;
}
export default class NoClassRule extends Rule {
visitor() {
return {
AttrNode(node) {
if (node.name === 'class') {
this.log({
message: NoClassRule.ERROR_MESSAGE(node.value.chars),
message: createErrorMessage(node.value.chars),
line: node.loc && node.loc.start.line,
column: node.loc && node.loc.start.column,
source: this.sourceForNode(node),
node,
});
}
},
};
}
}

NoClassRule.ERROR_MESSAGE = classNames => `Found class="${classNames}". Use local-class attribute.`;

module.exports = NoClassRule;
10 changes: 4 additions & 6 deletions rules/no-class.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
const { generateRuleTests } = require('../utils/test-helpers');
const Rule = require('./no-class');
import { generateRuleTests } from '../utils/test-helpers.js';
import { createErrorMessage } from './no-class.js';

generateRuleTests({
name: 'css-modules/no-class',

config: true,

good: [`<div></div>`, `<div local-class=""></div>`],

bad: [
{
template: '<div class="foo"></div>',
result: {
message: Rule.ERROR_MESSAGE('foo'),
message: createErrorMessage('foo'),
line: 1,
column: 5,
source: 'class="foo"',
Expand All @@ -21,7 +19,7 @@ generateRuleTests({
{
template: '<div class="foo" local-class="bar"></div>',
result: {
message: Rule.ERROR_MESSAGE('foo'),
message: createErrorMessage('foo'),
line: 1,
column: 5,
source: 'class="foo"',
Expand Down
14 changes: 6 additions & 8 deletions rules/static-local-class.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { Rule } = require('ember-template-lint');
import { Rule } from 'ember-template-lint';

export const ERROR_MESSAGE =
'`local-class` attributes should only use static values or `if` conditions';
/**
* ember-css-modules allows us to use `local-class` attributes to refer to
* CSS modules that are scoped to only the components. To achieve that CSS
Expand All @@ -17,7 +19,7 @@ const { Rule } = require('ember-template-lint');
* This custom ember-template-lint rule checks that we only use static values
* or if-conditions as values for the `local-class` attributes.
*/
class StaticLocalClassRule extends Rule {
export default class StaticLocalClassRule extends Rule {
visitor() {
return {
AttrNode(node) {
Expand All @@ -38,10 +40,11 @@ class StaticLocalClassRule extends Rule {

logForNode(node) {
this.log({
message: StaticLocalClassRule.ERROR_MESSAGE,
message: ERROR_MESSAGE,
line: node.loc && node.loc.start.line,
column: node.loc && node.loc.start.column,
source: this.sourceForNode(node),
node,
});
}

Expand All @@ -62,8 +65,3 @@ class StaticLocalClassRule extends Rule {
return false;
}
}

StaticLocalClassRule.ERROR_MESSAGE =
'`local-class` attributes should only use static values or if conditions';

module.exports = StaticLocalClassRule;
12 changes: 5 additions & 7 deletions rules/static-local-class.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const { generateRuleTests } = require('../utils/test-helpers');
const Rule = require('./static-local-class');
import { generateRuleTests } from '../utils/test-helpers.js';
import { ERROR_MESSAGE } from './static-local-class.js';

generateRuleTests({
name: 'css-modules/static-local-class',

config: true,

good: [
`<div></div>`,
`<div local-class=""></div>`,
Expand All @@ -18,7 +16,7 @@ generateRuleTests({
{
template: '<div local-class={{@foo}}></div>',
result: {
message: Rule.ERROR_MESSAGE,
message: ERROR_MESSAGE,
line: 1,
column: 17,
source: '{{@foo}}',
Expand All @@ -27,7 +25,7 @@ generateRuleTests({
{
template: '<div local-class="foo {{@bar}}"></div>',
result: {
message: Rule.ERROR_MESSAGE,
message: ERROR_MESSAGE,
line: 1,
column: 17,
source: '"foo {{@bar}}"',
Expand All @@ -36,7 +34,7 @@ generateRuleTests({
{
template: '<div local-class={{if @foo @bar}}></div>',
result: {
message: Rule.ERROR_MESSAGE,
message: ERROR_MESSAGE,
line: 1,
column: 17,
source: '{{if @foo @bar}}',
Expand Down
11 changes: 5 additions & 6 deletions utils/test-helpers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/* eslint-env jest */

const _generateRuleTests = require('ember-template-lint/lib/helpers/rule-test-harness');
import { generateRuleTests as _generateRuleTests } from 'ember-template-lint';
import plugin from '../index.js';

function generateRuleTests(options) {
export function generateRuleTests(options) {
return _generateRuleTests({
plugins: [require('../index')],

config: true,
groupMethodBefore: beforeEach,
groupingMethod: describe,
testMethod: test,
focusMethod: test.only,
plugins: [plugin],

...options,
});
}

module.exports = { generateRuleTests };
Loading

0 comments on commit 87ddf1d

Please sign in to comment.