Skip to content

Commit

Permalink
refactor: update resource name validation to use inflection library a…
Browse files Browse the repository at this point in the history
…nd improve error messaging
  • Loading branch information
Baltazore committed Jan 5, 2025
1 parent 57013a5 commit 5ab2d5d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { dasherize, singularize } = require('@ember-data/request-utils/string');
const { dasherize, singularize } = require('inflection');

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
Expand All @@ -22,17 +23,22 @@ module.exports = {
const args = decorator.arguments;

// TODO: add support for passing normalize function to rule to test
const resourceName = args.at(0); // The first argument is the resource name
const normalizedResourceName = dasherize(singularize(resourceName.value));
const resource = args.at(0); // The first argument is the resource name
const resourceName = resource.value;
const normalizedResourceName = dasherize(singularize(resourceName));

console.log({ resourceName, normalizedResourceName });
console.log(resourceName === normalizedResourceName, 'true');

if (resourceName.value === normalizedResourceName) {
if (resourceName !== normalizedResourceName) {
context.report({
node,
message: `The @{{decorator}} decorator resource name should be singular and dasherized, but found '{{resourceName}}'.`,
message: `The @${node.callee.name} decorator resource name should be singular and dasherized (${normalizedResourceName}), but found '${resourceName}'.`,
data: {
decorator: decorator.callee.name,
},
});
return;
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
const rule = require('../src/rules/no-invalid-relationships');
const RuleTester = require('eslint').RuleTester;

const parserOptions = { ecmaVersion: 2022, sourceType: 'module', requireConfigFile: false };

const ruleTester = new RuleTester({
languageOptions: {
parser: require('@babel/eslint-parser'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
const rule = require('../src/rules/require-singular-dasherized-resource-name');
const RuleTester = require('eslint').RuleTester;

const eslintTester = new RuleTester({
parserOptions: { ecmaVersion: 2015 },
const ruleTester = new RuleTester({
languageOptions: {
parser: require('@babel/eslint-parser'),
ecmaVersion: 'latest',
sourceType: 'module',
parserOptions: {
requireConfigFile: false,
babelOptions: {
babelrc: false,
configFile: false,
plugins: [[require.resolve('@babel/plugin-proposal-decorators'), { legacy: true }]],
},
},
},
});

const messageId = 'warp-drive.require-singular-dasherized-resource-name';

eslintTester.run('resource-naming', rule, {
ruleTester.run('require-singular-dasherized-resource-name', rule, {
valid: [
`import Model, { belongsTo } from '@ember-data/model';
Expand All @@ -30,14 +40,14 @@ eslintTester.run('resource-naming', rule, {
invalid: [
{
code: `import Model, { belongsTo } from '@ember-data/model';
export default class extends Model {
@belongsTo('Post', { async: true, inverse: 'post-comments' }) post;
@belongsTo('Posts', { async: true, inverse: 'post-comments' }) post;
}`,
output: null,
errors: [
{
message: 'The @{{decorator}} decorator resource name should be singular and dasherized, but found Post.',
message:
"The @belongsTo decorator resource name should be singular and dasherized (Post), but found 'Posts'.",
type: 'CallExpression',
},
],
Expand Down

0 comments on commit 5ab2d5d

Please sign in to comment.