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

Add typescript for naming.presets #323

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ packages/bemjson-to-jsx-demo

#Remove after AVA done
packages/config/test/*.test.js

# Build

es
lib
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ coverage
node_modules
npm-debug.log
lerna-debug.log

# Build

es
lib
typings
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
"nyc": "^11.0.3",
"proxyquire": "^1.8.0",
"sinon": "^2.3.6",
"ts-node": "^7.0.1",
"tslint": "^5.0.0",
"tslint-config-typings": "^0.3.1",
"typescript": "^2.4.1"
"typescript": "^3.0.1"
},
"scripts": {
"lint": "npm run lint:js && npm run lint:dts",
Expand Down
23 changes: 0 additions & 23 deletions packages/naming.presets/index.d.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/naming.presets/index.js

This file was deleted.

9 changes: 0 additions & 9 deletions packages/naming.presets/legacy.js

This file was deleted.

13 changes: 0 additions & 13 deletions packages/naming.presets/origin-react.js

This file was deleted.

11 changes: 8 additions & 3 deletions packages/naming.presets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@
"engines": {
"node": ">= 8.0"
},
"main": "index.js",
"typings": "index.d.ts",
"main": "lib/index.js",
"module": "es/index.js",
"typings": "typings/index.d.ts",
"files": [
"*.js",
"index.d.ts"
],
"scripts": {
"test": "nyc mocha"
"build:cjs": "tsc --module commonjs --outDir lib",
"build:es": "tsc --module es2015 --outDir es",
"build": "npm run clean && npm run build:cjs && npm run build:es",
"clean": "rm -rf lib es typings",
"test": "nyc mocha -r ts-node/register"
}
}
9 changes: 0 additions & 9 deletions packages/naming.presets/react.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
'use strict';
import * as __presets__ from './';
import { Presets } from './types';

var presets = require('.');

var DEFAULT_PRESET = 'origin';
const DEFAULT_PRESET = 'origin';
// Use alias because namespace not has index signature
const presets: Presets = __presets__;

module.exports = init;

Expand All @@ -13,13 +14,13 @@ module.exports = init;
* @param {Object} [userDefaults] - defaults
* @returns {INamingConventionDelims}
*/
function init(options, userDefaults) {
function init(options: any, userDefaults: any) {
if (!options) {
return presets[DEFAULT_PRESET];
}

if (typeof options === 'string') {
var preset = presets[options];
const preset = presets[options];

if (!preset) {
throw new Error('The `' + options + '` naming is unknown.');
Expand All @@ -28,7 +29,7 @@ function init(options, userDefaults) {
return preset;
}

var defaultPreset = options.preset || DEFAULT_PRESET;
const defaultPreset = options.preset || DEFAULT_PRESET;

// TODO: Warn about incorrect preset
if (typeof userDefaults === 'string') {
Expand All @@ -37,11 +38,11 @@ function init(options, userDefaults) {
userDefaults = {};
}

var defaults = presets[defaultPreset];
var defaultDelims = userDefaults.delims || defaults.delims;
var defaultModDelims = userDefaults.mod || defaultDelims.mod;
var optionsDelims = options.delims || {};
var mod = optionsDelims.mod || defaultModDelims;
const defaults = presets[defaultPreset];
const defaultDelims = userDefaults.delims || defaults.delims;
const defaultModDelims = userDefaults.mod || defaultDelims.mod;
const optionsDelims = options.delims || {};
const mod = optionsDelims.mod || defaultModDelims;

const res = {
delims: {
Expand All @@ -53,7 +54,7 @@ function init(options, userDefaults) {
val: mod.val || defaultModDelims.val
}
},
fs: Object.assign({}, defaults.fs, userDefaults.fs, options.fs),
fs: { ...defaults.fs, ...userDefaults.fs, ...options.fs },
wordPattern: options.wordPattern || userDefaults.wordPattern || defaults.wordPattern
};

Expand Down
5 changes: 5 additions & 0 deletions packages/naming.presets/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { origin } from './origin';
export { legacy } from './legacy';
export { react } from './react';
export { originReact } from './origin-react';
export { twoDashes } from './two-dashes';
10 changes: 10 additions & 0 deletions packages/naming.presets/src/legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { INamingConvention } from './types';
import { origin } from './origin';

export const legacy: INamingConvention = {
...origin,
fs: {
...origin.fs,
pattern: '${entity}${layer?@${layer}}.${tech}'
}
};
15 changes: 15 additions & 0 deletions packages/naming.presets/src/origin-react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { INamingConvention } from './types';
import { origin } from './origin';

export const originReact: INamingConvention = {
...origin,
delims: {
...origin.delims,
elem: '-'
},
fs: {
...origin.fs,
delims: { elem: '' }
},
wordPattern: '[a-zA-Z0-9]+'
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
import { INamingConvention } from './types';

module.exports = {
export const origin: INamingConvention = {
delims: {
elem: '__',
mod: { name: '_', val: '_' }
Expand Down
10 changes: 10 additions & 0 deletions packages/naming.presets/src/react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { INamingConvention } from './types';
import { originReact } from './origin-react';

export const react: INamingConvention = {
...originReact,
fs: {
...originReact.fs,
pattern: '${entity}${layer?@${layer}}.${tech}'
}
};
14 changes: 14 additions & 0 deletions packages/naming.presets/src/two-dashes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { INamingConvention } from './types';

export const twoDashes: INamingConvention = {
delims: {
elem: '__',
mod: { name: '--', val: '_' }
},
fs: {
// delims: { elem: '__', mod: '--' }, // redundand because of defaults
pattern: '${layer}.blocks/${entity}.${tech}',
scheme: 'nested'
},
wordPattern: '[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*'
};
25 changes: 25 additions & 0 deletions packages/naming.presets/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export type Mod = {
name: string;
val: string;
};

export interface INamingConventionDelims {
elem: string;
mod?: Mod | string;
}

export type FileSystem = {
delims?: INamingConventionDelims;
pattern: string;
scheme: string;
};

export interface INamingConvention {
delims: INamingConventionDelims;
fs: FileSystem;
wordPattern: string;
}

export type Presets = {
[key: string]: INamingConvention
};
6 changes: 3 additions & 3 deletions packages/naming.presets/test/cell.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const BemEntityName = require('@bem/sdk.entity-name');

const createStringify = require('@bem/sdk.naming.cell.stringify');

const presets = require('..');
const presets = require('../lib');

const createPreset = (name, fsConv, conv) => {
const res = Object.assign({}, presets[name], conv);
Expand All @@ -22,8 +22,8 @@ describe('default', () => {
const originNested = createStringify(presets.origin);
const reactFlat = createStringify(createPreset('react', { scheme: 'flat' }));
const reactNested = createStringify(presets.react);
const twoFlat = createStringify(createPreset('two-dashes', { scheme: 'flat' }));
const twoNested = createStringify(presets['two-dashes']);
const twoFlat = createStringify(createPreset('twoDashes', { scheme: 'flat' }));
const twoNested = createStringify(presets['twoDashes']);

it('should return path + tech', () => {
expect(originNested(
Expand Down
8 changes: 8 additions & 0 deletions packages/naming.presets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.shared",
"compilerOptions": {
"rootDir": "src",
"declaration": true,
"declarationDir": "typings"
}
}
10 changes: 0 additions & 10 deletions packages/naming.presets/two-dashes.js

This file was deleted.

17 changes: 17 additions & 0 deletions tsconfig.shared.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"alwaysStrict": true,
"downlevelIteration": true,
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"pretty": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"target": "es5"
}
}
5 changes: 4 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "tslint-config-typings"
"extends": "tslint-config-typings",
"rules": {
"interface-name": [true, "always-prefix"]
}
}