Skip to content

Commit fb82e21

Browse files
authored
Merge pull request #451 from stasm/ts-dedent
(@fluent/dedent) Migrate to TypeScript
2 parents a07b7cd + edda60f commit fb82e21

15 files changed

+90
-53
lines changed

fluent-bundle/makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ html:
5151
--excludePrivate \
5252
--logger none \
5353
--hideGenerator
54-
@echo -e " $(OK) html build"
54+
@echo -e " $(OK) html built"
5555

5656
clean:
5757
@rm -f esm/*.js esm/*.d.ts esm/.compiled

fluent-dedent/.esdoc.json

-16
This file was deleted.

fluent-dedent/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
esm/
12
/index.js
23
/compat.js

fluent-dedent/.npmignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.nyc_output
22
coverage
3-
docs
3+
esm/.compiled
4+
src
45
test
56
makefile
7+
tsconfig.json

fluent-dedent/makefile

+35-9
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,58 @@ GLOBAL := FluentDedent
33

44
include ../common.mk
55

6-
build: index.js compat.js
6+
lint:
7+
@eslint --config $(ROOT)/eslint_ts.json --max-warnings 0 src/*.ts
8+
@eslint --config $(ROOT)/eslint_test.json --max-warnings 0 test/
9+
@echo -e " $(OK) lint"
10+
11+
.PHONY: compile
12+
compile: esm/.compiled
13+
14+
esm/.compiled: $(SOURCES)
15+
@tsc
16+
@touch $@
17+
@echo -e " $(OK) esm/ compiled"
718

8-
test:
19+
.PHONY: test
20+
test: esm/.compiled
921
@nyc --reporter=text --reporter=html mocha \
1022
--recursive --ui tdd \
1123
--require esm \
1224
test/**/*_test.js
1325

14-
index.js: $(SOURCES)
15-
@rollup $(CURDIR)/src/index.js \
26+
build: index.js compat.js
27+
28+
index.js: esm/.compiled
29+
@rollup $(CURDIR)/esm/index.js \
1630
--config $(ROOT)/bundle_config.js \
1731
--banner "/* $(PACKAGE)@$(VERSION) */" \
1832
--amd.id $(PACKAGE) \
1933
--name $(GLOBAL) \
2034
--output.file $@
2135
@echo -e " $(OK) $@ built"
2236

23-
compat.js: $(SOURCES)
24-
@rollup $(CURDIR)/src/index.js \
37+
compat.js: esm/.compiled
38+
@rollup $(CURDIR)/esm/index.js \
2539
--config $(ROOT)/compat_config.js \
2640
--banner "/* $(PACKAGE)@$(VERSION) */" \
2741
--amd.id $(PACKAGE) \
2842
--name $(GLOBAL) \
2943
--output.file $@
3044
@echo -e " $(OK) $@ built"
3145

32-
lint: _lint
33-
html: _html
34-
clean: _clean
46+
html:
47+
@typedoc src \
48+
--out ../html/dedent \
49+
--mode file \
50+
--excludeNotExported \
51+
--excludePrivate \
52+
--logger none \
53+
--hideGenerator
54+
@echo -e " $(OK) html built"
55+
56+
clean:
57+
@rm -f esm/*.js esm/*.d.ts esm/.compiled
58+
@rm -f index.js compat.js
59+
@rm -rf .nyc_output coverage
60+
@echo -e " $(OK) clean"

fluent-dedent/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
"email": "[email protected]"
1212
}
1313
],
14-
"directories": {
15-
"lib": "./src"
16-
},
14+
"type": "commonjs",
1715
"main": "./index.js",
18-
"module": "./src/index.js",
16+
"module": "./esm/index.js",
17+
"types": "./esm/index.d.ts",
1918
"repository": {
2019
"type": "git",
2120
"url": "https://github.com/projectfluent/fluent.js.git"

fluent-dedent/src/index.js renamed to fluent-dedent/src/index.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,46 @@ const RE_BLANK = /^[ \t]*$/;
99
* line and it must end on a line of its own, with the closing delimiter on a
1010
* next line.
1111
*
12-
* @param {Array<string>} strings
13-
* @param {...any} values
14-
* @returns string
12+
* @param strings
13+
* @param values
1514
*/
16-
export default function ftl(strings, ...values) {
15+
export default function ftl(
16+
strings: TemplateStringsArray,
17+
...values: Array<unknown>
18+
): string {
1719
let code = strings.reduce((acc, cur) => acc + values.shift() + cur);
1820
let lines = code.split("\n");
19-
let [first, commonIndent] = [lines.shift(), lines.pop()];
2021

21-
if (!RE_BLANK.test(first)) {
22+
let first = lines.shift();
23+
if (first === undefined || !RE_BLANK.test(first)) {
2224
throw new RangeError("Content must start on a new line.");
2325
}
24-
if (!RE_BLANK.test(commonIndent)) {
26+
27+
let commonIndent = lines.pop();
28+
if (commonIndent === undefined || !RE_BLANK.test(commonIndent)) {
2529
throw new RangeError("Closing delimiter must appear on a new line.");
2630
}
2731

28-
function dedent(line, idx) {
32+
let dedented = [];
33+
for (let i = 0; i < lines.length; i++) {
34+
let line = lines[i];
2935
let lineIndent = line.slice(0, commonIndent.length);
3036
if (lineIndent.length === 0) {
3137
// Empty blank lines are preserved even if technically they are not
3238
// indented at all. This also short-circuits the dedentation logic when
3339
// commonIndent.length is 0, i.e. when all indents should be kept.
34-
return line;
40+
dedented.push(line);
41+
continue;
3542
}
43+
3644
if (lineIndent !== commonIndent) {
3745
// The indentation of the line must match commonIndent exacty.
38-
throw new RangeError(`Insufficient indentation in line ${idx + 1}.`);
46+
throw new RangeError(`Insufficient indentation in line ${i + 1}.`);
3947
}
48+
4049
// Strip commonIndent.
41-
return line.slice(commonIndent.length);
50+
dedented.push(line.slice(commonIndent.length));
4251
}
4352

44-
return lines.map(dedent).join("\n");
53+
return dedented.join("\n");
4554
}

fluent-dedent/test/blank_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
import assert from "assert";
4-
import ftl from "../src/index";
4+
import ftl from "../esm/index";
55

66
suite("blank lines", function() {
77
test("leading", function() {

fluent-dedent/test/content_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
import assert from "assert";
4-
import ftl from "../src/index";
4+
import ftl from "../esm/index";
55

66
suite("content lines", function() {
77
test("no indent", function () {

fluent-dedent/test/eol_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
import assert from "assert";
4-
import ftl from "../src/index";
4+
import ftl from "../esm/index";
55

66
suite("EOL at extremes", function () {
77
test("no EOLs", function () {

fluent-dedent/test/interpolation_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
import assert from "assert";
4-
import ftl from "../src/index";
4+
import ftl from "../esm/index";
55

66
suite("interpolation", function() {
77
test("single", function() {

fluent-dedent/test/mixed_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
import assert from "assert";
4-
import ftl from "../src/index";
4+
import ftl from "../esm/index";
55

66
suite("mixed indent", function() {
77
test("same amount", function() {

fluent-dedent/test/tabs_test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22

33
import assert from "assert";
4-
import ftl from "../src/index";
4+
import ftl from "../esm/index";
55

66
suite("tab indent", function() {
77
test("same amount", function() {

fluent-dedent/tsconfig.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2019",
4+
"module": "es2015",
5+
"strict": true,
6+
"allowJs": false,
7+
"esModuleInterop": true,
8+
"moduleResolution": "node",
9+
"noEmitHelpers": true,
10+
"declaration": true,
11+
"outDir": "./esm",
12+
},
13+
"include": [
14+
"./src/**/*.ts"
15+
]
16+
}

fluent-sequence/makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test: esm/.compiled
2525

2626
build: index.js compat.js
2727

28-
index.js: $(SOURCES)
28+
index.js: esm/.compiled
2929
@rollup $(CURDIR)/esm/index.js \
3030
--config $(ROOT)/bundle_config.js \
3131
--banner "/* $(PACKAGE)@$(VERSION) */" \
@@ -34,7 +34,7 @@ index.js: $(SOURCES)
3434
--output.file $@
3535
@echo -e " $(OK) $@ built"
3636

37-
compat.js: $(SOURCES)
37+
compat.js: esm/.compiled
3838
@rollup $(CURDIR)/esm/index.js \
3939
--config $(ROOT)/compat_config.js \
4040
--banner "/* $(PACKAGE)@$(VERSION) */" \
@@ -45,13 +45,13 @@ compat.js: $(SOURCES)
4545

4646
html:
4747
@typedoc src \
48-
--out ../html/bundle \
48+
--out ../html/sequence \
4949
--mode file \
5050
--excludeNotExported \
5151
--excludePrivate \
5252
--logger none \
5353
--hideGenerator
54-
@echo -e " $(OK) html build"
54+
@echo -e " $(OK) html built"
5555

5656
clean:
5757
@rm -f esm/*.js esm/*.d.ts esm/.compiled

0 commit comments

Comments
 (0)