Skip to content
This repository has been archived by the owner on Feb 4, 2018. It is now read-only.

Commit

Permalink
Merge pull request #113 from bem-sdk/fix/typings
Browse files Browse the repository at this point in the history
Fix typings
  • Loading branch information
blond authored Apr 20, 2017
2 parents 9ac650f + cedd0ea commit a97884b
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 115 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ TypeScript support

The package includes [typings](./index.d.ts) for TypeScript. You have to set up transpilation yourself. When you set `module` to `commonjs` in your `tsconfig.json` file, TypeScript will automatically find the type definitions for `@bem/entity-name`.

The interfaces are provided in global namespace `BemSDK.EntityName`. It is necessary to use interfaces in JsDoc.
The interfaces are provided in global namespace `BEMSDK.EntityName`. It is necessary to use interfaces in JsDoc.

Debuggability
-------------
Expand Down
26 changes: 1 addition & 25 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,3 @@
import './globals.d';

declare class BemEntityName {
constructor(obj: BemSDK.EntityName.Options);

readonly block: string;
readonly elem: string | undefined;
readonly mod: BemSDK.EntityName.ModifierRepresentation | undefined;
readonly modName: string | undefined;
readonly modVal: string | true | undefined;
readonly type: BemSDK.EntityName.TYPE;
readonly scope: BemEntityName | null;
readonly id: string;

isSimpleMod(): boolean | null;
isEqual(entityName: BemEntityName): boolean;
belongsTo(entityName: BemEntityName): boolean;
valueOf(): BemSDK.EntityName.StrictRepresentation;
toJSON(): BemSDK.EntityName.StrictRepresentation;
toString(): string;
inspect(depth: number, options: object): string;

static create(obj: BemSDK.EntityName.NonStrictRepresentation | string): BemEntityName;
static isBemEntityName(entityName: any): boolean;
}
import BemEntityName from './types/entity-name.d';

export = BemEntityName;
23 changes: 15 additions & 8 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs"
},
"files": [
"index.js",
"index.d.ts"
]
"compilerOptions": {
"target": "ES6",
"module": "commonjs"
},
"include": [
"lib",
"types"
],
"exclude": [
"node_modules"
],
"files": [
"index.js",
"index.d.ts"
]
}
24 changes: 12 additions & 12 deletions lib/entity-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const TYPES = {

class BemEntityName {
/**
* @param {BemSDK.EntityName.Options} obj — representation of entity name.
* @param {BEMSDK.EntityName.Options} obj — representation of entity name.
*/
constructor(obj) {
if (!obj.block) {
Expand Down Expand Up @@ -61,7 +61,7 @@ class BemEntityName {
*
* name.block; // button
*
* @returns {string} name of entity block.
* @returns {BEMSDK.EntityName.BlockName} name of entity block.
*/
get block() { return this._data.block; }

Expand All @@ -76,7 +76,7 @@ class BemEntityName {
*
* name.elem; // text
*
* @returns {?string} - name of entity element.
* @returns {?BEMSDK.EntityName.ElementName} - name of entity element.
*/
get elem() { return this._data.elem; }

Expand All @@ -94,7 +94,7 @@ class BemEntityName {
* modName.mod; // { name: 'disabled', val: true }
* blockName.mod; // undefined
*
* @returns {?BemSDK.EntityName.ModifierRepresentation} - entity modifier.
* @returns {?BEMSDK.EntityName.Modifier} - entity modifier.
*/
get mod() { return this._data.mod; }

Expand All @@ -103,7 +103,7 @@ class BemEntityName {
*
* If entity is not modifier then returns `undefined`.
*
* @returns {?string} - entity modifier name.
* @returns {?BEMSDK.EntityName.ModifierName} - entity modifier name.
* @deprecated use {@link BemEntityName#mod.name}
*/
get modName() {
Expand All @@ -117,7 +117,7 @@ class BemEntityName {
*
* If entity is not modifier then returns `undefined`.
*
* @returns {?(string|true)} - entity modifier name.
* @returns {?BEMSDK.EntityName.ModifierValue} - entity modifier name.
* @deprecated use {@link BemEntityName#mod.val}
*/
get modVal() {
Expand All @@ -141,7 +141,7 @@ class BemEntityName {
*
* name.type; // elemMod
*
* @returns {string} - type of entity. One of 'block', 'elem', 'blockMod', 'elemMod'.
* @returns {BEMSDK.EntityName.Type} - type of entity.
*/
get type() {
if (this._type) { return this._type; }
Expand Down Expand Up @@ -172,7 +172,7 @@ class BemEntityName {
* buttonTextName.scope; // BemEntityName { block: 'button' }
* buttonTextBoldName.scope; // BemEntityName { block: 'button', elem: 'elem' }
*
* @returns {BemEntityName} - scope entity name.
* @returns {(BemEntityName|null)} - scope entity name.
*/
get scope() {
if (this.type === TYPES.BLOCK) { return null; }
Expand Down Expand Up @@ -200,7 +200,7 @@ class BemEntityName {
*
* name.id; // button_disabled
*
* @returns {string} - id of entity.
* @returns {BEMSDK.EntityName.Id} - id of entity.
*/
get id() {
if (this._id) { return this._id; }
Expand Down Expand Up @@ -300,7 +300,7 @@ class BemEntityName {
*
* // ➜ { block: 'button', mod: { name: 'focused', value: true } }
*
* @returns {BemSDK.EntityName.StrictRepresentation}
* @returns {BEMSDK.EntityName.Representation}
*/
valueOf() { return this._data; }

Expand All @@ -314,7 +314,7 @@ class BemEntityName {
*
* JSON.stringify(name); // {"block":"input","mod":{"name":"available","val":true}}
*
* @returns {BemSDK.EntityName.StrictRepresentation}
* @returns {BEMSDK.EntityName.Representation}
*/
toJSON() {
return this._data;
Expand Down Expand Up @@ -373,7 +373,7 @@ class BemEntityName {
* BemEntityName.create({ block: 'my-button', modName: 'theme', modVal: 'red' });
* // → BemEntityName { block: 'my-button', mod: { name: 'theme', val: 'red' } }
*
* @param {(BemSDK.EntityName.NonStrictRepresentation|string)} obj — representation of entity name.
* @param {(BEMSDK.EntityName.CreateOptions|string)} obj — representation of entity name.
* @returns {BemEntityName} An object representing entity name.
*/
static create(obj) {
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"typings": "index.d.ts",
"files": [
"lib/**",
"types/**",
"index.js",
"index.d.ts",
"globals.d.ts"
"index.d.ts"
],
"engines": {
"node": ">= 4.0"
Expand All @@ -38,6 +38,7 @@
"es6-error": "4.0.2"
},
"devDependencies": {
"@types/node": "^4.2.5",
"@types/proxyquire": "^1.3.27",
"@types/sinon": "^2.1.2",
"ava": "^0.19.0",
Expand All @@ -56,7 +57,7 @@
"test": "nyc ava",
"lint": "npm run lint:js && npm run lint:dts",
"lint:js": "eslint .",
"lint:dts": "tslint **.d.ts",
"lint:dts": "tslint types/*.d.ts",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"ava": {
Expand Down
4 changes: 2 additions & 2 deletions test/belongs-to.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('ava');
import test from 'ava';

const BemEntityName = require('../lib/entity-name');
import BemEntityName from '..';

test('should not detect belonging between block and itself', t => {
const blockName = new BemEntityName({ block: 'block' });
Expand Down
2 changes: 1 addition & 1 deletion test/bem-fields.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import BemEntityName from '../lib/entity-name';
import BemEntityName from '..';

test('should provide `block` field', t => {
const entityName = new BemEntityName({ block: 'block' });
Expand Down
2 changes: 1 addition & 1 deletion test/create.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import BemEntityName from '../lib/entity-name';
import BemEntityName from '..';

test('should return object as is if it`s a BemEntityName', t => {
const entityName = new BemEntityName({ block: 'block' });
Expand Down
8 changes: 4 additions & 4 deletions test/deprecate.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const test = require('ava');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
import test from 'ava';
import sinon from 'sinon';
import proxyquire from 'proxyquire';

const BemEntityName = require('../lib/entity-name');
import BemEntityName from '..';

const deprecateSpy = sinon.spy();
const deprecate = proxyquire('../lib/deprecate', {
Expand Down
4 changes: 2 additions & 2 deletions test/entity-type-error.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('ava');
import test from 'ava';

const EntityTypeError = require('../lib/entity-type-error');
import EntityTypeError from '../lib/entity-type-error';

test('should create type error', t => {
const error = new EntityTypeError();
Expand Down
6 changes: 3 additions & 3 deletions test/id.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test from 'ava';
const sinon = require('sinon');
const proxyquire = require('proxyquire');
import sinon from 'sinon';
import proxyquire from 'proxyquire';

import BemEntityName from '../lib/entity-name';
import BemEntityName from '..';

test('should build equal id for equal blocks', t => {
const entityName1 = new BemEntityName({ block: 'block' });
Expand Down
8 changes: 4 additions & 4 deletions test/inspect.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import test from 'ava';
const sinon = require('sinon');
import {EOL} from 'os';

import BemEntityName from '../lib/entity-name';
import test from 'ava';
import sinon from 'sinon';

const EOL = require('os').EOL;
import BemEntityName from '..';

test.beforeEach(t => {
t.context.stdoutWriteStub = sinon.stub(process.stdout, 'write');
Expand Down
2 changes: 1 addition & 1 deletion test/is-bem-entity-name.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import BemEntityName from '../lib/entity-name';
import BemEntityName from '..';

test('should check valid entities', t => {
const entityName = new BemEntityName({ block: 'block' });
Expand Down
2 changes: 1 addition & 1 deletion test/is-equal.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import BemEntityName from '../lib/entity-name';
import BemEntityName from '..';

test('should detect equal block', t => {
const entityName1 = new BemEntityName({ block: 'block' });
Expand Down
2 changes: 1 addition & 1 deletion test/is-simple-mod.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import BemEntityName from '../lib/entity-name';
import BemEntityName from '..';

test('should be true for simple modifiers', t => {
const entityName = new BemEntityName({ block: 'block', mod: 'mod' });
Expand Down
2 changes: 1 addition & 1 deletion test/scope.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import BemEntityName from '../lib/entity-name';
import BemEntityName from '..';

test('should return scope of block', t => {
const entityName = new BemEntityName({ block: 'block' });
Expand Down
2 changes: 1 addition & 1 deletion test/to-json.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import BemEntityName from '../lib/entity-name';
import BemEntityName from '..';

test('should create stringified object', t => {
const entityName = new BemEntityName({ block: 'button' });
Expand Down
4 changes: 2 additions & 2 deletions test/to-string.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';
const sinon = require('sinon');
const proxyquire = require('proxyquire');
import sinon from 'sinon';
import proxyquire from 'proxyquire';

const spy = sinon.spy();
const BemEntityName = proxyquire('../lib/entity-name', {
Expand Down
2 changes: 1 addition & 1 deletion test/type.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import BemEntityName from '../lib/entity-name';
import BemEntityName from '..';

test('should determine block', t => {
const entityName = new BemEntityName({ block: 'block' });
Expand Down
2 changes: 1 addition & 1 deletion test/value-of.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava';

import BemEntityName from '../lib/entity-name';
import BemEntityName from '..';

test('should return normalized object', t => {
const entity = new BemEntityName({ block: 'block', mod: 'mod' });
Expand Down
27 changes: 27 additions & 0 deletions types/entity-name.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import './globals.d';

declare class BemEntityName {
constructor(obj: BEMSDK.EntityName.Options);

readonly block: BEMSDK.EntityName.BlockName;
readonly elem: BEMSDK.EntityName.ElementName | undefined;
readonly mod: BEMSDK.EntityName.Modifier | undefined;
readonly modName: BEMSDK.EntityName.ModifierName | undefined;
readonly modVal: BEMSDK.EntityName.ModifierValue | undefined;
readonly type: BEMSDK.EntityName.Type;
readonly scope: BemEntityName | null;
readonly id: BEMSDK.EntityName.Id;

isSimpleMod(): boolean | null;
isEqual(entityName: BemEntityName): boolean;
belongsTo(entityName: BemEntityName): boolean;
valueOf(): BEMSDK.EntityName.Representation;
toJSON(): BEMSDK.EntityName.Representation;
toString(): string;
inspect(depth: number, options: object): string;

static create(obj: BEMSDK.EntityName.CreateOptions | string): BemEntityName;
static isBemEntityName(entityName: any): boolean;
}

export = BemEntityName;
Loading

0 comments on commit a97884b

Please sign in to comment.