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

(@fluent/dedent) Migrate to TypeScript #451

Merged
merged 5 commits into from
Feb 7, 2020
Merged
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
2 changes: 1 addition & 1 deletion fluent-bundle/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ html:
--excludePrivate \
--logger none \
--hideGenerator
@echo -e " $(OK) html build"
@echo -e " $(OK) html built"

clean:
@rm -f esm/*.js esm/*.d.ts esm/.compiled
Expand Down
16 changes: 0 additions & 16 deletions fluent-dedent/.esdoc.json

This file was deleted.

1 change: 1 addition & 0 deletions fluent-dedent/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
esm/
/index.js
/compat.js
4 changes: 3 additions & 1 deletion fluent-dedent/.npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
.nyc_output
coverage
docs
esm/.compiled
src
test
makefile
tsconfig.json
44 changes: 35 additions & 9 deletions fluent-dedent/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,58 @@ GLOBAL := FluentDedent

include ../common.mk

build: index.js compat.js
lint:
@eslint --config $(ROOT)/eslint_ts.json --max-warnings 0 src/*.ts
@eslint --config $(ROOT)/eslint_test.json --max-warnings 0 test/
@echo -e " $(OK) lint"

.PHONY: compile
compile: esm/.compiled

esm/.compiled: $(SOURCES)
@tsc
@touch $@
@echo -e " $(OK) esm/ compiled"

test:
.PHONY: test
test: esm/.compiled
@nyc --reporter=text --reporter=html mocha \
--recursive --ui tdd \
--require esm \
test/**/*_test.js

index.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
build: index.js compat.js

index.js: esm/.compiled
@rollup $(CURDIR)/esm/index.js \
--config $(ROOT)/bundle_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
--output.file $@
@echo -e " $(OK) $@ built"

compat.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
compat.js: esm/.compiled
@rollup $(CURDIR)/esm/index.js \
--config $(ROOT)/compat_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
--amd.id $(PACKAGE) \
--name $(GLOBAL) \
--output.file $@
@echo -e " $(OK) $@ built"

lint: _lint
html: _html
clean: _clean
html:
@typedoc src \
--out ../html/dedent \
--mode file \
--excludeNotExported \
--excludePrivate \
--logger none \
--hideGenerator
@echo -e " $(OK) html built"

clean:
@rm -f esm/*.js esm/*.d.ts esm/.compiled
@rm -f index.js compat.js
@rm -rf .nyc_output coverage
@echo -e " $(OK) clean"
7 changes: 3 additions & 4 deletions fluent-dedent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
"email": "stas@mozilla.com"
}
],
"directories": {
"lib": "./src"
},
"type": "commonjs",
"main": "./index.js",
"module": "./src/index.js",
"module": "./esm/index.js",
"types": "./esm/index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/projectfluent/fluent.js.git"
Expand Down
33 changes: 21 additions & 12 deletions fluent-dedent/src/index.js → fluent-dedent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,46 @@ const RE_BLANK = /^[ \t]*$/;
* line and it must end on a line of its own, with the closing delimiter on a
* next line.
*
* @param {Array<string>} strings
* @param {...any} values
* @returns string
* @param strings
* @param values
*/
export default function ftl(strings, ...values) {
export default function ftl(
strings: TemplateStringsArray,
...values: Array<unknown>
): string {
let code = strings.reduce((acc, cur) => acc + values.shift() + cur);
let lines = code.split("\n");
let [first, commonIndent] = [lines.shift(), lines.pop()];

if (!RE_BLANK.test(first)) {
let first = lines.shift();
if (first === undefined || !RE_BLANK.test(first)) {
throw new RangeError("Content must start on a new line.");
}
if (!RE_BLANK.test(commonIndent)) {

let commonIndent = lines.pop();
if (commonIndent === undefined || !RE_BLANK.test(commonIndent)) {
throw new RangeError("Closing delimiter must appear on a new line.");
}

function dedent(line, idx) {
let dedented = [];
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
let lineIndent = line.slice(0, commonIndent.length);
if (lineIndent.length === 0) {
// Empty blank lines are preserved even if technically they are not
// indented at all. This also short-circuits the dedentation logic when
// commonIndent.length is 0, i.e. when all indents should be kept.
return line;
dedented.push(line);
continue;
}

if (lineIndent !== commonIndent) {
// The indentation of the line must match commonIndent exacty.
throw new RangeError(`Insufficient indentation in line ${idx + 1}.`);
throw new RangeError(`Insufficient indentation in line ${i + 1}.`);
}

// Strip commonIndent.
return line.slice(commonIndent.length);
dedented.push(line.slice(commonIndent.length));
}

return lines.map(dedent).join("\n");
return dedented.join("\n");
}
2 changes: 1 addition & 1 deletion fluent-dedent/test/blank_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import assert from "assert";
import ftl from "../src/index";
import ftl from "../esm/index";

suite("blank lines", function() {
test("leading", function() {
Expand Down
2 changes: 1 addition & 1 deletion fluent-dedent/test/content_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import assert from "assert";
import ftl from "../src/index";
import ftl from "../esm/index";

suite("content lines", function() {
test("no indent", function () {
Expand Down
2 changes: 1 addition & 1 deletion fluent-dedent/test/eol_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import assert from "assert";
import ftl from "../src/index";
import ftl from "../esm/index";

suite("EOL at extremes", function () {
test("no EOLs", function () {
Expand Down
2 changes: 1 addition & 1 deletion fluent-dedent/test/interpolation_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import assert from "assert";
import ftl from "../src/index";
import ftl from "../esm/index";

suite("interpolation", function() {
test("single", function() {
Expand Down
2 changes: 1 addition & 1 deletion fluent-dedent/test/mixed_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import assert from "assert";
import ftl from "../src/index";
import ftl from "../esm/index";

suite("mixed indent", function() {
test("same amount", function() {
Expand Down
2 changes: 1 addition & 1 deletion fluent-dedent/test/tabs_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";

import assert from "assert";
import ftl from "../src/index";
import ftl from "../esm/index";

suite("tab indent", function() {
test("same amount", function() {
Expand Down
16 changes: 16 additions & 0 deletions fluent-dedent/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es2019",
"module": "es2015",
"strict": true,
"allowJs": false,
"esModuleInterop": true,
"moduleResolution": "node",
"noEmitHelpers": true,
"declaration": true,
"outDir": "./esm",
},
"include": [
"./src/**/*.ts"
]
}
8 changes: 4 additions & 4 deletions fluent-sequence/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test: esm/.compiled

build: index.js compat.js

index.js: $(SOURCES)
index.js: esm/.compiled
@rollup $(CURDIR)/esm/index.js \
--config $(ROOT)/bundle_config.js \
--banner "/* $(PACKAGE)@$(VERSION) */" \
Expand All @@ -34,7 +34,7 @@ index.js: $(SOURCES)
--output.file $@
@echo -e " $(OK) $@ built"

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

html:
@typedoc src \
--out ../html/bundle \
--out ../html/sequence \
--mode file \
--excludeNotExported \
--excludePrivate \
--logger none \
--hideGenerator
@echo -e " $(OK) html build"
@echo -e " $(OK) html built"

clean:
@rm -f esm/*.js esm/*.d.ts esm/.compiled
Expand Down