Skip to content

Commit

Permalink
chore(deps): use flat config
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssimon committed Jan 5, 2025
1 parent 26d106a commit 6628679
Show file tree
Hide file tree
Showing 8 changed files with 2,417 additions and 3,759 deletions.
1 change: 0 additions & 1 deletion .eslintignore

This file was deleted.

1 change: 0 additions & 1 deletion commitlint.config.js

This file was deleted.

1 change: 1 addition & 0 deletions commitlint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default { extends: ['@commitlint/config-conventional'] }
66 changes: 66 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import jest from 'eslint-plugin-jest'

import { FlatCompat } from '@eslint/eslintrc'
import { fixupConfigRules } from '@eslint/compat'


// mimic CommonJS variables -- not needed if using CommonJS
const __filename = fileURLToPath(import.meta.url) // eslint-disable-line no-underscore-dangle
const __dirname = path.dirname(__filename) // eslint-disable-line no-underscore-dangle

const compat = new FlatCompat({
baseDirectory: __dirname,
})


export default [
{
ignores: [
'.yarn/',
'.yalc/',
'coverage/',
],
},

...fixupConfigRules(compat.config({
extends: [
'@jenssimon/base',
],
})).map((rule) => ({
files: [
'**/*.js',
'**/*.mjs',
],
...rule,
})),

{
files: [
'**/*.js',
],
rules: {
'unicorn/prefer-module': 'off',
},
},

...fixupConfigRules(compat.config({
extends: [
'@jenssimon/sfcc',
],
})).map((rule) => ({
files: [
'test/**/*.js',
],
...rule,
})),

{
files: [
'**/*.test.js',
],
...jest.configs['flat/recommended'],
},
]
18 changes: 10 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ const t = require('@babel/types')
const SUPPORTED_EXTENSIONS = ['js', 'ds', 'json']

const getModulePath = (moduleName, basePath, cartridge, target) => {
const relPath = path.relative(
const relativePath = path.relative(
path.dirname(moduleName),
`${basePath}/${cartridge}${target}`,
)
return (relPath.includes('.') ? '' : './') + relPath
return (relativePath.includes('.') ? '' : './') + relativePath
}

const plugin = (babel, { cartridgePath, basePath }) => ({
visitor: {
Program(thePath, state) {
const imports = []
thePath.traverse(importsVisitor, { imports })
imports.forEach((imp) => {
// eslint-disable-next-line no-restricted-syntax
for (const imp of imports) {
/**
* Finds and sets the rewrittten module path.
*
Expand All @@ -41,9 +42,10 @@ const plugin = (babel, { cartridgePath, basePath }) => ({
//
if (imp.source.indexOf('*/') === 0) {
resolve((target) => cartridgePath
// eslint-disable-next-line sonarjs/no-nested-functions
.find((cartridge) => SUPPORTED_EXTENSIONS
.find((ext) => fs.existsSync(`${basePath}/${cartridge}${target}.${ext}`))))
.find((cartridge) => SUPPORTED_EXTENSIONS.find(
// eslint-disable-next-line sonarjs/no-nested-functions
(extension) => fs.existsSync(`${basePath}/${cartridge}${target}.${extension}`),
)))
}

// Handle
Expand All @@ -55,7 +57,7 @@ const plugin = (babel, { cartridgePath, basePath }) => ({
if (imp.source.indexOf('~/') === 0) {
resolve(() => path.relative(basePath, path.dirname(state.file.opts.filename)).split(path.sep)[0])
}
})
}
},

MemberExpression(thePath, state) {
Expand Down Expand Up @@ -85,7 +87,7 @@ const plugin = (babel, { cartridgePath, basePath }) => ({

// Find the the cartridge which contains the next match for the module path
const foundCartridge = newCartridgePath.find((theCartridge) => SUPPORTED_EXTENSIONS
.find((ext) => fs.existsSync(`${basePath}/${theCartridge}${shortendedPathToModule}.${ext}`)))
.find((extension) => fs.existsSync(`${basePath}/${theCartridge}${shortendedPathToModule}.${extension}`)))

let foundRequire
if (foundCartridge) {
Expand Down
46 changes: 8 additions & 38 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,71 +32,41 @@
"postpack": "pinst --enable",
"semantic-release": "semantic-release"
},
"dependencies": {
"@babel/types": "^7.23.0",
"imports-visitor": "^2.0.0"
},
"devDependencies": {
"@babel/core": "^7.23.2",
"@babel/eslint-parser": "^7.22.15",
"@commitlint/cli": "^19.0.0",
"@commitlint/config-conventional": "^19.0.0",
"@eslint/compat": "^1.2.4",
"@eslint/eslintrc": "^3.2.0",
"@jenssimon/eslint-config-base": "^8.0.0",
"@jenssimon/eslint-config-sfcc": "^4.8.0",
"@jest/globals": "^29.7.0",
"commitizen": "^4.3.0",
"cz-conventional-changelog": "^3.3.0",
"eslint": "^8.52.0",
"eslint-plugin-jest": "^28.9.0",
"eslint-plugin-jest": "^28.10.0",
"husky": "^9.0.0",
"jest": "^29.7.0",
"lint-staged": "^15.0.2",
"pinst": "^3.0.0",
"semantic-release": "^24.0.0"
},
"dependencies": {
"@babel/types": "^7.23.0",
"imports-visitor": "^2.0.0"
},
"files": [
"/lib"
],
"eslintConfig": {
"extends": [
"@jenssimon/base"
],
"rules": {
"unicorn/prefer-module": "off",
"jest/unbound-method": "off"
},
"overrides": [
{
"files": [
"**/*.test.*",
"**/*.spec.*",
"**/__tests__/**",
"**/__mocks__/**"
],
"plugins": [
"jest"
],
"extends": [
"plugin:jest/all"
],
"rules": {
"jest/max-expects": "off",
"jest/no-hooks": "off",
"jest/prefer-expect-assertions": "off",
"jest/prefer-lowercase-title": "off",
"jest/unbound-method": "off"
}
}
]
},
"packageManager": "yarn@4.6.0",
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"lint-staged": {
"**/*.{js,ts}": "eslint"
"**/*.{js,mjs}": "eslint"
},
"release": {
"branches": [
Expand Down
5 changes: 5 additions & 0 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,28 @@ const bonjour = require('./cartridges/app_core/cartridge/scripts/bonjour')
const petstore = require('./cartridges/app_core/cartridge/scripts/petstore')
const things = require('./cartridges/app_brand/cartridge/scripts/things')


describe('babel-plugin-sfcc-modules', () => {
it('can handle require(\'*\') with a module in cartridge path behind.', () => {
expect(hello).toBe('Hello World')
})


it('can handle require(\'*\') with a module in cartridge path before', () => {
expect(hallo).toBe('Hallo Welt')
})


it('can handle require(\'*\') with a module in the same cartridge in cartridge path', () => {
expect(bonjour).toBe('Bonjour monde')
})


it('can handle require(\'^\')', () => {
expect(petstore).toBe('Cat')
})


it('can handle module.exports', () => {
expect(things()).toStrictEqual(['badger', 'mushroom', 'snake'])
})
Expand Down
Loading

0 comments on commit 6628679

Please sign in to comment.