Skip to content

Commit 00ad016

Browse files
author
Micha Reiser
authored
Upgrade dependencies (#45)
1 parent c9393e9 commit 00ad016

8 files changed

+3475
-3027
lines changed

.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
language: node_js
22
node_js:
33
- stable
4+
- 14
5+
- 12
46
- 10
5-
- 8
67

78
script:
89
- npm test

package.json

+14-13
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"main": "dist/index.js",
77
"typings": "./i18n.d.ts",
88
"engines": {
9-
"node": ">=8.0.0"
9+
"node": ">=10.14.2"
1010
},
1111
"scripts": {
1212
"pretest": "tsc",
@@ -17,11 +17,11 @@
1717
"author": "Micha Reiser <[email protected]>",
1818
"license": "MIT",
1919
"dependencies": {
20-
"acorn": "^6.1.1",
21-
"ast-types": "^0.12.2",
20+
"acorn": "^8.0.4",
21+
"ast-types": "^0.14.2",
2222
"cheerio": "^1.0.0-rc.2",
23-
"escodegen": "^1.11.1",
24-
"htmlparser2": "^3.10.1",
23+
"escodegen": "^2.0.0",
24+
"htmlparser2": "^5.0.0",
2525
"loader-utils": "^1.2.3",
2626
"source-map": "^0.7.3",
2727
"webpack": "^4.29.6",
@@ -34,21 +34,22 @@
3434
"@babel/preset-typescript": "^7.3.3",
3535
"@types/acorn": "^4.0.5",
3636
"@types/cheerio": "^0.22.11",
37+
"@types/domhandler": "^2.4.1",
3738
"@types/escodegen": "^0.0.6",
38-
"@types/estree": "^0.0.39",
39+
"@types/estree": "^0.0.45",
3940
"@types/htmlparser2": "^3.7.31",
40-
"@types/jest": "^24.0.11",
41+
"@types/jest": "^26.0.15",
4142
"@types/loader-utils": "^1.1.3",
42-
"@types/node": "^11.11.3",
43+
"@types/node": "^14.14.2",
4344
"@types/webpack": "^4.4.25",
4445
"@types/webpack-sources": "^0.1.5",
45-
"babel-jest": "^24.5.0",
46+
"babel-jest": "^26.6.0",
4647
"deep-extend": "^0.6.0",
4748
"html-loader": "^0.5.5",
48-
"jest": "^24.5.0",
49-
"memfs": "^2.15.2",
50-
"prettier": "^1.16.4",
51-
"typescript": "^3.3.3333",
49+
"jest": "^26.6.0",
50+
"memfs": "^3.2.0",
51+
"prettier": "^2.1.2",
52+
"typescript": "^4.0.3",
5253
"unionfs": "^4.2.0"
5354
}
5455
}

src/html/translate-html-parser.ts

+19-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from "path";
2-
import * as htmlparser from "htmlparser2";
2+
import type {DomHandler} from "domhandler";
3+
import {Parser} from 'htmlparser2';
34

45
import Translation from "../translation";
56
import ElementContext, {
@@ -29,9 +30,9 @@ function isAngularExpression(value: string): boolean {
2930
* Attributes translated with the translate filter are handled in the opentag event
3031
* Expressions used in the body of an element are translated in the text event.
3132
*/
32-
export default class TranslateHtmlParser implements htmlparser.Handler {
33+
export default class TranslateHtmlParser implements Partial<DomHandler> {
3334
context: HtmlParseContext;
34-
parser: htmlparser.Parser;
35+
parser: Parser;
3536

3637
constructor(
3738
private loader: TranslateLoaderContext,
@@ -42,7 +43,7 @@ export default class TranslateHtmlParser implements htmlparser.Handler {
4243

4344
parse(html: string): void {
4445
this.context = new DocumentContext(this.loader, html);
45-
this.parser = new htmlparser.Parser(this, { decodeEntities: true });
46+
this.parser = new Parser(this, { decodeEntities: true });
4647
this.parser.parseComplete(html);
4748

4849
this.context = this.parser = null;
@@ -76,31 +77,24 @@ export default class TranslateHtmlParser implements htmlparser.Handler {
7677
});
7778
}
7879

79-
onclosetag(name: string): void {
80+
onclosetag(): void {
8081
if (!(this.context instanceof ElementContext)) {
8182
throw new Error("onopentag did not create an element context");
8283
}
8384

84-
if (this.context.tagName !== name) {
85-
this.context.emitSuppressableError(
86-
"Error parsing html, close tag does not match open tag",
87-
this.context.elementStartPosition
88-
);
89-
} else {
90-
const element: AngularElement = {
91-
tagName: this.context.tagName,
92-
attributes: this.context.attributes,
93-
texts: this.context.texts,
94-
startPosition: this.context.elementStartPosition
95-
};
96-
97-
const extractorContext = this.createExtractorContext();
98-
99-
for (const extractor of this.translationExtractors) {
100-
extractor(element, extractorContext);
101-
}
102-
}
85+
const element: AngularElement = {
86+
tagName: this.context.tagName,
87+
attributes: this.context.attributes,
88+
texts: this.context.texts,
89+
startPosition: this.context.elementStartPosition
90+
};
91+
92+
const extractorContext = this.createExtractorContext();
10393

94+
for (const extractor of this.translationExtractors) {
95+
extractor(element, extractorContext);
96+
}
97+
10498
this.context = this.context.leave();
10599
}
106100

@@ -147,6 +141,6 @@ export default class TranslateHtmlParser implements htmlparser.Handler {
147141
}
148142
}
149143

150-
function getStartIndex(parser: htmlparser.Parser): number {
144+
function getStartIndex(parser: Parser): number {
151145
return (parser as any).startIndex;
152146
}

src/js/js-loader.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ async function extractTranslations(
6262
sourceMaps: any
6363
) {
6464
const options: LoaderOptions = loaderUtils.getOptions(loader) || {};
65-
const parserOptions = options.parserOptions || {};
65+
const parserOptions = options.parserOptions || { ecmaVersion: "latest" };
6666

6767
loader.pruneTranslations(path.relative(loader.context, loader.resourcePath));
6868

@@ -78,7 +78,7 @@ async function extractTranslations(
7878
comment: true,
7979
sourceMap: generateSourceMaps ? loader.resourcePath : undefined,
8080
sourceMapWithCode: generateSourceMaps,
81-
sourceContent: generateSourceMaps ? source : undefined
81+
sourceContent: generateSourceMaps ? source : undefined,
8282
});
8383

8484
if (generateSourceMaps) {

src/js/translate-visitor.ts

+31-41
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
import * as path from "path";
2-
import types, { NodePath } from "ast-types";
2+
import type {NodePath} from "ast-types/lib/node-path";
3+
import {namedTypes as n, builders as b, PathVisitor} from "ast-types";
34
import { Context } from "ast-types/lib/path-visitor";
4-
import {
5-
CallExpression,
6-
Literal,
7-
ArrayExpression,
8-
ObjectExpression,
9-
Identifier,
10-
Node,
11-
MemberExpression
12-
} from "ast-types/gen/nodes";
5+
import type { namedTypes } from "ast-types";
136
import Translation from "../translation";
147
import TranslateLoaderContext from "../translate-loader-context";
158

16-
const { namedTypes: n, builders: b } = types;
179
const TRANSLATE_SERVICE_NAME = "$translate";
1810

1911
export default function createTranslateVisitor(
2012
loader: TranslateLoaderContext,
21-
parserOptions: acorn.Options = {}
13+
parserOptions: acorn.Options = { ecmaVersion: "latest" }
2214
) {
2315
let context: Context = null;
2416
const comments: acorn.Comment[] = [];
@@ -34,7 +26,7 @@ export default function createTranslateVisitor(
3426
* Handles a $translate(translateId, interpolateParams, interpolationId, defaultText) call.
3527
* @param path the path to the call expression
3628
*/
37-
function visitTranslate(path: NodePath<CallExpression>): void {
29+
function visitTranslate(path: NodePath<namedTypes.CallExpression>): void {
3830
const call = path.node;
3931
const args = call.arguments;
4032

@@ -55,19 +47,19 @@ export default function createTranslateVisitor(
5547
}
5648

5749
function getTranslationIdFromTranslateCall(
58-
path: NodePath<CallExpression>
50+
path: NodePath<namedTypes.CallExpression>
5951
): any[] {
6052
const args = path.node.arguments;
6153

6254
if (n.Literal.check(args[0])) {
63-
return [(<Literal>args[0]).value];
55+
return [args[0].value];
6456
}
6557

6658
if (n.ArrayExpression.check(args[0])) {
67-
const arrayExpression = <ArrayExpression>args[0];
59+
const arrayExpression = args[0];
6860
return arrayExpression.elements.map(element => {
6961
if (n.Literal.check(element)) {
70-
return (<Literal>element).value;
62+
return element.value;
7163
}
7264
throwSuppressableError(
7365
"The array with the translation ids should only contain literals",
@@ -83,13 +75,13 @@ export default function createTranslateVisitor(
8375
}
8476

8577
function getDefaultTextFromTranslateCall(
86-
path: NodePath<CallExpression>
78+
path: NodePath<namedTypes.CallExpression>
8779
): any {
8880
const args = path.node.arguments;
8981

9082
if (args.length > 3) {
9183
if (n.Literal.check(args[3])) {
92-
return (<Literal>args[3]).value;
84+
return args[3].value;
9385
}
9486

9587
throwSuppressableError(
@@ -107,7 +99,7 @@ export default function createTranslateVisitor(
10799
* translation id.
108100
* @param path of the call expression.
109101
*/
110-
function visitRegisterTranslation(path: NodePath<CallExpression>): void {
102+
function visitRegisterTranslation(path: NodePath<namedTypes.CallExpression>): void {
111103
const call = path.node,
112104
args = call.arguments;
113105

@@ -118,12 +110,12 @@ export default function createTranslateVisitor(
118110
);
119111
}
120112

121-
const translationId = (<Literal>args[0]).value;
113+
const translationId = args[0].value;
122114
let defaultText: any;
123115

124116
if (args.length > 1) {
125117
if (n.Literal.check(args[1])) {
126-
defaultText = <string>(<Literal>args[1]).value;
118+
defaultText = <string>args[1].value;
127119
} else {
128120
throwError(
129121
"Illegal argument for call to i18n.registerTranslation: the default text has to be a literal",
@@ -158,9 +150,7 @@ export default function createTranslateVisitor(
158150
);
159151
}
160152

161-
const translations: Translation[] = (<ObjectExpression>(
162-
translationsArgument
163-
)).properties.map(property => {
153+
const translations: Translation[] = (translationsArgument).properties.map(property => {
164154
let translationId: any;
165155
let defaultText: any;
166156

@@ -177,9 +167,9 @@ export default function createTranslateVisitor(
177167
}
178168

179169
if (n.Identifier.check(property.key)) {
180-
translationId = (<Identifier>property.key).name;
170+
translationId = property.key.name;
181171
} else if (n.Literal.check(property.key)) {
182-
translationId = (<Literal>property.key).value;
172+
translationId = property.key.value;
183173
} else {
184174
throwError(
185175
"Illegal argument for call to i18n.registerTranslations: The key needs to be a literal or an identifier.",
@@ -188,7 +178,7 @@ export default function createTranslateVisitor(
188178
}
189179

190180
if (n.Literal.check(property.value)) {
191-
defaultText = (<Literal>property.value).value;
181+
defaultText = property.value.value;
192182
} else {
193183
throwError(
194184
`Illegal argument for call to i18n.registerTranslations: The value for the key '${translationId}' needs to be a literal`,
@@ -212,7 +202,7 @@ export default function createTranslateVisitor(
212202
function createTranslation(
213203
translationId: any,
214204
defaultText: any,
215-
node: Node
205+
node: namedTypes.Node
216206
): Translation {
217207
const idAsString = valueToString(translationId, "");
218208
const defaultTextAsString = valueToString(defaultText, undefined);
@@ -227,14 +217,14 @@ export default function createTranslateVisitor(
227217
* @param call the call expression
228218
* @returns {string} the name of the function
229219
*/
230-
function getFunctionName(call: CallExpression): string | undefined {
220+
function getFunctionName(call: namedTypes.CallExpression): string | undefined {
231221
var callee = call.callee;
232222
if (n.Identifier.check(callee)) {
233-
return (<Identifier>callee).name;
223+
return callee.name;
234224
} else if (n.MemberExpression.check(callee)) {
235-
const property = (<MemberExpression>callee).property;
225+
const property = callee.property;
236226
if (n.Identifier.check(property)) {
237-
return (<Identifier>property).name;
227+
return property.name;
238228
}
239229
return "[expression]";
240230
} else if (n.FunctionExpression.check(callee)) {
@@ -249,18 +239,18 @@ export default function createTranslateVisitor(
249239
* @param call the call expression
250240
* @returns {string} the name of the callee or null if the name could not be determined
251241
*/
252-
function getCalleeName(call: CallExpression): string | null {
242+
function getCalleeName(call: namedTypes.CallExpression): string | null {
253243
// this.method() or object.method()
254244
if (call.callee.type === "MemberExpression") {
255-
const member = <MemberExpression>call.callee;
245+
const member = call.callee;
256246
if (member.object.type === "Identifier") {
257-
return (<Identifier>member.object).name;
247+
return member.object.name;
258248
} else if (member.object.type === "ThisExpression") {
259249
return "this";
260250
} else if (member.object.type == "MemberExpression") {
261-
const parent = <MemberExpression>member.object;
251+
const parent = member.object;
262252
if (parent.property.type === "Identifier") {
263-
return (<Identifier>parent.property).name;
253+
return parent.property.name;
264254
}
265255
}
266256
}
@@ -274,7 +264,7 @@ export default function createTranslateVisitor(
274264
* @param message the message to emit
275265
* @param node the node for which a message is emitted
276266
*/
277-
function throwError(message: string, node: Node): never {
267+
function throwError(message: string, node: namedTypes.Node): never {
278268
const relativePath = path.relative(loader.context, loader.resourcePath);
279269
const start = node.loc!.start,
280270
completeMessage = `${message} (${relativePath}:${start.line}:${
@@ -325,8 +315,8 @@ export default function createTranslateVisitor(
325315
return "" + value;
326316
}
327317

328-
const visitor = types.PathVisitor.fromMethodsObject({
329-
visitCallExpression(path: NodePath<CallExpression>): boolean {
318+
const visitor = PathVisitor.fromMethodsObject({
319+
visitCallExpression(path: NodePath<namedTypes.CallExpression>): boolean {
330320
context = this;
331321
const call = path.node,
332322
functionName = getFunctionName(call),

0 commit comments

Comments
 (0)